ObservesProperty 方法不在 Prism 6 上观察模型的属性

ObservesProperty method isn't observing model's properties at Prism 6

我正在尝试学习 Prism MVVM,我正在制作一个 window,其中包含 2 个字段和一个按钮,当这两个字段不为空时会启用该按钮。

问题是我找不到使方法 ObservesProperty() 对对象(在那种情况下为 Pessoa )起作用的方法。 CanExecuteAtualizar() 方法仅在应用程序启动时被调用,当我编辑文本字段 NomeSobrenome 按钮没有任何反应并且该方法未被触发...

我尝试在没有模型的情况下工作,将 NomeSobrenomeUltimaAtualizacao 属性直接放在 ViewModel 中并且工作正常,根据 return 的方法 CanExecuteAtualizar,但我想将它与模型一起使用。有办法吗?

ViewAViewModel.cs

public class ViewAViewModel : BindableBase
{
    private Pessoa _pessoa;

    public Pessoa Pessoa
    {
        get { return _pessoa; }
        set { SetProperty(ref _pessoa, value); }
    }

    public ICommand CommandAtualizar { get; set; }

    public ViewAViewModel()
    {
        Pessoa = new Pessoa();
        Pessoa.Nome = "Gabriel";
        CommandAtualizar = new DelegateCommand(ExecuteAtualizar, CanExecuteAtualizar).ObservesProperty(() => Pessoa.Nome).ObservesProperty(() => Pessoa.Sobrenome);
    }

    public bool CanExecuteAtualizar()
    {
        return !string.IsNullOrWhiteSpace(Pessoa.Nome) && !string.IsNullOrWhiteSpace(Pessoa.Sobrenome);
    }

    public void ExecuteAtualizar()
    {
        Pessoa.UltimaAtualizacao = DateTime.Now;
    }
}

Pessoa.cs

public class Pessoa : BindableBase
{
    private string _nome;

    public string Nome
    {
        get { return _nome; }
        set { SetProperty(ref _nome, value); }
    }

    private string _sobrenome;

    public string Sobrenome
    {
        get { return _sobrenome; }
        set { SetProperty(ref _sobrenome, value); }
    }

    private DateTime? _ultimaAtualizacao;

    public DateTime? UltimaAtualizacao
    {
        get { return _ultimaAtualizacao; }
        set { SetProperty(ref _ultimaAtualizacao, value); }
    }
}

ViewA.xaml

<UserControl x:Class="PrismDemo.Views.ViewA"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:local="clr-namespace:PrismDemo.Views"
             mc:Ignorable="d"
             d:DesignHeight="100" d:DesignWidth="500">
    <Grid Background="White">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="2*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Label Content="Nome:"  Grid.Column="0" Grid.Row="0" HorizontalAlignment="Left" VerticalAlignment="Center" />
        <TextBox Grid.Column="1" Grid.Row="0"  Margin="3" TabIndex="0" Text="{Binding Pessoa.Nome, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
        <Label Content="Sobrenome:"  Grid.Column="0" Grid.Row="1" HorizontalAlignment="Left" VerticalAlignment="Center" />
        <TextBox Grid.Column="1" Grid.Row="1"  Margin="3" TabIndex="1" Text="{Binding Pessoa.Sobrenome, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
        <Label Content="Última atualização:"  Grid.Column="0" Grid.Row="2" HorizontalAlignment="Left" VerticalAlignment="Center" />
        <Label Grid.Column="1" Grid.Row="2"  Margin="3" HorizontalAlignment="Left" Content="{Binding Pessoa.UltimaAtualizacao, Mode=TwoWay}" />
        <Button Content="Atualizar" Grid.Column="1" Grid.Row="3" Width="70" Margin="2,2,3,2" HorizontalAlignment="Right" Command="{Binding CommandAtualizar}" />
    </Grid>
</UserControl>

DelegateCommand.ObservesPropery 不支持复杂的对象属性。它只支持命令中定义的 ViewModel 上存在的属性。这是因为复杂对象的生命周期是未知的,如果创建了很多对象实例就会造成内存泄漏。我的建议是这样定义你 属性:

