Chr*_*ris 2 c# android mvvmcross
我有一个包含按钮的模板的列表视图。当按钮被点击时,我希望触发一个事件并返回一个列表视图行的值,这样我就可以用它来将它添加到数据库中。我的问题是,我不知道如何将我的 buttonevent 绑定到 itemtemplate。我已经尝试了一些方法,但到目前为止没有成功。
我的列表视图:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Mvx.MvxListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#aeaeae"
android:dividerHeight="1px"
local:MvxBind="ItemsSource MenuCollection; ItemClick OrderBtnClick"
local:MvxItemTemplate="@layout/listitem_menuitem" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我的项目模板:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Mvx.MvxImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
local:MvxBind="ImageUrl ImageUrl" />
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:textSize="40dp"
local:MvxBind="Text Name" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:textSize="20dp"
local:MvxBind="Text ShortDescription" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:minWidth="25px"
android:minHeight="25px">
<Button
android:layout_width="wrap_content"
android:layout_height="70dip"
android:layout_alignParentRight="true"
android:layout_marginTop="20dip"
android:layout_marginRight="20dip"
android:layout_gravity="right|center_vertical"
android:text="Bestel"
android:id="@+id/button1" />
</LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我的视图模型:
public class ListPresentationViewModel: MvxViewModel
{
private readonly ISQLService _sqlSvc;
public ListPresentationViewModel (ISQLService sqlService)
{
_sqlSvc = sqlService;
MenuCollection = _sqlSvc.MenuItemGetAll ();
}
private List<MenuItem> _menuCollection = new List<MenuItem> ();
public List<MenuItem> MenuCollection {
get{ return _menuCollection;}
set {
_menuCollection = value;
RaisePropertyChanged (() => MenuCollection);
}
}
private IMvxCommand _orderBtnClick;
public IMvxCommand OrderBtnClick{
get{
_orderBtnClick = _orderBtnClick ?? new MvxCommand(btnClick);
return _orderBtnClick;}
}
private void btnClick()
{
//Do Something
}
}
Run Code Online (Sandbox Code Playgroud)
我将 local:MvxBind="Click OrderBtnClick" 放在模板和列表视图中的按钮上。当我从 itemtemplate 中删除按钮时,ItemClick 似乎工作,但这不是我要找的。我希望按钮触发事件。任何人都可以指出我正确的方向吗?
更新:
我已经尝试了在这里发布的第二个建议 stuart Lodge 。这是我的包装类:
public class MenuItemWrap
{
MenuItem _mnuItem;
ListPresentationViewModel _parent;
public MenuItemWrap ()
{
}
public MenuItemWrap (MenuItem item, ListPresentationViewModel parent)
{
_mnuItem = item;
_parent = parent;
}
public IMvxCommand Click {
get {
return new MvxRelayCommand (() => _parent.btnClick(WrapConverter.ConvertToWrapMenuItem(_mnuItem, _parent)));
}
}
public MenuItem Item{ get { return _mnuItem; } }
}
Run Code Online (Sandbox Code Playgroud)
我的视图模型:
public class ListPresentationViewModel: MvxViewModel
{
private readonly ISQLService _sqlSvc;
public ListPresentationViewModel (ISQLService sqlService)
{
_sqlSvc = sqlService;
MenuCollection = WrapConverter.ConvertToWrapperClass(_sqlSvc.MenuItemGetAll (), this);
}
private List<MenuItemWrap> _menuCollection = new List<MenuItemWrap> ();
public List<MenuItemWrap> MenuCollection {
get{ return _menuCollection;}
set {
_menuCollection = value;
RaisePropertyChanged (() => MenuCollection);
}
}
private IMvxCommand _orderBtnClick;
public IMvxCommand OrderBtnClick{
get{
_orderBtnClick = _orderBtnClick ?? new MvxCommand<MenuItemWrap> (btnClick);
return _orderBtnClick;
}
}
public void btnClick(MenuItemWrap item)
{
MenuCollection.Clear ();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的模板:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Mvx.MvxImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
local:MvxBind="ImageUrl Item.ImageUrl" />
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:textSize="40dp"
local:MvxBind="Text Item.Name" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:textSize="20dp"
local:MvxBind="Text Item.ShortDescription" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:minWidth="25px"
android:minHeight="25px">
<Button
android:layout_width="wrap_content"
android:layout_height="70dip"
android:layout_alignParentRight="true"
android:layout_marginTop="20dip"
android:layout_marginRight="20dip"
android:layout_gravity="right|center_vertical"
android:text="Bestel"
local:MvxBind="Click btnClick.OrderBtnClick"
android:id="@+id/button1" />
</LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我的列表视图完美运行。所有属性都正确绑定,我可以看到名称、简短描述和图像。不起作用的是按钮单击。在我的应用程序输出中,我收到一条错误消息:MvxBind:Warning: 76.06 Unable to bind: source property source not found Cirrious.MvvmCross.Binding.Parse.PropertyPath.PropertyTokens.MvxPropertyNamePropertyToken on MenuItemWrap
我尝试了几种方法来修复它,但没有成功。我会提到我在 MvvMCross 程序集中没有找到 RelayCommand 类,所以我将代码从这里复制粘贴到我的项目中。
我找到了解决方案。问题是点击绑定。您应该只引用包装类中的操作,而不是两者。这是我的包装类和列表视图项模板。
项目模板:
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:minWidth="25px"
android:minHeight="25px">
<Button
android:layout_width="wrap_content"
android:layout_height="70dip"
android:layout_alignParentRight="true"
android:layout_marginTop="20dip"
android:layout_marginRight="20dip"
android:layout_gravity="right|center_vertical"
android:text="Bestel"
local:MvxBind="Click OrderClick" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
包装类:
public class MenuItemWrap
{
MenuItem _mnuItem;
ListPresentationViewModel _parent;
public MenuItemWrap (MenuItem item, ListPresentationViewModel parent)
{
_mnuItem = item;
_parent = parent;
}
public IMvxCommand OrderClick {
get {
return new MvxCommand (() => _parent.btnClick (_mnuItem));
}
}
public MenuItem Item{ get { return _mnuItem; } }
}
Run Code Online (Sandbox Code Playgroud)
我的视图模型:
public class ListPresentationViewModel: MvxViewModel
{
private readonly ISQLService _sqlSvc;
public ListPresentationViewModel (ISQLService sqlService)
{
_sqlSvc = sqlService;
MenuCollection = WrapConverter.ConvertToWrapperClass (_sqlSvc.MenuItemGetAll(), this);
}
private int _catId;
public int CategorieId {
get{ return _catId;}
set{
_catId = value;
ChangeMenuCollection ();
}
}
private void ChangeMenuCollection()
{
MenuCollection = WrapConverter.ConvertToWrapperClass (_sqlSvc.MenuItemByCategorie (_catId), this);
}
private List<MenuItemWrap> _menuCollection = new List<MenuItemWrap> ();
public List<MenuItemWrap> MenuCollection {
get{ return _menuCollection;}
set {
_menuCollection = value;
RaisePropertyChanged (() => MenuCollection);
}
}
private IMvxCommand _orderBtnClick;
public IMvxCommand OrderBtnClick {
get {
_orderBtnClick = _orderBtnClick ?? new MvxCommand<MenuItem> (btnClick);
return _orderBtnClick;
}
}
public void btnClick (MenuItem item)
{
//Do Something
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10217 次 |
| 最近记录: |