wpf wpf中如何给ResourceDictionary动态添加key和value?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15176083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:49:17  来源:igfitidea点击:

How to dynamically add key and value to the ResourceDictionary in wpf?

c#wpfresourcedictionary

提问by ASHOK A

<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

英文资源.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

主窗口.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

使用上面的代码,我得到以下窗口

enter image description here

在此处输入图片说明

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);
    }

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.

如果我单击“添加”按钮,该值应该插入到我已经拥有的资源字典(EnglishResources.xaml)中。但它不是插入。请帮帮我。

I need Like

我需要喜欢

<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>

After Added

添加后

<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>

After i added the value into resource dictionary i get result like above. the sysis changed into to sand name space are merged as a line.

在我将值添加到资源字典后,我得到了如上的结果。在SYS变为以小号和命名空间被合并为一条线。

回答by punker76

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);
}

Usage

用法

AddKeyValue("NewKey1", "StringValue");
AddKeyValue("NewKey2", 1000);

Hope that helps.

希望有帮助。