为什么在绑定 class 继承接口时无法更新 UI?
Why I can't update the UI when binding a class inherits the interface?
我正在尝试编写一个绑定 class 继承接口的演示。
下面是接口代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace App1
{
public interface BaseClass
{
string AAA { get; set; }
}
}
这是页面的隐藏代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace App1
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page, INotifyPropertyChanged
{
public BaseClass BC;
public static MainPage MP;
public MainPage()
{
this.InitializeComponent();
MP = this;
BC = new ChildClass();
}
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string name)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
public class ChildClass : BaseClass
{
string _AAA = "123";
public string AAA
{
get { return _AAA; }
set { _AAA = value; MP.RaisePropertyChanged("AAA"); }
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
BC.AAA = "456";
}
}
}
最后是 XAML:
<Page
x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Text="{x:Bind BC.AAA,Mode=OneWay}"></TextBlock>
<Button Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Click="Button_Click">123</Button>
</Grid>
</Page>
我运行程序运行后,会成功显示AAA的默认字符串。但是,当我点击按钮时,它无法将文本更改为“456”。
我想知道是否存在 INotifyPropertyChanged 问题。但是不知道怎么回事
你能告诉我这有什么问题吗?谢谢。
2 个错误:
第一个:
请注意 ChidClass
调用 MP.RaisePropertyChanged("AAA")
,而不是 BaseClass
。所以,与其像
这样声明 BC
public BaseClass BC;
做
public ChildClass BC;
代替。
第二个:
属性 AAA
不属于 MainPage
class,它属于 ChildClass
,所以不要在 [=17] 中实现 INotifyPropertyChanged
=], 在 ChildClass
:
中实现
public class ChildClass : BaseClass, INotifyPropertyChanged
{
string _AAA = "123";
public string AAA
{
get { return _AAA; }
set { _AAA = value; RaisePropertyChanged("AAA"); }
}
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string name)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
这应该有效。
编辑:
你犯的第一个错误,如果是故意的,因为你想将 UI 绑定到这个界面,那么 对不起,你不能那样做。
为什么?因为,要实现这一点,您需要在该接口中引发 INotifyPropertyChanged
事件,但 c# 中的接口(当前为 c# 7.3)不允许您执行任何默认实现,这意味着您不能编写任何代码来引发那个事件。话虽如此,即将推出的 C# 8.0 将具有此功能。你能等到那个时候吗?
我正在尝试编写一个绑定 class 继承接口的演示。
下面是接口代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace App1
{
public interface BaseClass
{
string AAA { get; set; }
}
}
这是页面的隐藏代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace App1
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page, INotifyPropertyChanged
{
public BaseClass BC;
public static MainPage MP;
public MainPage()
{
this.InitializeComponent();
MP = this;
BC = new ChildClass();
}
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string name)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
public class ChildClass : BaseClass
{
string _AAA = "123";
public string AAA
{
get { return _AAA; }
set { _AAA = value; MP.RaisePropertyChanged("AAA"); }
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
BC.AAA = "456";
}
}
}
最后是 XAML:
<Page
x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Text="{x:Bind BC.AAA,Mode=OneWay}"></TextBlock>
<Button Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Click="Button_Click">123</Button>
</Grid>
</Page>
我运行程序运行后,会成功显示AAA的默认字符串。但是,当我点击按钮时,它无法将文本更改为“456”。
我想知道是否存在 INotifyPropertyChanged 问题。但是不知道怎么回事
你能告诉我这有什么问题吗?谢谢。
2 个错误:
第一个:
请注意 ChidClass
调用 MP.RaisePropertyChanged("AAA")
,而不是 BaseClass
。所以,与其像
public BaseClass BC;
做
public ChildClass BC;
代替。
第二个:
属性 AAA
不属于 MainPage
class,它属于 ChildClass
,所以不要在 [=17] 中实现 INotifyPropertyChanged
=], 在 ChildClass
:
public class ChildClass : BaseClass, INotifyPropertyChanged
{
string _AAA = "123";
public string AAA
{
get { return _AAA; }
set { _AAA = value; RaisePropertyChanged("AAA"); }
}
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string name)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
这应该有效。
编辑: 你犯的第一个错误,如果是故意的,因为你想将 UI 绑定到这个界面,那么 对不起,你不能那样做。
为什么?因为,要实现这一点,您需要在该接口中引发 INotifyPropertyChanged
事件,但 c# 中的接口(当前为 c# 7.3)不允许您执行任何默认实现,这意味着您不能编写任何代码来引发那个事件。话虽如此,即将推出的 C# 8.0 将具有此功能。你能等到那个时候吗?