class 中使用合成的 MvvmLight
MvvmLight in a class that uses composition
我有一个 ViewModel,派生自 MvvmLight.ViewModelBase
,它使用组合来重用其他 类:
我想在合成中重复使用的 类 的简化版本:
class TimeFrameFactory
{
public DateTime SelectedTime {get; set;}
public ITimeFrame CreateTimeFrame() {...}
}
class GraphFactory
{
public int GraphWidth {get; set;}
public IGraph CreateGraph(ITimeframe timeframe) {...}
}
我的 ViewModel 派生自 MvvmLight ViewModelBase 是这两者的组合:
class MyViewModel : ViewModelBase
{
private readonly TimeFrameFactory timeFrameFactory = new TimeFrameFactory();
private readonly GraphFactory graphFactory = new GraphFactory();
private Graph graph;
// standard MVVM light method to get/set a field:
public Graph Graph
{
get => this.Graph;
private set => base.Set(nameof(Graph), ref graph, value);
}
// this one doesn't compile:
public DateTime SelectedTime
{
get => this.timeFrameFactory.SelectedTime;
set => base.Set(nameof(SelectedTime), ref timeFrameFactory.SelectedTime, value);
}
// this one doesn't compile:
public int GraphWidth
{
get => this.timeFrameFactory.GraphWidth;
set => base.Set(nameof(GraphWidth), ref timeFrameFactory.GraphWidth, value);
}
public void CreateGraph()
{
ITimeFrame timeFrame = this.timeFrameFactory.CreateTimeFrame();
this.Graph = this.GraphFactory.CreateGraph(timeFrame);
}
}
Get/Set 与字段一起工作,但如果我想将 属性 转发到复合对象,我不能使用 base.Set
set => base.Set(nameof(GraphWidth), ref timeFrameFactory.GraphWidth, value);
属性不允许引用。
我当然可以写:
public int GraphWidth
{
get => this.timeFrameFactory.GraphWidth;
set
{
base.RaisePropertyChanging(nameof(GraphWidh));
base.Set(nameof(GraphWidth), ref timeFrameFactory.GraphWidth, value);
base.RaisePropertyChanged(nameof(GraphWidh));
}
}
如果您必须为很多属性执行此操作,那就太麻烦了。有没有一种巧妙的方法可以做到这一点,可能类似于 ObservableObject.Set
?
嗯,基本方法需要能够读取(用于比较)和写入传递的 field/property,因此引用
由于您不能通过引用传递属性,我认为您被迫编写了另一个基本方法
A) 获得 getter/setter 代表。 (冗长/烦人)
public int GraphWidth
{
get => this.timeFrameFactory.GraphWidth;
set => base.Set(nameof(GraphWidth), () => timeFrameFactory.GraphWidth, x => timeFrameFactory.GraphWith = x, value);
}
或
B) 传递一个包含 属性 的 Expression<Func<T>>
并使用反射在基础中提取 属性 和 get/set 它(慢,但可能会提取名称也是)
public int GraphWidth
{
get => this.timeFrameFactory.GraphWidth;
set => base.Set(() => timeFrameFactory.GraphWidth, value);
}
我有一个 ViewModel,派生自 MvvmLight.ViewModelBase
,它使用组合来重用其他 类:
我想在合成中重复使用的 类 的简化版本:
class TimeFrameFactory
{
public DateTime SelectedTime {get; set;}
public ITimeFrame CreateTimeFrame() {...}
}
class GraphFactory
{
public int GraphWidth {get; set;}
public IGraph CreateGraph(ITimeframe timeframe) {...}
}
我的 ViewModel 派生自 MvvmLight ViewModelBase 是这两者的组合:
class MyViewModel : ViewModelBase
{
private readonly TimeFrameFactory timeFrameFactory = new TimeFrameFactory();
private readonly GraphFactory graphFactory = new GraphFactory();
private Graph graph;
// standard MVVM light method to get/set a field:
public Graph Graph
{
get => this.Graph;
private set => base.Set(nameof(Graph), ref graph, value);
}
// this one doesn't compile:
public DateTime SelectedTime
{
get => this.timeFrameFactory.SelectedTime;
set => base.Set(nameof(SelectedTime), ref timeFrameFactory.SelectedTime, value);
}
// this one doesn't compile:
public int GraphWidth
{
get => this.timeFrameFactory.GraphWidth;
set => base.Set(nameof(GraphWidth), ref timeFrameFactory.GraphWidth, value);
}
public void CreateGraph()
{
ITimeFrame timeFrame = this.timeFrameFactory.CreateTimeFrame();
this.Graph = this.GraphFactory.CreateGraph(timeFrame);
}
}
Get/Set 与字段一起工作,但如果我想将 属性 转发到复合对象,我不能使用 base.Set
set => base.Set(nameof(GraphWidth), ref timeFrameFactory.GraphWidth, value);
属性不允许引用。
我当然可以写:
public int GraphWidth
{
get => this.timeFrameFactory.GraphWidth;
set
{
base.RaisePropertyChanging(nameof(GraphWidh));
base.Set(nameof(GraphWidth), ref timeFrameFactory.GraphWidth, value);
base.RaisePropertyChanged(nameof(GraphWidh));
}
}
如果您必须为很多属性执行此操作,那就太麻烦了。有没有一种巧妙的方法可以做到这一点,可能类似于 ObservableObject.Set
?
嗯,基本方法需要能够读取(用于比较)和写入传递的 field/property,因此引用
由于您不能通过引用传递属性,我认为您被迫编写了另一个基本方法
A) 获得 getter/setter 代表。 (冗长/烦人)
public int GraphWidth
{
get => this.timeFrameFactory.GraphWidth;
set => base.Set(nameof(GraphWidth), () => timeFrameFactory.GraphWidth, x => timeFrameFactory.GraphWith = x, value);
}
或
B) 传递一个包含 属性 的 Expression<Func<T>>
并使用反射在基础中提取 属性 和 get/set 它(慢,但可能会提取名称也是)
public int GraphWidth
{
get => this.timeFrameFactory.GraphWidth;
set => base.Set(() => timeFrameFactory.GraphWidth, value);
}