{X:Static Class.Default} 和 {StaticResource Class} 哪个更高效?

发布于 2024-10-27 05:11:21 字数 477 浏览 0 评论 0原文

假设我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

榕城若虚 2024-11-03 05:11:21

我怀疑这里的任何内容都会比其他内容更有效,但这里的关键区别是实际发生的情况:

  1. 在第一种方法中,您引用类 MyConverter 的静态字段
  2. 在第二种情况下,您正在创建一个 < 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:

  1. In first approach you're referencing static field of class MyConverter
  2. In second case you're creating an instance of 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.

萌能量女王 2024-11-03 05:11:21

我发现第一个选项很有趣,因为它提供了一种很好且干净的可能性,可以将类与单例结合使用来完成任务的不同变体。想象一个像这样的可见性值转换器:

public class VisibilityConverter : IValueConverter
{
    private static readonly VisibilityConverter defaultInstance = new VisibilityConverter();

    public static VisibilityConverter Default = new VisibilityConverter();
    public static VisibilityConverter InverseBoolean = new VisibilityConverter() { Inverse = true };

        public bool Inverse { get; set; }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var isVisible = value != null
                && value.GetType() == typeof(bool)
                && (bool)value;

            if (Inverse)
            {
                isVisible = !isVisible;
            }

            return isVisible ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
        }

然后可以轻松地在默认和反向模式下使用它:

Converter={x:Static converters:VisibilityConverter.Default}
Converter={x:Static converters:VisibilityConverter.InverseBoolean}

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:

public class VisibilityConverter : IValueConverter
{
    private static readonly VisibilityConverter defaultInstance = new VisibilityConverter();

    public static VisibilityConverter Default = new VisibilityConverter();
    public static VisibilityConverter InverseBoolean = new VisibilityConverter() { Inverse = true };

        public bool Inverse { get; set; }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var isVisible = value != null
                && value.GetType() == typeof(bool)
                && (bool)value;

            if (Inverse)
            {
                isVisible = !isVisible;
            }

            return isVisible ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
        }

This can then easily be used in a default and in a inverse mode:

Converter={x:Static converters:VisibilityConverter.Default}
Converter={x:Static converters:VisibilityConverter.InverseBoolean}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文