Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The application I am writing has a configuration step in its startup where I collect required configs and user inputs before I init my Autofac DI. Then I close said window by invoking the standard Window.Close() call.

The code seems to execute correctly but the next window does not want to show. It seems like the Window.Close() results in an event which overrides anything after it has been called.

Here is some more code to shed some light on what I am doing:

C#
var connectionview = new ConnectionView(connectionViewModel);
var result = connectionview.ShowDialog();
if (result.messageResult == MessageDialogResult.No)
               return;

var bootstrapper = new Bootstrapper(tempConnectionContext);
Container = bootstrapper.Bootstrap();
_mainViewModel = Container.Resolve<MainWindowViewModel>();
MainWindow = new MainWindow(_mainViewModel);
MainWindow.Show();


If anyone could point me into a direction too it would help. Any help would be wonderful.
Thanks

What I have tried:

I don't think I am searching in the right places.
Posted
Updated 27-Nov-17 14:24pm

For MVVM, you need to use a proxy service as VMs are not supposed to access any UI, including windows. I have answer this in a similar question.

Please check out my answer there rather than me reposting it here: How to change listbox item size dynamically in C# WPF?[^]

UPDATE:

This article will be of great help to you: Asynchronous Programming - Async from the Start[^]
 
Share this answer
 
v2
Comments
Ocqlarity 28-Nov-17 3:31am    
Thanks, works like a charm and it provides some cool additions.
Graeme_Grant 28-Nov-17 4:59am    
Glad to hear! :)

Also, check out my WPF Toggle switch article[^]... a cool addition to any WPF app!
In both Windows Forms and WPF, there is a hierarchy to windows. Your startup window is the top of the hierarchy tree and any other windows that show up are children, with those children able to show their own child windows.

Once you close the top window of the tree, your application dies with it. So, the solution is rather simple. You set the wrong window as your startup window. The startup windows should ALWAYS be the main form of your application, not a login form, not a settings dialog, ...
 
Share this answer
 
Comments
Graeme_Grant 27-Nov-17 20:25pm    
But that would mean that he would have to break the MVVM rules to directly access the Visual Tree from the VM. You don't need to do that. See my answer and link below for the correct way.
PureNsanity 30-Nov-17 3:36am    
The OP said:

Quote:OP said:
The application I am writing has a configuration step in its startup

I would not consider any startup or Bootstrapper code to be VM.

There's also a lot easier ways to create glue from control to control, and between VM and control than proxies... While a proxy can be a good way to solve it in some instances, DependencyProperties, Converters, and Commands are made for that without having to manage an additional proxy class.
Ocqlarity 5-Mar-18 6:13am    
Hi Dave

Thank you for the contribution. Although your response is correct, Graeme is right in saying that I do not want to break the MVVM rules and therefore the answer you have provided does not work for my situation. I have used your recommendation in a different project though. So thanks again.
I think your application is closed after the Close of the mainwindow. So either don't make the first window the MainWindow (just a Dialog, like a splashscreen) or use ShutdownMode="OnExplicitShutdown"
for you application
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900