静态梅索德/函数Func键&LT的结合; T&GT;在XAML属性 [英] Binding of static methode/function to Func<T> property in XAML
问题描述
我正在使用XAML创建一个对象树的一个节点上看起来是这样的:
I'm working on using XAML to create an object tree and one of the nodes looks like this:
public class ExecuteMethod : INode
{
#region Implementation of INode
public bool Evaluate()
{
return Function != null && Function();
}
public string Name { get; set; }
private string _type;
public string Type
{
get
{
if (string.IsNullOrEmpty(_type))
{
_type = GetType().Name;
}
return _type;
}
}
#endregion
public Func<bool> Function { get; set; }
}
我的目标是必须要做出的背后尽可能干净的XAML和code这是不是这样的,现在在那里我创建的包装性能为每个函数:
My goal is essential to make the XAML and code behind as clean as possible which isn't the case right now where I'm creating wrapper properties for every function:
public static Func<bool> Func1 { get { return Method1; } }
public static bool Method1()
{
//Do stuff here
return true;
}
和XAML看起来像这样对于上述code:
and the xaml looks like this for the above code:
<Root
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns="clr-namespace:XamlBT;assembly=XamlBT"
xmlns:d="clr-namespace:TestBT;assembly=TestBT">
<Root.Child>
<Sequence Name="sequence1" >
<ExecuteMethod Name="e1.1" Function="{x:Static d:Program.Func1}" />
<Selector Name="selector1" >
<ExecuteMethod Name="e2.1" Function="{x:Static d:Program.Func1}" />
</Selector>
</Sequence>
</Root.Child>
我想知道是否有绑定的方法/函数的函数功能属性快速简便的方法,我在这里所说的方法,而不是执行的方法/函数的值。 (我能想到的valueConverter或ExecuteMethod节点/类中使用一些魔法反射,但只是觉得脏,怪异)
我想如何在XAML一个例子来看看:
I would like to know if there's a quick and easy way to bind methods/functions to the Func property, I'm talking about the method here NOT the value of the executed method/function. (I can think of using some reflection magic in a valueConverter or inside the ExecuteMethod node/class but that just feels dirty and weird) An example of how I'd like the XAML to look:
<Root
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns="clr-namespace:XamlBT;assembly=XamlBT"
xmlns:d="clr-namespace:TestBT;assembly=TestBT">
<Root.Child>
<Sequence Name="sequence1" >
<ExecuteMethod Name="e1.1" Function="{x:Static d:Program.Method1}" />
<Selector Name="selector1" >
<ExecuteMethod Name="e2.1" Function="{x:Static d:Program.Method1}" />
</Selector>
</Sequence>
</Root.Child>
感谢提前对不起坏英语语法的任何帮助,这不是我的母语:)
Thanks for any help in advance and sorry for the bad English grammar, it's not my native language :)
推荐答案
我能想到的几种方法,使它看起来更清洁,但没有适合你所要求的具有约束力的语法。我猜你会是什么最高兴将编写自己的标记扩展所以你可以使它看起来像 {D:STATICMETHOD Program.Method1}
,但你绝对必须使用反射,但将是微不足道缓存,并会看起来比值转换器更好。
I can think of couple ways to make it look cleaner but there isn't a binding syntax for what you are asking. I'm guessing what you would be most happy with would be writing your own markup extension so you could make it look like {d:StaticMethod Program.Method1}
, but you would definitely have to use reflection, but it would be trivial to cache and would look better than a value converter.
这篇关于静态梅索德/函数Func键&LT的结合; T&GT;在XAML属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!