2013-03-02 91 views
1
<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> 

如何動態地將密鑰和值添加到wpf中的ResourceDictionary中?

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> 

與上面的代碼中,我得到了下面的窗口

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

如果我點擊添加按鈕值應在資源字典其中(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> 

<!-- 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並將名稱空間合併爲一行。

回答

3

你可以用這個簡單的解決方案

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

希望有所幫助做到這一點。

+0

如果我使用這個,這不是在我的資源字典中添加鍵和值。 – 2013-03-05 10:49:25

+0

@ASHOKA你保存了文件嗎? – punker76 2013-03-05 11:05:29

+0

我已經完全試過你的代碼。這是對的嗎 ? – 2013-03-05 13:21:11

相關問題