为什么我不能在XAML中定义ResourceDictionary并自己实例化它?

Moj*_*ter 2 .net c# silverlight wpf xaml

好吧,这个问题很难在一行中提出来.这是交易.如果我有这个XAML:

<ResourceDictionary
  x:Class="MyAssembly.MiscResources"    
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <SolidColorBrush x:Key="MyBrush" Color="Purple" />    

</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)

然后我在一些C#中有这个:

var dict = new MiscResources();
Run Code Online (Sandbox Code Playgroud)

dict得到创建并且似乎正常运行,但它有0个元素.并不是说这是某种必要的行为,但我完全不明白为什么它不起作用.我错过了什么?

App*_*ink 6

您缺少对ResourceDictionary的部分类中的Initializecomponent()的调用

namespace YourNameSpace
{
    public partial class someClassName: ResourceDictionary
    {
        public someClassName()
        {
            InitializeComponent(); // you need this for the LoadComponent call on the Baml..
        }
    }
}
Run Code Online (Sandbox Code Playgroud)