ResourceDictionary + StaticResource alternative for pure System.XAML, no PresentationFramework
I would like to enjoy the ResourceDictionary
+ StaticResource
functionality, but without the need to load PresentationFramework - pure System.Xaml.
I managed, quite easily, to define the Resources property as a dictionary, but I need a way to reference the items there, 开发者_开发百科i.e. I need something like StaticResource
. I looked at it with Reflector and it seems to depend on a stuff in WindowsBase and PresentationFramework.
I was wondering if there is another way, without gliding into PresentationFramework. I also understand, that I can to copy-paste all the relevant code from PresentationFramework leaving only the bare minimum to support the needed functionality - this is my last resort.
Here is what I have until now:
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>
So far so good, everything is fine. Now I wish to change the Xaml so that A.Value
reference the A.Resources["ssss"]
value and here is where I need the StaticResource
alternative.
You may need to create a "StaticResourceExtention" to be part of the markup =)
精彩评论