发布应用时未找到资源字典

Resource Dictionary Not Found When App is Published

我已经编写了一个 WPF/C# 应用程序,我正在尝试发布。该应用程序当前使用两个资源字典在主题之间切换。这是 App.xaml 文件,它指定了两个资源字典:

<Application x:Class="TopDeck.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
         xmlns:visualizationToolkit="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit"
         xmlns:chartingprimitives="clr-namespace:System.Windows.Controls.DataVisualization.Charting.Primitives;assembly=System.Windows.Controls.DataVisualization.Toolkit"
         StartupUri="MainWindow.xaml">

    <Application.Resources>
        <ResourceDictionary>
            <SolidColorBrush Color="#d0157820" x:Key="muddyBrush"/>
            <Style TargetType="TextBlock" x:Key="MenuFont">
                <Setter Property="Foreground" Value="Black"/>
            </Style>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="DarkTheme.xaml"/>
                <ResourceDictionary Source="LightTheme.xaml" />
            </ResourceDictionary.MergedDictionaries>

        </ResourceDictionary>
    </Application.Resources>


</Application>

我有两个文件,DarkTheme.xaml 和 LightTheme.xaml,它们是资源字典。我没有包括它们,因为我认为它们的内容与这里无关。

在我的MainWindow.xaml.cs中,我有一个在两个主题之间切换的方法。当我 运行 我的代码在 Visual Studio 2013:

时,此方法有效
private void ChangeTheme(int themeChoice)
{
    ResourceDictionary dict;
    var assembly = Assembly.GetEntryAssembly() ?? Assembly.GetCallingAssembly();
    var path = System.IO.Path.GetDirectoryName(assembly.Location);
    path = path.Replace("bin\Debug", "");
    path = path.Replace("bin\Release", "");

    if (themeChoice == 0)
    {
        dict = (ResourceDictionary)XamlReader.Load(new FileStream(System.IO.Path.Combine(path, "LightTheme.xaml"), FileMode.Open));
            theme = "LightTheme.xaml";
    }
    else
    {
        dict = (ResourceDictionary)XamlReader.Load(new FileStream(System.IO.Path.Combine(path, "DarkTheme.xaml"), FileMode.Open));
        theme = "DarkTheme.xaml";
    }

    Application.Current.Resources.MergedDictionaries.Clear();
    Application.Current.Resources.MergedDictionaries.Add(dict);

}

问题是,当我发布此应用程序并尝试 运行 它时,我收到此错误消息:

{"Could not find file 'C:\Users\Emerald\AppData\Local\Apps\2.0\JTHXX0YW.KTK\1MZ2M8ZP.TV0\topd..tion_1428230028859fe5_0001.0000_ec5ac614e9be24b1\DarkTheme.xaml'.":"C:\Users\Emerald\AppData\Local\Apps\2.0\JTHXX0YW.KTK\1MZ2M8ZP.TV0\topd..tion_1428230028859fe5_0001.0000_ec5ac614e9be24b1\DarkTheme.xaml"}

有谁知道我可以做些什么来维护我的应用程序中的资源字典切换,但又避免了这个错误?有没有办法指定我希望 DarkTheme.xaml 和 LightTheme.xaml 存在于已发布的应用程序中?如果没有,有没有办法在不显式加载文件的情况下引用这些资源字典?

我想您所说的发布是指通过 ClickOnce 发布。因此,要包含您的 DarkTheme.xaml 和 LightTheme.xaml,请转到它们的属性并将构建操作设置为 "Content"。然后您可以检查(在项目属性 > 发布 > 应用程序文件中确实包含它们)。但是,还请考虑以下方式来实现您的目标:

var themeDict = new ResourceDictionary();
var uriPath = string.Format("/{0};component/DarkTheme.xaml", 
Assembly.GetEntryAssembly().GetName().Name);
var uri = new Uri(uriPath, UriKind.RelativeOrAbsolute);
themeDict.Source = uri;        
Application.Current.Resources.MergedDictionaries.Add(themeDict);

您在这里所做的是为您的(已经存在的)DarkTheme.xaml 文件构造 url,然后告诉 ResourceDictionary url。有了这个,您不需要将构建操作更改为内容,也不需要部署任何东西。