private Pessoa _pessoa;
public Pessoa Pessoa
{
    get { return _pessoa; }
    set 
    {
        if (_pessoa != null)
            _pessoa.PropertyChanged -= PropertyChanged; 

        SetProperty(ref _pessoa, value);


        if (_pessoa != null)
            _pessoa.PropertyChanged += PropertyChanged;
    }
}

然后在PropertyChanged方法中,调用DelegateCommand.RaiseCanExecuteChanged

编辑: Xamarin.Forms 7.0.

的 Prism 现已提供复杂 属性 支持

确实 DelegateCommand.ObservesPropery 不支持复杂对象,但它是命令与 Prism 一起使用的方式。在我看来,手动调用 PropertyChanged 是一个丑陋的 hack,应该避免。它还使 Prism 试图减少的代码再次膨胀。

另一方面,将复杂类型的所有属性移动到 ViewModel 中会降低 ViewModel 的可读性。在这种情况下创建复杂类型的真正原因是首先要避免那里有太多的单一属性。

但是您可以将 Command 定义移到复杂类型中。然后你可以在复杂类型的构造函数中为所有简单属性设置 ObservesProperty,一切都按预期工作。

型号:

using Prism.Commands;
using Prism.Mvvm;
using static System.String;

public class LoginData : BindableBase
{
    public LoginData()
    {
        DbAddr = DbName = DbUser = DbPw = "";

        TestDbCommand = new DelegateCommand(TestDbConnection, CanTestDbConnection)
            .ObservesProperty(() => DbAddr)
            .ObservesProperty(() => DbName)
            .ObservesProperty(() => DbUser)
            .ObservesProperty(() => DbPw);
    }

    public DelegateCommand TestDbCommand { get; set; }

    public bool CanTestDbConnection()
    {
        return !IsNullOrWhiteSpace(DbAddr)
            && !IsNullOrWhiteSpace(DbName)
            && !IsNullOrWhiteSpace(DbUser)
            && !IsNullOrWhiteSpace(DbPw);
    }

    public void TestDbConnection()
    {
        var t = new Thread(delegate () {
            Status = DatabaseFunctions.TestDbConnection(this);
        });
        t.Start();
    }

    private string _dbAddr;
    public string DbAddr
    {
        get => _dbAddr;
        set => SetProperty(ref _dbAddr, value);
    }

    ...
}

ViewModel:

public class DatabaseConfigurationViewModel
{
    public DatabaseConfigurationViewModel()
    {
        CurrentLoginData = new LoginData(true);
    }

    public LoginData CurrentLoginData { get; set; }
}

查看:

<UserControl x:Class="TestApp.Views.DatabaseConfiguration"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:prism="http://prismlibrary.com/"
         prism:ViewModelLocator.AutoWireViewModel="True"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
    <StackPanel Orientation="Vertical">
        <Label>IP Adresse oder URL:</Label>
        <TextBox Text="{Binding CurrentLoginData.DbAddr, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"></TextBox>
        ...
        <Button Command="{Binding CurrentLoginData.TestDbCommand}">Teste Verbindung</Button>
       </StackPanel>
   </Grid>

在我的 Prism 版本 (7.0.0.362) 中,您可以使用 ObserveCanExecute,并通过 属性 HasChanges 更新每个 属性 更改您的实体。

TestDbCommand = new DelegateCommand(TestDbConnection).ObservesCanExecute(() => HasChanged);
Pessoa = new Pessoa();
Pessoa.PropertyChanged += Pessoa_PropertyChanged;

然后在构造函数中使用验证方法更新HasChanges,并在析构函数中分离该方法。

private void Pessoa_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    HasChanged = ValidatePessoa(Pessoa);
}

~YourViewModel()
{
    Pessoa.PropertyChanged -= Pessoa_PropertyChanged;
}
bool _hasChanged;
public bool HasChanged { get => _hasChanged; set => SetProperty(ref _hasChanged, value); }