【问题标题】:How to dynamically add ResourceDictionary to MergedDictionaries in WPF app如何在 WPF 应用程序中将 ResourceDictionary 动态添加到 MergedDictionaries
【发布时间】:2015-06-30 12:04:20
【问题描述】:

我有下一段 WPF 代码:

<Page.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Languages/English.xaml"/>
            <ResourceDictionary Source="Languages/Romana.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Page.Resources>

如何从这些 ResourceDictionarys 的代码之一中进行选择?

编辑:

<Page.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Languages/English.xaml"/>
            <ResourceDictionary Source="Languages/Romana.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Page.Resources>

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <ScrollViewer HorizontalScrollMode="Auto" HorizontalScrollBarVisibility="Hidden" VerticalScrollMode="Disabled" VerticalScrollBarVisibility="Hidden"  BorderThickness="0,3,0,3" BorderBrush="Aqua">
        <StackPanel Orientation="Horizontal">
            <AppBarToggleButton x:Name="Connect_toggle" Label="{StaticResource connect}" HorizontalAlignment="Stretch" Icon="Accept" VerticalAlignment="Stretch" d:LayoutOverrides="Width" Click="Connect_toggle_Click"/>
            <AppBarToggleButton x:Name="Options_toggle" Label="{StaticResource options}" HorizontalAlignment="Stretch" Icon="Accept" VerticalAlignment="Stretch" d:LayoutOverrides="Width" Click="Options_toggle_Click"/>
        </StackPanel>
    </ScrollViewer>

我没有指定我使用的是 Windows Universal (VS2015)。

【问题讨论】:

  • “选择代码”是什么意思?它们都合并在一起了 - 如果您的意思是这样,您可以按键选择单个资源?
  • 我只想使用其中一个。例如:我有一个组合框,如果我选择“英语”,我的应用程序将加载 English.xaml。选择 Romana 将加载 Romana.xaml。实际上,那些 xaml 文件定义了我的应用程序语言
  • @CharlesMager,从代码中选择或者我们通常说的in code behind,所以问题是how to merge ResourceDictionary in code behind
  • @Sinatr:看来 OP 实际上是在询问如何在各种 ResourceDictionary 之间动态切换。最好的问候,

标签: c# wpf resources resourcedictionary win-universal-app


【解决方案1】:

您可以动态选择ResourceDictionary 文件并使用C# 代码隐藏将其添加到MergedDictionaries,如下面的代码sn-p 所示:

    // prefix to the relative Uri for resource (xaml file)
    string _prefix = String.Concat(typeof(App).Namespace, ";component/");

    // clear all ResourceDictionaries
    this.Resources.MergedDictionaries.Clear();

    // add ResourceDictionary
    this.Resources.MergedDictionaries.Add
    (
        new ResourceDictionary { Source = new Uri(String.Concat(_prefix + "Languages/English.xaml", UriKind.Relative) }
    );

其中“Languages/English.xaml”是与您的示例相关的选定ResourceDictionary 文件的示例相对路径。

希望这会有所帮助。

【讨论】:

  • 它不起作用。在调试模式下,它返回给我这个错误:Internal.Uri.dll 中发生了“System.UriFormatException”类型的异常,但未在用户代码中处理
  • 查看扩展答案。最好的问候,
  • 请发布与案例相关的整个代码块并突出显示导致错误的行。问候,
  • 已编辑!请立即检查
  • C# 代码与该 ComboBox 事件处理程序相关的位置在哪里(引用:“例如:我有一个组合框,如果我选择“英语”,...”)以及该代码中的行是什么块导致错误?问候,
猜你喜欢
  • 2012-12-07
  • 1970-01-01
  • 1970-01-01
  • 2011-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多