如何在代码隐藏中创建ResourceDictionary?

Cha*_*u A 3 c# wpf xaml .net-4.0 resourcedictionary

<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="one">ONE</sys:String>
    <sys:String x:Key="two">TWO</sys:String>
    <sys:String x:Key="three">THREE</sys:String>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)

现在想要使用 C# 在 WPF 中通过代码隐藏动态创建与上面相同的资源 ResourceDictionary 。可以这样创建吗?

yo *_*han 5

public MainWindow()
    {
        InitializeComponent();
        DataContext = new MainViewModel();
        ResourceDictionary rd = new ResourceDictionary();
        rd.Add("one", "ONE");
        rd.Add("two", "TWO");
        rd.Add("three", "THREE");
        this.Resources.MergedDictionaries.Add(rd);
    }
Run Code Online (Sandbox Code Playgroud)

我希望这个能帮上忙。