模型中的简单属性-6ren">
gpt4 book ai didi

c# - 如何从 "traditional"过渡到响应式(Reactive) MVVM

转载 作者:行者123 更新时间:2023-11-30 18:22:31 25 4
gpt4 key购买 nike

我想过渡到响应式 View 模型/模型。

到目前为止,我已经使用了 3 个场景:

  • “ValueA”:一次只能从一个 View 模型访问模型值,并且只能通过 View 模型更改值

    => 模型中的简单属性, View 模型中使用 PropertyChanged 转发属性

  • “ValueB”:从多个 View 模型访问模型值和/或从其他来源更改

    => 模型中带有事件的属性,转发属性以及从已更改事件到 View 模型中的 PropertyChanged 的转换

  • “ValueC”:仅在 View 模型中使用的值

    => 模型中没有属性,属性由 View 模型中的 PropertyChanged 自己的字段支持

这是我的“当前”方法:

class Model
{
public string ValueA {get;set;}

private string valueB;
public event ValueBChangedEvent ValueBChanged;
public string ValueB
{
get
{
return valueB;
}
set
{
valueB = value;
ValueBChanged();
}
}
}

class ViewModel : INotifyPropertyChanged
{
private Model model;

public string ValueA
{
get
{
return model.ValueA;
}
set
{
model.ValueA = value;
OnPropertyChanged();
}
}

ViewModel()
{
model.ValueBChanged += model_ValueBChanged;
}

private void model_ValueBChanged()
{
OnPropertyChanged("ValueB");
}


public string ValueB
{
get
{
return model.ValueB;
}
set
{
model.ValueB = value;
// no change notification since done via model
}
}


private string valueC;
public string ValueC
{
get
{
return valueC;
}
set
{
valueC = value;
OnPropertyChanged();
}
}
}

这就是我打算使用响应式扩展对它们进行建模的方式:

class ReactiveModel
{
public string ValueA {get;set;}

private ISubject<string> valueB = new Subject<string>();
public ISubject<string> ValueB
{
get
{
return valueB;
}
}
}

class ReactiveViewModel : INotifyPropertyChanged
{
private ReactiveModel model;

public string ValueA
{
get
{
return ???;
}
set
{
???
}
}


private ReactiveProperty<string> valueB = model.valueB.ToReactiveProperty();
public Reactive<string> ValueB
{
get
{
return valueB;
}
// no setter since access via ValueB.Value which is read-write
}


private ISubject<string> _valueC = new Subject<string>();
private ReactiveProperty<string> valueC = _valueC.ToReactiveProperty();
public ReactiveProperty<string> ValueC
{
get
{
return valueC;
}
// no setter since access via ValueC.Value which is read-write
}
}

总结:

  • “ValueA”:我对这个案子一无所知
  • “ValueB”:乍一看有效,但既不会将更改从 View 模型传播到模型,也不会以其他方式传播。
  • “ValueC”:按预期工作

如果我有 ValueA 和 ValueB 的解决方案,我会很高兴。

最佳答案

ValueB: View 模型负责更新模型。 ReactiveProperty仅使用模型属性中的 IObservable 接口(interface)并从 ValueB 中读取值(不写入任何内容)。ReactiveProperty 由 View 通过 Value 属性更改。ReactiveProperty 实现了 IObservable,您应该订阅更改以获取新值。

ValueA:我们可以在 View 模型端创建一个 ReactiveProperty 订阅,以将更改的值传播到模型。

解决方案的代码如下:

class ReactiveModel
{
public string ValueA {get;set;}
private readonly Subject<string> valueB = new Subject<string>();
public IObservable<string> ValueB
{
get
{
return valueB;
}
}

public void UpdateB(string newValue)
{
valueB.OnNext(newValue);
}
}

class ReactiveViewModel
{
private readonly ReactiveModel model;
private readonly ReactiveProperty<string> valueA;
private readonly ReactiveProperty<string> valueB;

public ReactiveViewModel(ReactiveModel model)
{
this.model = model;

valueA = new ReactiveProperty<string>(model.ValueA);
valueA.Subscribe(x => model.ValueA = x);

valueB = model.ValueB.ToReactiveProperty();
valueB.Subscribe(model.UpdateB);
}

public IObservable<string> ValueA
{
get
{
return valueA;
}
}

public ReactiveProperty<string> ValueB
{
get
{
return valueB;
}
}
}

XAML 在这两种情况下都是:

<TextBox Text="{Binding ValueA.Value, UpdateSourceTrigger=PropertyChanged}"/>

关于c# - 如何从 "traditional"过渡到响应式(Reactive) MVVM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33879981/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com