有没有办法在没有 StartUpUri 的情况下启动 WPF 应用程序而不破坏其他东西?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3439320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to start a WPF application without StartUpUri that doesn't break something else?
提问by Bill Jeeves
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.
我已经尝试了几个小时才能达到可以启动 WPF 应用程序并拥有完全控制权的地步。我希望能够创建一个 ViewModel,创建一个 View(窗口),将 View 的数据上下文设置为 ViewModel,然后显示该视图。
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.
我尝试了很多方法,最有希望的是将 App.xaml 更改为页面,然后添加我自己的 Main 方法。不幸的是,这不能正常工作,因为 VS2010 然后不会在设计器中显示 App.xaml 中的样式,尽管它们在运行应用程序时确实有效。
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?
有没有办法做我想做的事?如果没有,人们通常如何在 WPF 中启动 MVVM 应用程序,在视图本身之外创建一个 ViewModel?
回答by BlackWasp
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.
我会使用启动事件。您可以将其添加到 App.xaml 并删除 StartupUri 行。添加它时,Visual Studio 可以在 App.xaml.cs 文件中为你创建事件。您可以在其中初始化您的 ViewModel 和 View。
回答by digitalMoto
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
这是基本的 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.
Application.MainWindow
也可以使用。第一个显示的 Window 将自动神奇地分配给 MainWindow。当然,您可以跳过创建mainView
和直接写入的步骤,MainWindow
这也会简化语法。
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.
最后一点,我正在做Show
数据绑定之前的工作。您需要这样做以避免MainViewModel
在创建过程中抛出异常的情况。如果MainView
未显示,应用程序将关闭而不让您看到错误。
回答by JanW
in our application, we have choosen the way which you already proposed: writing a new Main method. You also have to make some changes in the project application settings then (no startup object). The app xaml has to look something like this:
在我们的应用程序中,我们选择了您已经建议的方式:编写一个新的 Main 方法。您还必须在项目应用程序设置中进行一些更改(无启动对象)。应用程序 xaml 必须如下所示:
<Application x:Class="EVOCURA.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Startup="Application_Startup"
Exit="Application_Exit">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!--Custom Controls-->
<ResourceDictionary Source="<your resources here>"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
The code behind will look something like this:
后面的代码看起来像这样:
public sealed partial class App : Application
{
static App()
{ }
public App()
{ }
private void Application_Startup(object sender, StartupEventArgs e)
{
// create the main window and assign your datacontext
MainAppWindow main = new MainAppWindow();
main.DataContext = <your datacontext here>
main.Show();
}
[STAThreadAttribute]
public static int Main(string[] args)
{
App app = new App();
app.InitializeComponent();
app.Run();
return 0;
}
}
Have a look at the Startup Event and notice, that no default StartupUri is specified im App.xaml
查看启动事件并注意,在 App.xaml 中没有指定默认的 StartupUri
You could also pass the DataContext in a new constructor of your MainWindow, or create the DataContext directly in xaml.
您还可以在 MainWindow 的新构造函数中传递 DataContext,或直接在 xaml 中创建 DataContext。
回答by Zied
The simplest way to assign an instance of the ViewModel to the DataContext of the view is in the code behind of the Window.
将 ViewModel 的实例分配给视图的 DataContext 的最简单方法是在 Window 后面的代码中。
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new myViewModel();
}
}
For the first part of your question, you can have the controlof your application in the StartUp event
对于问题的第一部分,您可以在 StartUp 事件中控制您的应用程序
<Application x:Class="myApplication.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml" Startup="Application_Startup">
<Application.Resources>
</Application.Resources>
</Application>
Code Behind :
背后的代码:
public partial class App : Application
{
private void Application_Startup(object sender, StartupEventArgs e)
{
// Place your code here
}
}