[英]How do I use ViewModelViewHost in a ListView ItemTemplate with ReactiveUI and Xamarin Forms?
[英]How do I chain SourceList observation using ReactiveUI and DynamicData?
如果術語關閉,請道歉; 我是iOS開發人員,不得不使用Xamarin.iOS開發應用程序。 我正在使用帶有DynamicData和MVVM架構的ReactiveUI。 我對RxSwift和FRP概念很滿意。 根據文檔,我有一個發布SourceList<MyThing>
的模型,如下所示:
// Property declarations
private readonly SourceList<MyThing> Things;
public IObservableCollection<MyThing> ThingsBindable { get; }
// Later, in the constructor...
Things = new SourceList<MyThing>();
// Is this of the right type?
ThingsBindable = new ObservableCollectionExtended<MyThing>();
Things
.Connect()
.Bind(ThingsBindable)
.Subscribe();
我可以在我的View中成功使用.BindTo()
(即iOS-land中的ViewController),以便在模型更改時更新UITableView:
Model
.WhenAnyValue(model => model.ThingsBindable)
.BindTo<MyThing, MyThingTableViewCell>(
tableView,
new NSString("ThingCellIdentifier"),
46, // Cell height
cell => cell.Initialize());
我想,而不是直接綁定到Model,讓ViewModel訂閱和發布(或以其他方式代理) SourceList<MyThing>
,或者它的可綁定版本,以便View只使用ViewModel屬性。 SourceList
在docs中聲明為private
; 我不確定這里的最佳做法:我是否公開並在ViewModel中進行Connect()
? 或者有沒有辦法從ViewModel IObservableCollection<MyThing> ThingsBindable
公開公開的IObservableCollection<MyThing> ThingsBindable
? 我也不相信ObservableCollectionExtended<MyThing>
是Bindable屬性的正確類型,但它似乎有效。
我已經嘗試過.ToProperty()
.Bind()
.Publish()
等的各種組合,並在ViewModel中制作一個View-binding Observable的版本無濟於事,我現在只是在牆上拋出自動完成功能什么堅持。 任何方向贊賞。 TIA。
我認為這是初學者的誤解。 這就是我按照自己想要的方式工作的原因; 也許它會幫助其他Xamarin.iOS / ReactiveUI / DynamicData新手。
在我的模型中,我聲明了一個私有SourceList
和一個公開公開的IObservableList<MyThing>
:
private readonly SourceList<MyThing> _ModelThings;
public IObservableList<MyThing> ModelThings;
然后在我的構造函數中實例化它們:
_ModelThings = new SourceList<MyThing>();
ModelThings = _Things.AsObservableList();
在我的ViewModel中,我聲明了一個本地ObservableCollectionExtended<MyThing>
並將其綁定到Model的公共屬性:
public ObservableCollectionExtended<MyThing> ViewModelThings;
// Then, in the constructor:
ViewModelThings = new ObservableCollectionExtended<MyThing>();
model.ModelThings
.Connect()
.Bind(ViewModelThings)
.Subscribe();
在我的ViewController中,我將表綁定到ViewModel.ViewModelThings
,如問題所示。 如果我想擁有另一個級別的模型,我可以簡單地通過Model.ModelThings
和Model.ModelThings
.Connect().Bind()
降低,正如格倫在他的評論中暗示的那樣。
FWIW,我發現Roland的Blog (特別是關於Observable Lists / Caches的部分)比GitHub文檔更容易理解。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.