Mvvm 棱镜的计算特性

Mvvm 棱镜的计算特性,mvvm,xamarin.forms,prism,Mvvm,Xamarin.forms,Prism,在Prism MVVM框架中执行计算属性的最佳方法是什么?我有一个Xamarin.Forms应用程序,在虚拟机上具有以下属性: private string _title; public string Title { get { return _title; } set { SetProperty(ref _title, value); OnPropertyChanged(() => Message); } } privat

在Prism MVVM框架中执行计算属性的最佳方法是什么?我有一个
Xamarin.Forms
应用程序,在虚拟机上具有以下属性:

private string _title;
public string Title
{
    get { return _title; }
    set
    {
        SetProperty(ref _title, value);
        OnPropertyChanged(() => Message);
    }
}

private string _name = "John";
public string Name
{
    get { return _name; }
    set
    {
        SetProperty(ref _name, value);
        OnPropertyChanged(() => Message);
    }
}

public string Message
{
    get { return String.Format("{0},{1}", Name, Title); }
}
代码运行得很好。但是,Prism库在
OnPropertyChanged
语句中警告我使用
RaisePropertyChanged
,这将避免使用魔术字符串,并且使用表达式更改OnPropertyChanged的效率较低

是否有其他方法通知视图在名称或标题更改时重新读取“消息”


这让我想到也许Prism有一种方法来设置东西,这样“名称”和“标题”就不必为了更新消息而知道消息。如果可能的话,这将是最好的。计算属性的“棱镜”方式是什么?我在他们的
Xamarin.Forms
文档中找不到这方面的任何例子。

如果您试图以更难的方式完成这项工作,您可以执行以下操作:

public class FooViewModel : BindableBase
{
    private string _foo;
    public string Foo
    {
        get => _foo;
        set => SetProperty(ref _foo, value, () => RaisePropertyChanged(nameof(FooBar)));
    }

    public string FooBar => $"{Foo} Bar";
}
public class SomeViewModel : BindableBase
{
    public string FirstName { get; set; }

    public string LastName { get; set; }

    public string FullName => $"{FirstName} {LastName}";
}
如果您想让您的生活更轻松,请安装ViewModel,您可以使用如下所示的ViewModel:

public class FooViewModel : BindableBase
{
    private string _foo;
    public string Foo
    {
        get => _foo;
        set => SetProperty(ref _foo, value, () => RaisePropertyChanged(nameof(FooBar)));
    }

    public string FooBar => $"{Foo} Bar";
}
public class SomeViewModel : BindableBase
{
    public string FirstName { get; set; }

    public string LastName { get; set; }

    public string FullName => $"{FirstName} {LastName}";
}
Fody应该给您一个空的Weavers.xml,您只需要将其更新为如下所示:

<?xml version="1.0" encoding="utf-8"?>
<Weavers>
    <PropertyChanged EventInvokerNames="RaisePropertyChanged" />
</Weavers>

如果您试图以更难的方式完成此任务,您可以执行以下操作:

public class FooViewModel : BindableBase
{
    private string _foo;
    public string Foo
    {
        get => _foo;
        set => SetProperty(ref _foo, value, () => RaisePropertyChanged(nameof(FooBar)));
    }

    public string FooBar => $"{Foo} Bar";
}
public class SomeViewModel : BindableBase
{
    public string FirstName { get; set; }

    public string LastName { get; set; }

    public string FullName => $"{FirstName} {LastName}";
}
如果您想让您的生活更轻松,请安装ViewModel,您可以使用如下所示的ViewModel:

public class FooViewModel : BindableBase
{
    private string _foo;
    public string Foo
    {
        get => _foo;
        set => SetProperty(ref _foo, value, () => RaisePropertyChanged(nameof(FooBar)));
    }

    public string FooBar => $"{Foo} Bar";
}
public class SomeViewModel : BindableBase
{
    public string FirstName { get; set; }

    public string LastName { get; set; }

    public string FullName => $"{FirstName} {LastName}";
}
Fody应该给您一个空的Weavers.xml,您只需要将其更新为如下所示:

<?xml version="1.0" encoding="utf-8"?>
<Weavers>
    <PropertyChanged EventInvokerNames="RaisePropertyChanged" />
</Weavers>


事实上,刚刚在互联网上搜索了一下,看起来福迪真的可以用了。同样感谢第一个代码示例。没问题。是的,它确实有效,我现在在我所有的项目中都使用它。事实上,我刚刚在互联网上搜索了一下,看起来Fody确实有效。同样感谢第一个代码示例。没问题。是的,它确实有效,我现在在我所有的项目中都使用它以及所有视图模型继承自
BindableBase
(INotifyPropertyChanged的一个实现)这一事实已经在您更新这些属性时更新了UI。您永远不必显式调用
RaisePropertyChanged
OnPropertyChanged
。因此,您需要将消息设置为与Name类似的属性以及所有视图模型继承自
BindableBase
(INotifyPropertyChanged的一个实现)这一事实已经在您更新这些属性时更新了UI。您永远不必显式调用
RaisePropertyChanged
OnPropertyChanged
。因此,您需要使消息成为一个与名称类似的属性。