[英]How to dynamically add key and value to the ResourceDictionary in wpf?
[英]How to dynamically add ResourceDictionary to MergedDictionaries in WPF app
我有代码的下一个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)。
您可以动态选择ResourceDictionary
文件, MergedDictionaries
使用C#代码隐藏将其添加到MergedDictionaries
中,如以下代码片段所示:
// 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
文件的示例相对路径。
希望这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.