c# - 使用组合的类中的 MvvmLight
问题描述
我有一个 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);
}
}
使用字段获取/设置有效,但如果我想将属性设置转发给复合对象,我不能使用base.Set
set => base.Set(nameof(GraphWidth), ref timeFrameFactory.GraphWidth, value);
属性上不允许使用 ref。
我当然可以写:
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
?
解决方案
好吧,基本方法需要能够读取(用于比较)和写入传递的字段/属性,因此 ref.
由于您不能通过引用传递属性,我认为您被迫编写另一个基本方法
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>>
包含属性并使用反射来提取属性并在基础中获取/设置它(慢,但也可能提取名称)
public int GraphWidth
{
get => this.timeFrameFactory.GraphWidth;
set => base.Set(() => timeFrameFactory.GraphWidth, value);
}
推荐阅读
- ms-access - 组合框保存 ID 而不是项目名称
- python - 为什么这个解决方案不适合“旋转数组”leetcode 问题?
- sql - 聚合访问 SQL 的问题
- xamarin.forms - VS2019 Xamarin Forms Remote IOS模拟器黑屏 - 如何修复?
- sql - Oracle -- 根据 varchar 时间戳提取月度数据
- django - Unity Facebook SDK 登录,使用 Django 服务在 Chrome 上遇到 CORS/CORB 问题
- compilation - 如何为 GDB 的 JIT 代码生成 ELF 文件格式?
- javascript - setItems Acrobat Pro 的分层命名
- linux - 为什么 dlsym 为非 pie 可执行文件返回 PLT 条目?
- kubernetes - kube-prometheus-stack - Grafana 中的新仪表板