【问题标题】:Mvvmcross: launching an android dialog containing a ListViewMvvmcross:启动一个包含 ListView 的 android 对话框
【发布时间】:2013-07-16 18:41:26
【问题描述】:

首先,我要对 Stuart Lodge 这个很棒的框架表示高度赞扬。与 Xamarin 的 Visual Studio 集成一起,这是我接触过的最高效的跨平台框架之一。

我想要实现的是在单击按钮时启动一个包含可选 ListView 的对话框。当用户关闭此对话框时,我需要访问所选项目。在遵循 MVVM 范例的同时,是否有推荐的方法来使用 Mvvmcross 的对话框插件?

我正在使用以下 Activity 来创建对话框。

[Activity(Theme = "@android:style/Theme.Holo.Dialog")]
    public class SearchResultDialogView : MvxActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.SearchResultView);
        }
    }

从另一个视图模型导航到SearchResultDialogViewModel 会将此视图显示为模态。所以看起来我正朝着正确的方向前进。但是,该对话框缺少“确定”和“取消”按钮,我还想摆脱默认标题。认为我需要一个 AlertDialog 但到目前为止我还没有成功使用此代码启动一个:

 [Activity(Theme = "@android:style/Theme.NoTitleBar")]
    public class SearchResultDialogView : MvxActivity
    {
        protected override Dialog OnCreateDialog(int id, Bundle args)
        {
             AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.SetTitle("some title");
            return builder.Create();
        }
    }

如果这个问题含糊不清,我们深表歉意。我是 Android UI 开发的新手。

TIA。

【问题讨论】:

    标签: android mvvm dialog xamarin mvvmcross


    【解决方案1】:

    这里有几种不同的用法。

    考虑到这些...

    如果您想显示一个常规弹出窗口来收集一些数据,那么您可以尝试使用基于片段的对话框来收集数据 - 这在 Fragments HomeView.csNameDialogFragment.cs 中演示(后面有一点代码) - 对于片段的一般背景,在http://mvvmcross.wordpress.com/看N=26

    如果您想使用单独的活动来收集数据,那么@gschackles 写了这篇文章,介绍了从子视图模型返回数据的一种方式 - http://www.gregshackles.com/2012/11/returning-results-from-view-models-in-mvvmcross/ - 我相信也可以使用其他方案。

    如果您确实想了解 Mvx Dialog 插件,请参阅 http://mvvmcross.wordpress.com/ 中的 N=23

    【讨论】:

    • 谢谢斯图尔特。将研究这些替代方案。同样,您的框架是一流的。
    【解决方案2】:

    您可以使用构建器来完成。

    http://developer.android.com/guide/topics/ui/dialogs.html#AddingAList

    代码是:

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(R.string.pick_color);
               .setItems(R.array.colors_array, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int which) {
                   // The 'which' argument contains the index position
                   // of the selected item
               }
        });
        return builder.create();
    }
    

    您可以通过将which 值返回给您的调用者来获取您的元素。

    【讨论】:

    • 看起来这与我在上面所做的类似,尽管我没有填充对话框的内容区域。
    猜你喜欢
    • 1970-01-01
    • 2013-09-07
    • 1970-01-01
    • 1970-01-01
    • 2013-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多