【问题标题】:Why can't I define a ResourceDictionary in XAML and instantiate it by itself?为什么我不能在 XAML 中定义 ResourceDictionary 并自行实例化它?
【发布时间】:2009-02-17 17:09:30
【问题描述】:

好的,这个问题真的很难用一行来问。这是交易。如果我有这个 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>

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

var dict = new MiscResources();

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

【问题讨论】:

    标签: c# .net wpf silverlight xaml


    【解决方案1】:

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

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

    【讨论】:

    • 啊,你是对的。我没有意识到这是将所有部件粘合在一起的重要粘合剂。我觉得它更神奇一点。谢谢你,AppleDrink。我以为没有人会想出一个答案。你是我的英雄。
    猜你喜欢
    • 1970-01-01
    • 2012-10-08
    • 1970-01-01
    • 2013-11-02
    • 1970-01-01
    • 1970-01-01
    • 2016-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多