wpf中如何给ResourceDictionary动态添加key和value? [英] How to dynamically add key and value to the ResourceDictionary in wpf?
本文介绍了wpf中如何给ResourceDictionary动态添加key和value?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<Application
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="MultiLanguage.App"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary x:Name="LanguageDictionary" Source="/LanguageResources;component/EnglishResources.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
EnglishResources.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<sys:String x:Key="add">ADD</sys:String>
<sys:String x:Key="key">Key</sys:String>
<sys:String x:Key="stringValue">String Value</sys:String>
MainWindow.xaml
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="MultiLanguage.MainWindow"
x:Name="Window"
Title="MainWindow"
Width="640" Height="480">
<Grid x:Name="LayoutRoot" HorizontalAlignment="Center" VerticalAlignment="Center">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Button x:Name="btnAdd" Content="{DynamicResource add}" Height="48" Grid.Column="0" Grid.Row="3" Grid.ColumnSpan="2" Margin="10" Click="btnAdd_Click" />
<TextBlock Text="{DynamicResource key}" Height="40" Width="100" Grid.Column="0" Grid.Row="0" Margin="10"/>
<TextBlock Text="{DynamicResource stringValue}" Height="40" Width="100" Grid.Column="0" Grid.Row="1" Margin="10"/>
<TextBox x:Name="txtKey" Height="40" Width="200" Grid.Column="1" Grid.Row="0" Margin="10"/>
<TextBox x:Name="txtStringValue" Height="40" Width="200" Grid.Column="1" Grid.Row="1" Margin="10"/>
</Grid>
使用上面的代码,我得到以下窗口
with above code, i get the following window
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
AddKeyValue(txtKey.Text, txtStringValue.Text);
}
private void AddKeyValue(object key, object value)
{
// load the resource dictionary
var rd = new System.Windows.ResourceDictionary();
rd.Source = new System.Uri("pack://application:,,,/LanguageResources;component/EnglishResources.xaml", System.UriKind.RelativeOrAbsolute);
// add the new key with value
rd.Add(key, value);
// now you can save the changed resource dictionary
var settings = new System.Xml.XmlWriterSettings();
settings.Indent = true;
var writer = System.Xml.XmlWriter.Create(@"EnglishResources.xaml", settings);
System.Windows.Markup.XamlWriter.Save(rd, writer);
}
如果我单击添加"按钮,该值应该插入到我已经拥有的资源字典(EnglishResources.xaml)中.但它不是插入.请帮帮我.
If i click Add button the value should be inserted in the resource dictionary which(EnglishResources.xaml) is already i have. But it is not insert. Please help me out.
我需要赞
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<sys:String x:Key="add">ADD</sys:String>
<sys:String x:Key="key">Key</sys:String>
<sys:String x:Key="stringValue">String Value</sys:String>
<!-- the value should be inserted here. But not Insert-->
</ResourceDictionary>
添加后
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib">
<s:String x:Key="add">ADD</sys:String>
<s:String x:Key="key">Key</sys:String>
<s:String x:Key="stringValue">String Value</sys:String>
<!-- the value should be inserted here. But not Insert-->
</ResourceDictionary>
将值添加到资源字典后,我得到了如上的结果.sys 改为 s ,命名空间合并为一行.
After i added the value into resource dictionary i get result like above. the sys is changed into to s and name space are merged as a line.
推荐答案
你可以用这个简单的解决方案
you can do it with this simple solution
private void AddKeyValue(object key, object value) {
// load the resource dictionary
var rd = new System.Windows.ResourceDictionary();
rd.Source = new System.Uri("pack://application:,,,/YOURAssemblyName;component/EnglishResources.xaml", System.UriKind.RelativeOrAbsolute);
// add the new key with value
//rd.Add(key, value);
if (rd.Contains(key)) {
rd[key] = value;
} else {
rd.Add(key, value);
}
// now you can save the changed resource dictionary
var settings = new System.Xml.XmlWriterSettings();
settings.Indent = true;
var writer = System.Xml.XmlWriter.Create(@"EnglishResources.xaml", settings);
System.Windows.Markup.XamlWriter.Save(rd, writer);
}
用法
AddKeyValue("NewKey1", "StringValue");
AddKeyValue("NewKey2", 1000);
希望有所帮助.
这篇关于wpf中如何给ResourceDictionary动态添加key和value?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文