.net 资源字典&x2B;纯System.XAML的StaticResource替代方案,无PresentationFramework

.net 资源字典&x2B;纯System.XAML的StaticResource替代方案,无PresentationFramework,.net,.net,我想享受ResourceDictionary+StaticResource功能,但不需要加载PresentationFramework-pure System.Xaml 我很容易地将Resources属性定义为dictionary,但我需要一种方法来引用那里的项,即我需要类似于StaticResource的东西。我用Reflector查看它,它似乎依赖于WindowsBase和PresentationFramework中的一个东西 我想知道是否有另一种方法,而不是滑入PresentationFr

我想享受
ResourceDictionary
+
StaticResource
功能,但不需要加载PresentationFramework-pure System.Xaml

我很容易地将Resources属性定义为dictionary,但我需要一种方法来引用那里的项,即我需要类似于
StaticResource
的东西。我用Reflector查看它,它似乎依赖于WindowsBase和PresentationFramework中的一个东西

我想知道是否有另一种方法,而不是滑入PresentationFramework。我也明白,我可以复制粘贴PresentationFramework中的所有相关代码,只留下最低限度的代码来支持所需的功能——这是我最后的选择

以下是我到目前为止所拥有的:

using System.Collections.Generic;
using System.Windows.Markup;
using System.Xaml;

namespace ConsoleApplication1
{
  [ContentProperty("Items")]
  public class XamlDictionary<TKey, TValue> : Dictionary<TKey, TValue>
  {
    public ICollection<KeyValuePair<TKey, TValue>> Items
    {
      get { return this; }
    }
  }

  public class A
  {
    private XamlDictionary<string, int> m_resources;
    public XamlDictionary<string, int> Resources 
    {
      get
      {
        if (m_resources == null)
        {
          m_resources = new XamlDictionary<string, int>();
        }
        return m_resources;
      }
      set { m_resources = value; }
    }
    public int Value { get; set; }
  }

  class Program
  {
    static void Main(string[] args)
    {
      var a = (A)XamlServices.Load("A.xaml");
    }
  }
}

<?xml version="1.0" encoding="utf-8" ?>
<l:A xmlns:scg="clr-namespace:System.Collections.Generic;assembly=mscorlib"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:sys="clr-namespace:System;assembly=mscorlib"
     xmlns:l="clr-namespace:ConsoleApplication1;assembly=ConsoleApplication1"
     Value="15">
  <l:A.Resources>
    <sys:Int32 x:Key="ssss">10</sys:Int32>
  </l:A.Resources>
</l:A>
使用System.Collections.Generic;
使用System.Windows.Markup;
使用System.Xaml;
命名空间控制台应用程序1
{
[内容属性(“项目”)]
公共类XamlDictionary:字典
{
公共i收集项目
{
获取{返回此;}
}
}
公共A类
{
私有XAMLM_资源;
公共XamlDictionary资源
{
得到
{
if(m_resources==null)
{
m_resources=新的XamlDictionary();
}
返回m_资源;
}
设置{m_resources=value;}
}
公共int值{get;set;}
}
班级计划
{
静态void Main(字符串[]参数)
{
var a=(a)XamlServices.Load(“a.xaml”);
}
}
}
10
到目前为止一切都很好。现在我希望更改Xaml,以便
A.Value
引用
A.Resources[“ssss”]
值,这里是我需要
StaticResource
替代项的地方。

您可能需要创建一个“staticresourceExtension”作为标记的一部分=)