简体   繁体   English

尝试从Winforms打开WPF窗口时出现XamlParseException

[英]XamlParseException when attempting to open a WPF Window from Winforms

I have a single Winforms project and multiple WPF projects in one solution. 在一个解决方案中,我有一个Winforms项目和多个WPF项目。 From the Winform application I'd like to open one of the WPF Windows (it's a MetroWindow from Mahapps, if it matters). 我想从Winform应用程序中打开一个WPF Windows(如果重要的话,它是Mahapps的MetroWindow)。

After looking at the accepted answer to this stackoverflow question I ended up with this piece of code: 在查看了此stackoverflow问题的可接受答案之后,我得到了以下代码:

OpenWPFAppButton_Click(object sender, EventArgs e)
{
    WPFApp wpfApp = new WPFApp();
    ElementHost.EnableModelessKeyboardInterop(wpfApp);
    wpfApp.Show();
}

Unfortunately if I click the button a XamlParseException occurs, which points to the first Style="{StaticResource ... }" line in WPFApp.xaml (the Main Xaml File). 不幸的是,如果我单击按钮,则会发生XamlParseException,它指向WPFApp.xaml(主Xaml文件)中的第一行Style="{StaticResource ... }"行。

在此处输入图片说明

Does this mean I cannot open WPF Windows from Winforms that include static resources? 这是否意味着我无法从包含静态资源的Winforms中打开WPF Windows? Or am I missing something simple here? 还是我在这里错过了一些简单的事情?

EDIT : Here is the content of the App.xaml file: 编辑 :这是App.xaml文件的内容:

<Application x:Class="WPFAppProjectName.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:system="clr-namespace:System;assembly=mscorlib"
         StartupUri="WPFApp.xaml">
<Application.Resources>
    <ResourceDictionary>

        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/Selen.Wpf.SystemStyles;component/ButtonStyles.xaml"/>
            <ResourceDictionary Source="pack://application:,,,/Selen.Wpf.SystemStyles;component/MenuStyles.xaml"/>
            <ResourceDictionary Source="pack://application:,,,/Selen.Wpf.SystemStyles;component/TextBoxStyles.xaml"/>
        </ResourceDictionary.MergedDictionaries>

    </ResourceDictionary>
</Application.Resources>

It is most likely that some common resources are defined in the App.xaml . 最有可能在App.xaml中定义了一些公共资源。 That file isn't loaded when running through Windows Forms and thus those resources are unavailable. 通过Windows Forms运行时不会加载该文件,因此这些资源不可用。 Hence you get this error. 因此,您会收到此错误。

You could include the resource definitions in the Window.xaml files, or an own common style resource file (aka Resource Dictionary) which is included in every Window. 您可以在Window.xaml文件中包含资源定义,也可以在每个Window中包含一个自己的通用样式资源文件(即资源字典)。

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

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