C# 如何使用Xamarin Android和反应式UI将数据绑定到自定义ListView
我使用的是带有反应式用户界面的Xamarin Android,而不是Xamarin表单。我有一个自定义的ListView(我已经将它的布局定义为xaml)。我不知道如何使用activity类中的OneWayBind方法将此控件绑定到ViewModel中的observableCollection中 我写的是C# 如何使用Xamarin Android和反应式UI将数据绑定到自定义ListView,c#,listview,data-binding,xamarin.android,reactiveui,C#,Listview,Data Binding,Xamarin.android,Reactiveui,我使用的是带有反应式用户界面的Xamarin Android,而不是Xamarin表单。我有一个自定义的ListView(我已经将它的布局定义为xaml)。我不知道如何使用activity类中的OneWayBind方法将此控件绑定到ViewModel中的observableCollection中 我写的是 this.OneWayBind(ViewModel, x => x.OutletListing, x => x.List).DisposeWith(SubscriptionDisp
this.OneWayBind(ViewModel, x => x.OutletListing, x => x.List).DisposeWith(SubscriptionDisposables);
但是,
System.ArgumentException:无法转换
System.Collections.ObjectModel.ObservableCollection1到
Android.Widget.ListView。要解决此问题,请注册IBindingTypeConverter
我在教程Xamarin中看到表单为此使用了ItemSource属性
谁能给我一个解决办法。
提前谢谢
更新
我不知道如何继续给出答案。我想进一步弄清楚这一点
这是我的ViewModel课程
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using ReactiveUI;
using System.Collections.ObjectModel;
using System.Reactive.Linq;
using Splat;
using System.Reactive.Disposables;
using System.Threading.Tasks;
namespace DistributrIII.Mobile.Droid.ViewModels
{
public class StockTakeVM : ReactiveObject
{
protected Lazy<CompositeDisposable> ViewModelBindings = new Lazy<CompositeDisposable>(() => new CompositeDisposable());
public void RegisterObservables()
{
StockItemListing = new ReactiveList<StockItemListingResult>();
this.LoadStockItems = ReactiveCommand.CreateFromTask<FilterParams, List<StockItemListingResult>>(
async filter =>
{
System.Diagnostics.Debug.WriteLine("Load StockItemListing #1");
var r = await GetStockItemListing(filter);
return r;
},
Observable.Return(true))
.DisposeWith(ViewModelBindings.Value);
this.LoadStockItems.ThrownExceptions
.Subscribe(ex =>
{
System.Diagnostics.Debug.WriteLine("Load StockItemListing Failed");
});
this.LoadStockItems
.ObserveOn(RxApp.MainThreadScheduler)
.Subscribe(result =>
{
StockItemListing.Clear();
foreach (var item in result)
StockItemListing.Add(item);
});
}
async Task<List<StockItemListingResult>> GetStockItemListing(FilterParams filter)
{
List<StockItemListingResult> items = new List<StockItemListingResult>() {
new StockItemListingResult
{
StockItemName = "PORK SAUSAGES (ECONOMY) 500G",
StockItemCode = "CODE: J3103386",
StockItemAmt = "150"
},
new StockItemListingResult
{
StockItemName = "COLLAR BACON 500G",
StockItemCode = "CODE: J3155667",
StockItemAmt = "152"
},
new StockItemListingResult
{
StockItemName = "COLLAR BACON 1KG",
StockItemCode = "CODE: J2344545",
StockItemAmt = "200"
},
new StockItemListingResult
{
StockItemName = "PORK CHIPPOLATAS 1KG",
StockItemCode = "CODE: J31038779",
StockItemAmt = "378"
},
new StockItemListingResult
{
StockItemName = "PORK SAUSAGES (ECONOMY) 500G",
StockItemCode = "CODE: J3103386",
StockItemAmt = "23"
},
new StockItemListingResult
{
StockItemName = "PORK SAUSAGES (ECONOMY) 500G",
StockItemCode = "CODE: J3103386",
StockItemAmt = "454"
},
new StockItemListingResult
{
StockItemName = "COLLAR BACON 500G",
StockItemCode = "CODE: J3155667",
StockItemAmt = "123"
},
new StockItemListingResult
{
StockItemName = "COLLAR BACON 1KG",
StockItemCode = "CODE: J2344545",
StockItemAmt = "675"
},
new StockItemListingResult
{
StockItemName = "PORK CHIPPOLATAS 1KG",
StockItemCode = "CODE: J31038779",
StockItemAmt = "11"
},
new StockItemListingResult
{
StockItemName = "PORK SAUSAGES (ECONOMY) 500G",
StockItemCode = "CODE: J3103386",
StockItemAmt = "34"
}
};
return items;
}
// Observable Properties
ReactiveList<StockItemListingResult> _stockItemListing;
public ReactiveList<StockItemListingResult> StockItemListing
{
get { return _stockItemListing; }
private set { this.RaiseAndSetIfChanged(ref _stockItemListing, value); }
}
ReactiveCommand<FilterParams, List<StockItemListingResult>> _loadStockItems;
public ReactiveCommand<FilterParams, List<StockItemListingResult>> LoadStockItems
{
get { return _loadStockItems; }
private set { this.RaiseAndSetIfChanged(ref _loadStockItems, value); }
}
public StockTakeVM()
{
RegisterObservables();
}
}
public class StockItemListingResult : ReactiveObject
{
Guid _stockItemId;
public Guid Id
{
get { return _stockItemId; }
set { this.RaiseAndSetIfChanged(ref _stockItemId, value); }
}
string _stockItemName;
public string StockItemName
{
get { return _stockItemName; }
set { this.RaiseAndSetIfChanged(ref _stockItemName, value); }
}
string _stockItemCode;
public string StockItemCode
{
get { return _stockItemCode; }
set { this.RaiseAndSetIfChanged(ref _stockItemCode, value); }
}
string _stockItemAmt;
public string StockItemAmt
{
get { return _stockItemAmt; }
set { this.RaiseAndSetIfChanged(ref _stockItemAmt, value); }
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统诊断;
使用Android.App;
使用Android.Content;
使用Android.OS;
使用Android.Runtime;
使用Android.Views;
使用Android.Widget;
使用ReactiveUI;
使用System.Collections.ObjectModel;
使用System.Reactive.Linq;
使用Splat;
使用系统反应性一次性用品;
使用System.Threading.Tasks;
命名空间DistributrIII.Mobile.Droid.ViewModels
{
公共类StockTakeVM:反应对象
{
受保护的惰性ViewModelBindings=new Lazy(()=>new CompositeDisposable());
public void RegisterObservables()
{
StockItemListing=新的反应列表();
this.LoadStockItems=ReactiveCommand.CreateFromTask(
异步筛选器=>
{
System.Diagnostics.Debug.WriteLine(“加载StockItemList#1”);
var r=等待GetStockItemList(过滤器);
返回r;
},
可观察。返回(真)
.DisposeWith(ViewModelBindings.Value);
this.LoadStockItems.ThrownExceptions
.订阅(ex=>
{
System.Diagnostics.Debug.WriteLine(“加载StockItemListing失败”);
});
这是LoadStockItems
.ObserveOn(RxApp.MainThreadScheduler)
.订阅(结果=>
{
StockItemListing.Clear();
foreach(结果中的var项目)
StockItemListing.Add(项目);
});
}
异步任务GetStockItemListing(FilterParams筛选器)
{
列表项=新列表(){
新StockItemListingResult
{
StockItemName=“猪肉香肠(经济型)500G”,
StockItemCode=“代码:J3103386”,
StockItemAmt=“150”
},
新StockItemListingResult
{
StockItemName=“领培根500G”,
StockItemCode=“代码:J3155667”,
StockItemAmt=“152”
},
新StockItemListingResult
{
StockItemName=“1千克”,
StockItemCode=“代码:J2344545”,
StockItemAmt=“200”
},
新StockItemListingResult
{
StockItemName=“PORK CHIPPOLATAS 1KG”,
StockItemCode=“代码:J31038779”,
StockItemAmt=“378”
},
新StockItemListingResult
{
StockItemName=“猪肉香肠(经济型)500G”,
StockItemCode=“代码:J3103386”,
StockItemAmt=“23”
},
新StockItemListingResult
{
StockItemName=“猪肉香肠(经济型)500G”,
StockItemCode=“代码:J3103386”,
StockItemAmt=“454”
},
新StockItemListingResult
{
StockItemName=“领培根500G”,
StockItemCode=“代码:J3155667”,
StockItemAmt=“123”
},
新StockItemListingResult
{
StockItemName=“1千克”,
StockItemCode=“代码:J2344545”,
StockItemAmt=“675”
},
新StockItemListingResult
{
StockItemName=“PORK CHIPPOLATAS 1KG”,
StockItemCode=“代码:J31038779”,
StockItemAmt=“11”
},
新StockItemListingResult
{
StockItemName=“猪肉香肠(经济型)500G”,
StockItemCode=“代码:J3103386”,
StockItemAmt=“34”
}
};
退货项目;
}
//可观测特性
ReactiveList_StockItemList;
公共反应列表StockItemListing
{
获取{return\u stockItemListing;}
私有集{this.RaiseAndSetIfChanged(ref _stockItemListing,value);}
}
ReactiveCommand_loadStockItems;
公共反应命令LoadStockItems
{
获取{return\u loadStockItems;}
私有集{this.RaiseAndSetIfChanged(ref _loadStockItems,value);}
}
公共存货
{
RegisterObservables();
}
}
公共类StockItemListingResult:Rea
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using ReactiveUI;
using Android.Support.V4.Widget;
using Android.Support.Design.Widget;
using DistributrIII.Mobile.Droid.ViewModels;
using DistributrIII.Mobile.Droid.Util;
using System.Reactive.Disposables;
using DistributrIII.Mobile.Droid.Models;
using Android.Support.V7.Widget;
using Android.Support.V7.App;
using Splat;
using Android.Support.V4.View;
using System.Reactive.Linq;
namespace DistributrIII.Mobile.Droid.Views.StockTake
{
[Activity(Label = "StockTakeActivity", MainLauncher = true, Theme = "@style/MainTheme")]
public class StockTakeActivity : DistributrBaseActivity<StockTakeVM>
{
private Android.Support.V7.Widget.SearchView _searchView;
public ListView List { get; private set; }
ReactiveList<StockItemListingResult> StockListItems;
StockTakeScreenAdapter osadapter;
List<StockItemModel> items = new List<StockItemModel>();
public StockTakeActivity()
{
this.ViewModel = new StockTakeVM();
this.StockListItems = new ReactiveList<StockItemListingResult>();
}
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Activity_StockTake);
this.Bind(ViewModel, x => x.StockItemListing, x => x.StockListItems);
//checking whether change happens
// StockListItems.ItemChanged
// .Where(x => x.PropertyName == "StockItemAmt" && x.Sender.StockItemAmt)
// .Select(x => x.Sender)
// .Subscribe(x => {
// Console.WriteLine("Make sure to save {0}!", x.DocumentName);
//});
this.WhenAnyValue(view => view.ViewModel.StockItemListing)
.Where(listing => listing != null)
.Select(listing => new DistributrIII.Mobile.Droid.Util.ReactiveListAdapter<StockTakeVM>(listing, Resource.Layout.Activity_StockTake))
.BindTo(this, view => view.List.Adapter);
this.ViewModel.LoadStockItems.Execute(new FilterParams
{
NameFilter = "",
Status = "All"
}).Subscribe();
//List = FindViewById<ListView>(Resource.Id.List);
SetupReactiveLists(this);
var toolbarST = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbarST);
toolbarST.InflateMenu(Resource.Menu.StockTakeSearch);
toolbarST.Title = "Stock Take";
}
public void SetupReactiveLists(Activity context)
{
List = FindViewById<ListView>(Resource.Id.List);
foreach (var item in StockListItems)
{
StockItemModel stockitem = new StockItemModel
{
StockItemName = item.StockItemName,
StockItemCode = item.StockItemCode
};
items.Add(stockitem);
}
osadapter = new StockTakeScreenAdapter(this, items);
List.Adapter = osadapter;
List.ItemClick += OnListItemClick;
}
}
public class StockTakeScreenAdapter : BaseAdapter<StockItemModel>
{
List<StockItemModel> items;
Activity context;
public StockTakeScreenAdapter(Activity context, List<StockItemModel> items) : base()
{
this.context = context;
this.items = items;
}
public override long GetItemId(int position)
{
return position;
}
public override StockItemModel this[int position]
{
get { return items[position]; }
}
public override int Count
{
get { return items.Count; }
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
var item = items[position];
View view = convertView;
if (view == null)
view = context.LayoutInflater.Inflate(Resource.Layout.ViewCell_StockTake, null);
view.FindViewById<TextView>(Resource.Id.stockitem_name).Text = item.StockItemName;
view.FindViewById<TextView>(Resource.Id.stockitem_code).Text = item.StockItemCode;
view.FindViewById<TextView>(Resource.Id.stockitem_amt).Text = item.StockItemAmt;
return view;
this.WhenAnyValue (view => view.ViewModel.OutletListing)
.Where (listing => listing != null)
.Select (listing => new ReactiveListAdapter (listing, MyViewCreator))
.BindTo (this, view => view.List.Adapter);