我显示帖子列表,如果我点击帖子中的编辑按钮,弹出提醒对话框以编辑帖子。MVVMCross在MvxListView中获取当前项目
我尝试绑定当前项目,当我点击我的列表视图中的按钮,但我不知道如何访问它。
这是我的代码:
视图模型
private Post _theCurrentPost;
public Post TheCurrentPost
{
get { return _theCurrentPost; }
set
{
_theCurrentPost = value;
RaisePropertyChanged(() => TheCurrentPost);
}
}
private MvxCommand _openEditDialog;
public ICommand OpenEditDialog
{
get
{
_openShareDialog = _openShareDialog ?? new MvxCommand(LoadCurrentSharedPost);
return _openShareDialog;
}
}
private void LoadCurrentSharedPost(Post cur)
{
TheSharedPost = cur;
}
Main.xmla
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res/Project"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/content_frame">
<Mvx.MvxListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/ListView"
local:MvxBind="ItemsSource PostsList; ItemClick ShowDetails"
local:MvxItemTemplate="@layout/posttemplate" />
</LinearLayout>
PostTemplate.xmla
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res/Project"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/fond_post"
android:paddingTop="10dp">
<TextView
android:id="@+id/Title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
local:MvxBind="Text Title"/>
<TextView
android:id="@+id/Text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/Title"
android:layout_alignParentLeft="true"
local:MvxBind="Text Content"/>
<Button
android:id="@+id/Edit"
android:layout_width="50dp"
android:layout_height="50dp"
local:MvxBind="Click OpenEditDialog"
android:background="@drawable/edit_button" />
</RelativeLayout>
</LinearLayout>
是否有人知道如何发送当前项目在宾迪ng? 我可以从ViewModel启动一个警告对话框,或者我必须在后面的代码中创建一个onclick方法。
谢谢。
'OpenEditDialog'将是你的列表项视图模型中的命令 - 在您'POST'即 - 所以它已经知道哪些'POST'正在编辑? – Stuart 2014-11-05 12:46:14
我想我发现我的错误:我无法访问模板中的ViewModel,所以我必须通过它像这篇文章中的MvxMessage http://stackoverflow.com/questions/23535943/bind-button-click- inside-customlayout-using-mvvmcross-and-mvxlistview。这是对的吗 ? – MaxDOT 2014-11-05 15:31:51
@MaxDOT取决于。如果您的命令是在ViewModel子窗体中处理的,那么正如Stuart所说,您已经可以访问它,因为您处于Post的上下文中。但是,我的答案(即你的链接)是当你想处理父ViewModel中的命令。这就是MvxMessenger派上用场的地方。 – Kiliman 2014-11-05 20:11:55