简体   繁体   English

如何使用 MVVM 从另一个 WPF 打开 WPF 应用程序?

[英]How to open WPF application from an other WPF both using MVVM?

I am trying to us the demo code from wpf chrometabs and in the other application I just added a button that calls the constructor of the demo:我正在尝试向我们展示来自wpf chrometabs的演示代码,而在另一个应用程序中,我只是添加了一个调用演示构造函数的按钮:

private void FrontendDebug_Click(object sender, RoutedEventArgs e)
{
   Demo.MainWindow mainWindow = new Demo.MainWindow();
   mainWindow.Show();
}

The problem is that it throws an exception at the InitalizeComponent :问题是它在InitalizeComponent抛出异常:

例外

I read somewhere that in MVVM, the DataContext should be set before calling InitializeComponent() but I don't know if that is the problem here or how to do it if it is.我在某处读到,在 MVVM 中,应该在调用InitializeComponent()之前设置DataContext ,但我不知道这是否是这里的问题或者如果是的话如何解决。

The error that you get points out, that you use a StaticResource markup extension to reference a resource with key Locator that is not found in this line:您得到的错误指出,您使用StaticResource标记扩展来引用具有此行中未找到的键Locator的资源:

DataContext="{Binding Source={StaticResource Locator},Path=ViewModelMainWindow}"

The locator is defined in the application resources, so it is accessible in the whole application.定位器在应用程序资源中定义,因此在整个应用程序中都可以访问。 Either you accidentially removed it or there is a typo in App.xaml .要么你不小心删除了它,要么App.xaml中有错字。 Make sure that it looks like this.确保它看起来像这样。

<Application x:Class="Demo.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="MainWindow.xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" d1p1:Ignorable="d" xmlns:d1p1="http://schemas.openxmlformats.org/markup-compatibility/2006">
    <Application.Resources>
        <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" xmlns:vm="clr-namespace:Demo.ViewModel" />
    </Application.Resources>
</Application>

I am trying to us the demo code from wpf chrometabs and in the other application I just added a button that calls the constructor of the demo:我正在尝试向我们展示来自 wpf chrometabs 的演示代码,而在另一个应用程序中,我只是添加了一个调用演示构造函数的按钮:

If you have another application that uses the demo project as library, the call below will only create an instance of the MainWindow .如果您有另一个使用演示项目作为库的应用程序,则下面的调用只会创建MainWindow的一个实例。 It will not bootstrap the application in the demo project.不会引导演示项目中的应用程序。

Demo.MainWindow mainWindow = new Demo.MainWindow();

Consequently, the App object from the demo project is never created and its resources are not available.因此,演示项目中的App object 从未创建,其资源不可用。 Even if it would be bootstrapped, the resources defined in App.xaml only apply to this application, nothing else.即使它会被引导, App.xaml中定义的资源也只适用于这个应用程序,没有别的。

Therefore, the mainWindow instance will try to find the resource in your application, not in the demo application.因此, mainWindow实例将尝试在您的应用程序中查找资源,而不是在演示应用程序中。 Since it is not defined there, you will get an exception.因为它没有在那里定义,你会得到一个例外。 You could add the view model locator to your application's App.xaml resources, but keep in mind that is applies to all resources defined on the application level.您可以将视图 model 定位器添加到应用程序的App.xaml资源中,但请记住,这适用于在应用程序级别定义的所有资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM