2014-11-05 71 views
0

我显示帖子列表,如果我点击帖子中的编辑按钮,弹出提醒对话框以编辑帖子。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方法。

谢谢。

+1

'OpenEditDialog'将是你的列表项视图模型中的命令 - 在您'POST'即 - 所以它已经知道哪些'POST'正在编辑? – Stuart 2014-11-05 12:46:14

+0

我想我发现我的错误:我无法访问模板中的ViewModel,所以我必须通过它像这篇文章中的MvxMessage http://stackoverflow.com/questions/23535943/bind-button-click- inside-customlayout-using-mvvmcross-and-mvxlistview。这是对的吗 ? – MaxDOT 2014-11-05 15:31:51

+1

@MaxDOT取决于。如果您的命令是在ViewModel子窗体中处理的,那么正如Stuart所说,您已经可以访问它,因为您处于Post的上下文中。但是,我的答案(即你的链接)是当你想处理父ViewModel中的命令。这就是MvxMessenger派上用场的地方。 – Kiliman 2014-11-05 20:11:55

回答

1

要发送ListItem作为参数,您必须使用“。”作为这里的绑定按钮的问题是根据它的同一主题Using MvxCommand With CommandParameter binding的另一篇文章中,你只需要添加CommandParameter,最终它会看起来像:

<Button 
    android:id="@+id/Edit" 
    android:layout_width="50dp" 
    android:layout_height="50dp" 
    local:MvxBind="Click OpenEditDialog, CommandParameter=." 
    android:background="@drawable/edit_button" /> 

是的,你可以启动一个警告对话框ViewModel。如果您打算使用MvvmCross创建其他项目,我会建议您为包含此服务的应用程序创建一个插件,如果您不知道插件,请点击这里查看http://slodge.blogspot.fr/2012/10/build-new-plugin-for-mvvmcrosss.html或在线查找。至于它的代码是这样的:

//This goes in core 
public interface IAlertService 
{ 
    void CreateAlert(string title, string messagem, etc...); 
} 

//This goes in droid 
public class AlertService : IAlertService 
{ 
    public void CreateAlert(string title, string messagem, etc...) 
    { 
      //Android code for creating AlertDialog 
    } 
} 
+0

我想同样的'本地:MvxBind =“点击OpenEditDialog,CommandParameter =。”'我得到相同的“。”同时执行commmand。有什么想法吗? – RIYAZ 2015-11-17 17:58:02

+0

看看这里:https://stackoverflow.com/questions/22778749/mvxtabsfragment-failed-to-create-target-binding-for-binding-command-for/22779090#22779090 – 2017-11-24 15:11:37