【发布时间】:2015-01-01 04:00:23
【问题描述】:
我会显示一个帖子列表,如果我点击帖子中的编辑按钮,则会弹出一个警告对话框来编辑帖子。
当我单击列表视图中的按钮时,我尝试绑定当前项目,但我不知道如何访问它。
这是我的代码:
视图模型
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>
有人知道如何在绑定中发送当前项目吗? 我是否可以从 ViewModel 启动一个警报对话框,或者我必须在后面的代码中创建一个 onclick 方法。
谢谢。
【问题讨论】:
-
OpenEditDialog将是您的列表项视图模型中的一个命令 - 即在您的Post中 - 所以它已经知道哪个Post正在被编辑? -
我想我发现了我的错误:我无法访问模板中的 ViewModel,所以我必须像这篇文章 stackoverflow.com/questions/23535943/… 中那样使用 MvxMessage 传递它。对吗?
-
@MaxDOT 取决于。如果您的命令正在子 ViewModel 中处理,那么正如 Stuart 所说,您已经可以访问它,因为您在 Post 的上下文中。但是,我的答案(您的链接)是当您想要在父 ViewModel 中处理命令时。这就是 MvxMessenger 派上用场的地方。
-
我的命令必须在父 ViewModel 中处理,所以我将使用 MvxMessenger。感谢您的宝贵时间。
标签: android binding xamarin mvvmcross android-alertdialog