使用 Prism ViewModel 和接口绑定的 UWP 停止工作

UWP Using Prism ViewModel & Interfaces Binding stops working

一切正常。当我单击按钮时,会切换边框元素的可见性。

在我的XAML后面的代码中:

Test2ViewModel ViewModel => DataContext as Test2ViewModel;
public Test2Page()
{
    this.InitializeComponent();
}

我的 ViewModel 为:

public class Test2ViewModel : ViewModelBase,ITest
{
    private bool _borderIsVisible;
    public bool borderIsVisible
    {
        get => _borderIsVisible;
        set { SetProperty(ref _borderIsVisible, value); }
    }

    public Test2ViewModel()
    {
        borderIsVisible = true;
    }
    public void ToggleVisibility()
    {
        if (borderIsVisible)
        {
            borderIsVisible = false;
        }
        else
        {
            borderIsVisible = true;
        }
    }

我的XAML:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="200" />
        <RowDefinition Height="200" />
    </Grid.RowDefinitions>
    <Button
        Grid.Row="0"
        HorizontalAlignment="Center"
        Click="{x:Bind ViewModel.ToggleVisibility}"
        Content="Click Me" />
    <Border
        Grid.Row="1"
        Width="250"
        Background="AliceBlue"
        BorderBrush="Blue"
        BorderThickness="4"
        Visibility="{x:Bind ViewModel.borderIsVisible, Mode=OneWay}" />
</Grid>

当我尝试实现这样的接口时,它停止工作:

ITest ViewModel => DataContext as Test2ViewModel;

应用程序运行但可见性绑定停止工作,我不知道为什么。

编译的{x:Bind}检查绑定的类型是否为INotifyPropertyChanged,以便它可以连接用于数据绑定的NotifyPropertyChanged事件。但是,由于 x:Bind 是在编译时计算的,所以它不能这样做,因为 ITest 不是从 INotifyPropertyChanged.

派生的

要解决此问题,您需要确保 ITest 扩展 INotifyPropertyChanged:

interface ITest : INotifyPropertyChanged
{
    ... 
}