{X:Static Class.Default} 和 {StaticResource Class} 哪个更高效?
假设我想在 XAML 中引用类。
public class MyConverter
{
public static readonly MyConverter Default = new MyConverter();
...
};
然后在 XAML 中我可以引用它
<Label Content="{Binding Text,Converter={x:Static local:MyConverter.Default}"/>
或者
<local:MyConverter x:Key="MyConverter"/>
...
<Label Content="{Binding Text,Converter={StaticResource local:MyConverter}"/>
哪种方式更有效?
Suppose I have class I want to reference in XAML.
public class MyConverter
{
public static readonly MyConverter Default = new MyConverter();
...
};
And then in XAML I can reference it either
<Label Content="{Binding Text,Converter={x:Static local:MyConverter.Default}"/>
or
<local:MyConverter x:Key="MyConverter"/>
...
<Label Content="{Binding Text,Converter={StaticResource local:MyConverter}"/>
Which way is more efficient?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我怀疑这里的任何内容都会比其他内容更有效,但这里的关键区别是实际发生的情况:
MyConverter
的静态字段MyConverter
的实例并使用它。我相信第一个可能会快几个百分点(或者你所说的高效是什么意思?),但这种差异不会给你带来太多利润。如果您已经有静态字段,我会选择选项#1。据我所知,
x:Static
在 Silverlight 中仍然不可用。I doubt anything here will be more effecient than other but the key difference here is what is actually going on:
MyConverter
MyConverter
and using it.I believe first one might couple percents faster (or what do you mean by efficient?) but this difference won't give you much profit. I would choose option #1 if you already have a static field. Also as far as I remember
x:Static
still is not available in Silverlight.我发现第一个选项很有趣,因为它提供了一种很好且干净的可能性,可以将类与单例结合使用来完成任务的不同变体。想象一个像这样的可见性值转换器:
然后可以轻松地在默认和反向模式下使用它:
I find the first option interesting as it provides a nice and clean possibility to use the class for different variations of a task in combination with a singleton. Imagine a visiblity value converter like this:
This can then easily be used in a default and in a inverse mode: