Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to start a WPF application without StartUpUri that doesn't break something else?

Tags:

I've been trying for hours to get to the point where I can start a WPF application and have full control. I want to be able to create a ViewModel, create a View (Window), set the data context of the View to be the ViewModel, then show the View.

I've tried lots of methods, the most promising being to change the App.xaml to be a page and then adding my own Main method. Unfortunately this doesn't work properly because VS2010 then does not show the styles from the App.xaml in the designer, though they do work when running the app.

Is there a way to do what I want? If not, how do people normally start MVVM apps in WPF, creating a ViewModel outside of the View itself?

like image 776
Bill Jeeves Avatar asked Aug 09 '10 10:08

Bill Jeeves


People also ask

How do I change the startup window in WPF?

If you look at App. xaml class of your WPF application, you will see the following XAML code. Here the StartupUri sets the startup Window of an application. If you want to change the Startup window to some other window, just change this value.

How can I access a control in WPF from another class or window?

If you want to access a control on a wpf form from another assembly you have to use the modifier attribute x:FieldModifier="public" or use the method proposed by Jean. Save this answer.

Is WPF frontend or backend?

WPF uses XAML as its frontend language and C# as its backend languages. WPF was introduced as a part of . NET Framework 3.0 as the Windows library to build Windows client apps and the next generation of Windows Forms. The current version of WPF is 4.5.


2 Answers

I would use the Startup event. You can add this to the App.xaml and remove the StartupUri line. When you add it, Visual Studio can create the event for you within the App.xaml.cs file. You can initialise your ViewModel and View within.

like image 200
BlackWasp Avatar answered Sep 28 '22 08:09

BlackWasp


Here is one simple way...

<Application    x:Class="Demo.Ux.WpfApp.App"   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> </Application> 

Here is the basic App.xaml.cs

public partial class App {   protected override void OnStartup(StartupEventArgs e)   {     try     {       var mainView = new MainView();       mainView.Show();       mainView.DataContext = new MainViewModel();     }     catch (Exception ex)     {       Debug.WriteLine(ex);     }   } } 

Application.MainWindow can be used as well. The first displayed Window will be assigned to MainWindow auto-magically. Of course, you can skip creating your mainView and write directly to MainWindow which would thin out the syntax as well.

 MainWindow = new MainView();  MainWindow.Show();  MainWindow.DataContext = new MainViewModel(); 

One final note, I'm doing the Show before the data bind. You need to do this to avoid a situation where the MainViewModel throw an exception during creation. If the MainView hasn't been shown, the app will close without letting you see the error.

like image 42
visionarycoder Avatar answered Sep 28 '22 10:09

visionarycoder