简体   繁体   English

UWP 使用 x:Bind 监控全局变量

[英]UWP Using x:Bind to monitor global variables

I have an App that groups all the global variables in a single class.我有一个应用程序将所有全局变量分组到一个 class 中。 They are used over MANY classes in the App.它们用于应用程序中的许多类。 (300+ variables, shortened for demo): (300 多个变量,为演示而缩短):

public class Vars
{
    public static string DateStr = "";
}

I want to use x:Bind to One-way bind the data to fields on a page:我想使用 x:Bind to One-way 将数据绑定到页面上的字段:

<TextBlock x:Name="Local_Data" Text="{x:Bind local:Vars.DateStr, Mode=OneWay}" Style="{StaticResource TextBlockStyle1}"/>

OneTime binding seems to work OK. OneTime 绑定似乎工作正常。 I can refresh the page and the DateStr reflects the new value.我可以刷新页面,并且 DateStr 反映了新值。

I changed the Vars class definition to:我将 Vars class 定义更改为:

public class Vars : INotifyPropertyChanged
{

    private static string _DateStr = "hello";

    public static string DateStr
    {
        get { return _DateStr; }
        set
        {
            _DateStr = value;
            OnPropertyChanged();
        }
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    /// <summary>
    /// Raises this object's PropertyChanged event.
    /// </summary>
    /// <param name="propertyName">The property that has a new value.</param>
    protected void OnPropertyChanged([CallerMemberName]string propertyName = null)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            var e = new PropertyChangedEventArgs(propertyName);
            handler(this, e);
        }
    }
    #endregion
}

When I try to build, I get the message:当我尝试构建时,我收到消息:

An object reference is required for the non-static field, method, or property 'Vars.OnPropertyChanged(string)'非静态字段、方法或属性“Vars.OnPropertyChanged(string)”需要 object 引用

If I change it to:如果我将其更改为:

    protected static void OnPropertyChanged([CallerMemberName]string propertyName = null)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            var e = new PropertyChangedEventArgs(propertyName);
            handler(this, e);
        }
    }

I get the message:我收到消息:

Keyword 'this' is not valid in a static property, static method, or static field initializer关键字“this”在 static 属性、static 方法或 static 字段初始化程序中无效

I assume that it is something trivial that I am missing.我认为我错过了一些微不足道的事情。 What is the proper way to flag the data changing?标记数据更改的正确方法是什么?

Thanks for your help.谢谢你的帮助。 Dan

As the error message mentioned, the reason for your issue is that you are using Static modifier for the binding source property- Vars.DateStr .正如提到的错误消息,您的问题的原因是您使用Static修饰符作为绑定源属性Vars.DateStr You just need to remove the Static modifier of the Vars.DateStr property.您只需要删除Vars.DateStr属性的Static修饰符。

  public  string DateStr
    {
        get { return _DateStr; }
        set
        {
            _DateStr = value;
            OnPropertyChanged("DateStr");
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM