为什么在使用 propertychanged 将字符串绑定到我的 XAML 时我没有收到任何文本? C# Xamarin
Why do I not get any text when I am binding a string to my XAML using a propertychanged? C# Xamarin
尝试通过字符串将文本绑定到我的 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");
}
}
}
我现在没有收到短信。
那不是 OnPropertyChanged()
的工作方式。您不应将要显示的新文本作为参数传递,而应将新文本分配给信息字段,并将 属性 的名称作为参数。
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
让事件的所有订阅者知道 属性 属性Name 已经改变。 XAML {Binding Forename}
充当 PropertyChanged
的 subscriber/listener,并会在得知它绑定到 属性 后立即更新其文本("Forename") 已更改。当您拨打 OnPropertyChanged("Forename")
.
时会发生什么
编辑 2
看来您可能忘记分配您的 DataContext
在您的 XAML 的代码隐藏中(将被命名为 mainwindow.xaml.cs)
InitializeComponent();
ViewModel vm = new ViewModel();
DataContext = vm;
vm.Forename = "TestString"
并将名字代码内容移至单独的 class,命名为 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");
}
}
}
}
通过查看您的代码,我怀疑您犯了 Binding
中最常见的错误。您不是 提出正确 属性 的更改。这个想法是 XAML 必须以某种方式知道 属性 已更改以更新 UI - 因此在紧密循环中改为 运行 并检查 属性 值是否不同于微软在屏幕上提出了名为 PropertyChanged
的 delegate
的想法。主要用于 getter/setters。
您在使用此委托时必须非常小心,因为在 .NET Framework 版本 4 之前,委托的参数是 string
- name [=29] =].更高版本的 .NET Framework 带来了特殊属性 CallerMemberName - here is example: Jesse Liberty
在您的情况下,您启动了 属性 更改事件,但在参数中您发送了不存在的属性名称:( 您应该只发送 OnPropertyChanged("Forename")
。
尝试通过字符串将文本绑定到我的 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");
}
}
}
我现在没有收到短信。
那不是 OnPropertyChanged()
的工作方式。您不应将要显示的新文本作为参数传递,而应将新文本分配给信息字段,并将 属性 的名称作为参数。
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
让事件的所有订阅者知道 属性 属性Name 已经改变。 XAML {Binding Forename}
充当 PropertyChanged
的 subscriber/listener,并会在得知它绑定到 属性 后立即更新其文本("Forename") 已更改。当您拨打 OnPropertyChanged("Forename")
.
编辑 2 看来您可能忘记分配您的 DataContext 在您的 XAML 的代码隐藏中(将被命名为 mainwindow.xaml.cs)
InitializeComponent();
ViewModel vm = new ViewModel();
DataContext = vm;
vm.Forename = "TestString"
并将名字代码内容移至单独的 class,命名为 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");
}
}
}
}
通过查看您的代码,我怀疑您犯了 Binding
中最常见的错误。您不是 提出正确 属性 的更改。这个想法是 XAML 必须以某种方式知道 属性 已更改以更新 UI - 因此在紧密循环中改为 运行 并检查 属性 值是否不同于微软在屏幕上提出了名为 PropertyChanged
的 delegate
的想法。主要用于 getter/setters。
您在使用此委托时必须非常小心,因为在 .NET Framework 版本 4 之前,委托的参数是 string
- name [=29] =].更高版本的 .NET Framework 带来了特殊属性 CallerMemberName - here is example: Jesse Liberty
在您的情况下,您启动了 属性 更改事件,但在参数中您发送了不存在的属性名称:( 您应该只发送 OnPropertyChanged("Forename")
。