【问题标题】:MVVMCross Calling function from the ViewModel to the View Xamarin从 ViewModel 到 View Xamarin 的 MVVMCross 调用函数
【发布时间】:2014-11-10 03:38:53
【问题描述】:

我想在有人单击按钮时显示一个弹出窗口,该按钮位于从 web 服务加载的列表视图中,我如何向视图发送消息以调用 AlertDialog 函数?

我可能不太清楚,我将代码粘贴到下面看看我尝试了什么:

在我的视图模型中:

public void editPost(Post item)
{
    PostToEdit = item;

    // Call the popup function
}

在我看来:

public Dialog showEditPopup()
{
    var customView = LayoutInflater.Inflate(Resource.Layout.EditDialog, null);

    var builder = new AlertDialog.Builder(this);
    builder.SetView(customView);
    builder.SetPositiveButton("Save", SaveClicked);
    builder.SetNegativeButton("Cancel", CancelClicked);

    return builder.Create();  
}

我尝试创建一个 onclick 函数来在视图中初始化我的 AlertDialog

var editButton = FindViewById<Button>(Resource.Id.editButton);
editButton.Click += delegate { ShowDialog(EditDialog); }; 

但应用程序崩溃,因为在 oncreate 时未加载帖子,因此 editButton 为 null 并且无法设置事件单击,所以我想在 viewmodel 中创建弹出窗口。

【问题讨论】:

  • 您尝试过任何建议的方法吗?
  • 是的,但最后在我的情况下使用 AlertDialog 并不是最好的方法,我必须在 Codebehind 和 ViewModel 之间以两种方式传递很多消息,所以我在查看并将参数绑定到VM。非常感谢您抽出宝贵的时间,您让我明白我的实施方式是错误的

标签: c# android xamarin mvvmcross android-alertdialog


【解决方案1】:

解决此问题的一种方法是(从视图中)在 VM 上设置 ActionFunc 回调,这将显示对话框,并且可以从 VM 启动。
另一种选择是由 VM 定义一个 View 将实现的接口(例如 IViewInteraction,它有一个类似 ShowDialog(string text) 的方法。

我建议第一种方法。

【讨论】:

【解决方案2】:

您可能需要在应用中显示警告对话框的地方不止一处。

在 MvvmCross 中有一个插件可以在所有平台上显示对话框。您实现从视图模型调用它,它将适用于所有平台。

https://github.com/brianchance/MvvmCross-UserInteraction

我建议你通过 NuGet 来添加它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-10
    • 1970-01-01
    • 2014-08-07
    • 1970-01-01
    相关资源
    最近更新 更多