为什么我在绑定类时无法更新UI继承接口?

问题描述 投票:0回答:1

我正在尝试编写一个绑定类的演示继承接口。 这是接口代码:

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; }
    }
}

这是Page的代码隐藏:

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问题。但我不知道它有什么问题。 你能告诉我这有什么问题吗?谢谢。

uwp uwp-xaml
1个回答
1
投票

2个错误:

第一:

请注意,ChidClass调用MP.RaisePropertyChanged("AAA"),而不是BaseClass。所以,而不是像BC一样宣布BC

public BaseClass BC;

public ChildClass BC;代替。

第二:属性AAA不属于MainPage类,它属于ChildClass,所以不是在INotifyPropertyChanged中实现MainPage,而是在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将具备此功能。你能等到那么吗?

© www.soinside.com 2019 - 2024. All rights reserved.