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.

4
  • 1
    This most likely isn't a WPF specific problem. My guess would be that you start a thread and it is neither marked as background nor finished when you try to close your application. Commented Jul 15, 2013 at 13:38
  • Can't I just remove the wpf tag instead of starting a totally new thread? Commented Jul 15, 2013 at 13:58
  • Are you sure the problem isn't that my ShutdownMode isn't coded correctly? In my App.xaml file I have ShutdownMode="OneMainWindowClose", but the program continues in the background when I click "x" Commented Jul 15, 2013 at 14:06
  • I found it. Thank you. Commented Jul 15, 2013 at 14:19

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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.
What is your StartupUri in app.xaml. Is it your MainWindow?
StartupUri is "Window1.xaml"
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.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.