简体   繁体   English

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

[英]Why can't I define a ResourceDictionary in XAML and instantiate it by itself?

Ok, that question was really hard to ask in one line. 好的,这个问题真的很难一一提出。 Here's the deal. 这是交易。 If I have this 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>

Then I have this in some C#: 然后我在一些C#中有这个:

var dict = new MiscResources();

dict gets created and seems to function normally, but it has 0 elements. dict被创建并似乎正常运行,但是它有0个元素。 Not that this is some kind of neccessary behavior, but I totally don't understand why it doesn't work. 并不是说这是某种必要的行为,但是我完全不理解为什么它不起作用。 What piece of this am I missing? 我错过了哪一部分?

you are missing the call to Initializecomponent() in your partial Class for ResourceDictionary 您缺少对ResourceDictionary的部分类中对Initializecomponent()的调用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM