【发布时间】: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