【问题标题】:Resource Dictionary Not Found When App is Published发布应用时找不到资源字典
【发布时间】:2015-09-13 03:13:41
【问题描述】:

我编写了一个 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 存在于已发布的应用程序中?如果没有,有没有办法在不显式加载文件的情况下引用这些资源字典?

【问题讨论】:

    标签: c# wpf xaml visual-studio-2013 resourcedictionary


    【解决方案1】:

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

    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。这样,您无需将构建操作更改为 Content,也无需部署任何内容。

    【讨论】:

    • 谢谢!我使用了你的代码并且它有效。对于访问此页面的其他人,我使用 ClickOnce 发布。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-17
    • 2013-01-01
    • 2017-08-21
    相关资源
    最近更新 更多