绑定类继承接口时,为什么不能更新UI?

时间:2018-11-16 14:38:00

标签: uwp uwp-xaml

我正在尝试编写一个绑定类的演示,该类继承了该接口。

这是接口的代码:

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问题。但我不知道这是怎么回事。

您能告诉我这是怎么回事吗?谢谢。

1 个答案:

答案 0 :(得分:1)

2个错误:

第一

请注意,ChidClass会调用MP.RaisePropertyChanged("AAA"),而不是BaseClass。因此,与其像

那样声明BC,

public BaseClass BC;

public ChildClass BC;

第二: 属性AAA不属于MainPage类,它属于ChildClass,因此与其在INotifyPropertyChanged中实现MainPage,而在{ {1}}:

ChildClass

这应该有效。

编辑: 您犯的第一个错误(如果是故意的)是因为您想要将UI绑定到此界面,然后抱歉,您不能这样做

为什么?因为要实现这一点,您需要在该接口中引发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)); } } 事件,但是c#(当前为c#7.3)中的接口不允许您执行任何默认实现,这意味着您无法编写任何代码来引发那件事。话虽如此,不久的将来,C#8.0将具有此功能。你能等到那时吗?