1

I'm new to WPF and have noticed that my application keeps running after I click the "X" button to exit the window. I've programmed GUI's before, so I know that this is common. However, when I look into the issue, whatever solutions I find do not seem to make any sense.

I've found this Applicaton.ShutdownMode solution, but it seems like I am not implementing it correctly. I want my application to shutdown "OnMainWindowClose". This is a question that is pretty much the same as mine that I don't think was explained clearly enough in the accepted answer.

Following the Application.ShutdownMode solution, I have this in my MainWindow.xaml file:

<Window x:Class="CartToolsPrototype1.Window1" Background="White"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        ResizeMode="CanMinimize"
    Title="{DynamicResource CartTools}" Height="802" Width="950" WindowStartupLocation="CenterScreen" >
    <Window.Resources>

        <Application
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            StartupUri="Window1.xaml"
            ShutdownMode="OnMainWindowClose"
            >
            </Application>

The compiler gives me an error that says, "All objects added to an IDictionary must have a Key attribute or some other type of key associated with them." What exactly am I doing wrong here, and how can I correctly implement my shutdown function?

Thank you.

Community
  • 1
  • 1
Eric after dark
  • 1,768
  • 4
  • 31
  • 79

1 Answers1

2

You don't put Application tag into your Window. In your solution you have app.xaml where you should put your:

ShutdownMode="OnMainWindowClose"

and quoting MSDN:

MainWindow is automatically set with a reference to the first Window object to be instantiated in the AppDomain.

dkozl
  • 32,814
  • 8
  • 87
  • 89
  • OOOOH, okay. Lightbulb... I put it in that file, but my Window1 must not be set as the "main window" because the x button doesn't close out the application when I close the main window. – Eric after dark Jul 15 '13 at 13:46
  • What is your `StartupUri` in `app.xaml`. Is it your `MainWindow`? – dkozl Jul 15 '13 at 13:50
  • StartupUri is "Window1.xaml" – Eric after dark Jul 15 '13 at 13:57
  • 2
    My solution just tells you how to change `ShutdownMode` and deal with compile error you're having but, as @GarryVass suggested, your app should close with default settings any way and I would suggest to do some more debugging to see if there's anything blocking shutdown. – dkozl Jul 15 '13 at 14:08
  • I marked this as my answer, because technically it did solve my compiler error, but I did find an error during debugging that was preventing shutdown. – Eric after dark Jul 15 '13 at 14:21