我有一个使用MVVM-pattern编写的跨平台项目(没有使用特定的框架,只是自编写的实现)。 Project有几个独立的模块,每个模块有几页。 每个页面都有ViewModel和某种负责数据导向逻辑(获取,保存,删除,转换等)的管理器。所以数据流看起来是这样的:
VM - >经理 - >服务 - >经理 - > VM
加载虚拟机时,它要求经理提供数据。 Manager执行服务调用,获取数据,从DTO构建模型集合,将此集合返回到ViewModel,后者将模型集合转换为ViewModel集合 要在列表中呈现。
现在我正在寻找一种使用Rx实现这种逻辑的方法。大多数页面都有一个要编辑的主列表(插入,删除,修改的项目)和几个支持集合(某些组合框的提供者可以从中选择值)。 可以通过标准异步/等待调用或通过将任务转换为Rx轻松检索支持集合 - 它们没有问题。但可修改的清单是。 我无法弄清楚如何在不破坏Rx逻辑的情况下跟踪页面整个生命周期中此列表中的更改。 我可以选择订阅:
import java.io.*;
import java.util.*;
public class atmValidator {
public static void main(String[] args) {
String fname = " ", lname = " ";
double balbefore = 0.0, balafter = 0.0, transamount = 0.0;
char code = ' ';
Scanner screen = null;
FileInputStream fis = null;
FileOutputStream fos = null;
PrintWriter pw = null;
//Declaring the variables/scanner/etc.
try {
fis = new FileInputStream ("transactions.txt");
fos = new FileOutputStream ("statement.txt");
screen = new Scanner (fis);
pw = new PrintWriter (fos);
} catch (FileNotFoundException e) {
System.out.println("File not found");
System.exit(0);
}
//The try/catch for reading from the file
while (screen.hasNext()) {
fname = screen.next();
lname = screen.next();
balbefore = screen.nextDouble();
code = screen.next().charAt(0);
transamount = screen.nextInt();
//A while loop to gather the info from the file, and the variables getting their values from the files.
switch (code) {
case 'D':
balafter = balbefore + transamount;
if (transamount <0) {
pw.println("ERROR: Enter positive amount.");
}
else if (balafter < 300) {
pw.println("Warning, balance below $300");
}
{
// Where I am stuck at :(
}
}
}
screen.close();
pw.close();
}
}
//Sorry for the weird braces and spaces
IEnumerable<Model>
Task<IEnumerable<Model>>
但我想我必须订阅IObservable<IEnumerable<Model>>
,因为我需要一种方法来跟踪个别更改。
我需要一种方法来修改此集合,从其他方法,如添加,删除或编辑。
那么我应该通过IObservable<Model>
(或其他方法)创建IObservable
并将Observable.Create
存储在Manager中的某个位置,以便在其他方法中调用IObserver
或OnNext
?但它看起来不像是Rx-way-to-do。
你对我的问题有什么建议吗?任何建议表示赞赏。感谢。
答案 0 :(得分:1)
看看ReactiveList<T>
- 它是ReactiveUI
框架的一部分,我们已经习惯了它对MVVM / WPF UI的影响很大。
答案 1 :(得分:1)
ReactiveList有效,但它没有暴露任何提供所有修改事件的Rx流。
请参阅this相关问题,答案是使用IObservable<IObservable<Model>>
,每个内部可观察对象代表列表中的一个项目及其修改(以及删除 - 完成时)。