【问题标题】:System.Windows.Markup.XamlParseException on a Multi Project Solution (Mixed platforms)多项目解决方案(混合平台)上的 System.Windows.Markup.XamlParseException
【发布时间】:2018-11-21 09:40:14
【问题描述】:

我已将我的 WPF 项目集成到一个混合解决方案中。该解决方案有 3 个用 WinForms (.cs) 编写的项目,1 个用 WinForms (.vb) 编写的项目,还有一个用 WPF 编写的项目(使用 MahApps)。 我的项目/窗口的所有者必须是用 WinForm 编写的三个之一。

我打过这样的电话:

using WPFSolution;
using System.Windows.Forms.Integration;

private void buttonOpenWPFUi_Click(object sender, EventArgs e)
    {
        var pm = new ProductsMenu();
        ElementHost.EnableModelessKeyboardInterop(pm);
        pm.Show();
    }

ProductsMenu是我在App.xaml上的起点

但是当我到达ProductsMenu.xaml.csInitializeComponents() 时,System.Windows.Markup.XamlParseException 被抛出

错误是:IOException: Cannot locate resource 'resources/icons.xaml'。

我的 App.xaml 文件:

<Application x:Class="WPFSolution.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         StartupUri="ProductsMenu.xaml" ShutdownMode="OnMainWindowClose">
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <!-- MahApps.Metro resource dictionaries. Make sure that all file names are Case Sensitive! -->
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.AnimatedTabControl.xaml" />
            <!-- Accent and AppTheme setting -->
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Red.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
            <!-- BaseDark.xaml | BaseLight.xaml -->
            <!-- Icons -->
            <ResourceDictionary Source="pack://application:,,,/WPFSolution;component/Resources/Icons.xaml"></ResourceDictionary>

            <!-- Languages -->
            <!--<ResourceDictionary Source="Resources/StringResources.en.xaml"></ResourceDictionary>-->
            <ResourceDictionary Source="Resources/StringResources.it.xaml"></ResourceDictionary>

        </ResourceDictionary.MergedDictionaries>

        <Style TargetType="{x:Type ToolTip}">
            <Setter Property="Background" Value="White"/>
            <Setter Property="BorderBrush" Value="Red"/>
            <Setter Property="BorderThickness" Value="2"/>
            <Setter Property="FontSize" Value="18"/>
            <Setter Property="FontWeight" Value="Bold"/>
            <Setter Property="Padding" Value="7,4,7,4"/>
        </Style>


        <!--IMMAGINI -->
        <!--nodi-->
        <!--<BitmapImage x:Key="Nodo1" UriSource="Resources/img/TileImages/nodi/nodo1.jpg"></BitmapImage>
        <BitmapImage x:Key="Nodo2" UriSource="Resources/img/TileImages/nodi/nodo2.jpg"></BitmapImage>
        <BitmapImage x:Key="Nodo3" UriSource="Resources/img/TileImages/nodi/nodo3.jpg"></BitmapImage>
        <BitmapImage x:Key="Nodo4" UriSource="Resources/img/TileImages/nodi/nodo4.jpg"></BitmapImage>-->


    </ResourceDictionary>
</Application.Resources>

我尝试将 Icons.xaml/Build Action 设置为资源和页面。 我错过了什么?

【问题讨论】:

  • WPFSolution 是包含 Icons.xaml 的程序集的名称吗?
  • 是的,它是包含文件的程序集。如果我将 WPF 解决方案作为独立模块运行,一切正常。
  • 应用程序项目是否引用了该程序集?
  • 是的,我在下面添加了引用:解决方案资源管理器 > MainProject > 引用 > 右键单击​​ > 添加引用... > 项目 > WPFSolution... 然后我检查了整个解决方案是否具有相同的.NET 框架版本 (4.6.1)

标签: c# wpf winforms mahapps.metro


【解决方案1】:

知道了(或至少是一种解决方法)。

  1. 我已取消引用 WPFSolution 项目。
  2. 编译 WPF 解决方案 作为 .dll (我已将所有合并的字典复制到每个 window.xaml,删除 app.xaml 文件并更改应用程序 键入到类库)
  3. 复制了 bin/release 中的 WPFSolution.dll - 主项目的 bin/debug
  4. 引用了 .dll

一切都像魅力一样运作

【讨论】:

    【解决方案2】:

    试试这个语法:

    <ResourceDictionary Source="/WPFSolution;component/Resources/Icons.xaml" />
    

    而且,在这种情况下,“页面”是正确的构建选项

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-05
      • 2018-05-07
      • 1970-01-01
      • 1970-01-01
      • 2020-05-06
      相关资源
      最近更新 更多