【问题标题】:Why do I not get any text when I am binding a string to my XAML using a propertychanged? C# Xamarin当我使用 propertychanged 将字符串绑定到我的 XAML 时,为什么我没有得到任何文本? C# Xamarin
【发布时间】:2016-03-07 21:31:55
【问题描述】:

尝试通过字符串将文本绑定到我的 xaml 并使用 propertychanged 函数根据 int 的值更改文本。 这是我的代码:

XAML:

<Label Text = "{Binding Forename}" />

代码:

public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged (string propertyName)
    {
        var changed = PropertyChanged;
        if (changed != null) {

            PropertyChanged (this, new PropertyChangedEventArgs (propertyName));    
        }

    }

    string info;
    int myInt = 0;

    public string Forename {

        get {
            return info;
        }
        set {
            if (myInt == 0) {
                info = value;
                OnPropertyChanged ("TextOne");
            }

            else if (myInt == 1)
            {
                OnPropertyChanged ("TextTwo");

            }
            else 
            {
                OnPropertyChanged ("TextThree");
            }
        }
    }

我现在没有收到任何文字。

【问题讨论】:

  • 为什么使用“TextOne”、“TextTwo”和“TextThree”作为属性名称?看起来该属性的名称是Forename
  • 啊哈,我以为我在那里为我的标签制作文字?
  • 否... 将属性名称放在那里。新的文本值应该只是属性值,因此在 setter 方法中,您需要始终将 info 设置为您想要的属性值。然后启动 OnPropertyChanged。它使用属性名称来知道哪个属性发生了变化。

标签: c# xamarin xamarin.forms


【解决方案1】:

OnPropertyChanged() 不是这样工作的。您不应将要显示的新文本作为参数传递,而应将新文本分配给 info 字段,并将 属性的名称 作为参数。

OnPropertyChanged(name) 所做的是告诉 XAML 属性 name 已更改,XAML 将在该属性上获取该属性,并相应地更新显示的值。

public string Forename {

    get {
        return info;
    }
    set {
        if (myInt == 0) {
            info = value;
            OnPropertyChanged ("Forename");
        }

        else if (myInt == 1)
        {
            info = "TextTwo";
            OnPropertyChanged ("Forename");

        }
        else 
        {
            info = "TextThree";
            OnPropertyChanged ("Forename");
        }
    }
}

编辑

由于您对应该在 protected virtual void OnPropertyChanged(string propertyName) 中输入的内容有一些疑问,因此该功能就可以了。当它被调用时,它将使用PropertyChangedEventHandler PropertyChanged 让所有订阅该事件的人知道属性 propertyName 已更改。 XAML {Binding Forename} 充当PropertyChanged 的订阅者/侦听器,一旦被告知其绑定的属性(“Forename”)已更改,它将立即更新其文本。当您拨打电话OnPropertyChanged("Forename") 时发生了什么。

编辑 2 看来您可能忘记分配 DataContext 在 XAML 的代码隐藏中(将命名为 mainwindow.xaml.cs)

InitializeComponent();
ViewModel vm = new ViewModel();
DataContext = vm;
vm.Forename = "TestString"

并将 Forename-code 内容移到一个单独的类中,命名为 ViewModel。像这样:

public class ViewModel{
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged (string propertyName)
    {
        var changed = PropertyChanged;
        if (changed != null) {
            PropertyChanged (this, new PropertyChangedEventArgs (propertyName));    
        }

    }

string info;
int myInt = 0;

public string Forename {

    get {
        return info;
    }
    set {
        if (myInt == 0) {
            info = value;
            OnPropertyChanged ("Forename");
        }

        else if (myInt == 1)
        {
            info = "TextTwo";
            OnPropertyChanged ("Forename");

        }
        else 
        {
            info = "TextThree";
            OnPropertyChanged ("Forename");
        }
    }
}
}

【讨论】:

  • 在我受保护的虚拟 void OnPropertyChanged(name) 中,我应该用值替换名称吗?我找不到值或 Forname 字符串
  • 由于OnPropertyChanged调用是一样的,你可以把它移到if{}else{}之后。
  • 我的意思是这个:“protected virtual void OnPropertyChanged ()”我应该在里面添加什么?
  • @William.John,当我说OnPropertyChanged(name) 时,我的意思是OnPropertyChanged() 中的参数应该是属性的名称,在本例中为“Forename”。据我所知,您的protected virtual void OnPropertyChanged () 很好。
  • @William.John 绑定工作一次还是根本没有工作?即您是否正确设置了DataContext
【解决方案2】:

通过查看您的代码,我怀疑您在Binding 中犯了最常见的错误。您没有为正确的属性提出更改。这个想法是 XAML 必须以某种方式知道该属性已更改以更新 UI - 因此在紧密循环中运行并检查属性值是否与屏幕上的值不同 Microsoft 提出了名为 PropertyChangeddelegate 的想法。主要用于 getter/setter。

在使用此委托时必须非常小心,因为在 .NET Framework 版本 4 之前,委托的参数是 string - 属性的名称。 .NET Framework 的更高版本带来了特殊属性CallerMemberName - 这里是示例:Jesse Liberty

在您的情况下,您提升了属性更改事件,但在参数中您发送了不存在的属性名称:(您应该只发送OnPropertyChanged("Forename")

【讨论】:

  • protected virtual void OnPropertyChanged ("Forename") //error: Unexpect symbol 'Forename'
  • 您应该将方法定义从protected virtual void OnPropertyChanged (string propertyName) 更改为protected virtual void OnPropertyChanged ("Forename"),在调用方法时将字符串“Forename”作为参数传递。 (在您的财产的set 部分)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-11
  • 1970-01-01
  • 2013-08-17
  • 1970-01-01
  • 1970-01-01
  • 2019-08-13
相关资源
最近更新 更多