1

我有一种情况,我的视图定义(即显示在布局文件上的内容)是在 json 结构中定义的。我需要能够定义这个 json 结构以及绑定,然后我的代码应该能够基于 json 结构动态创建控件并填写布局。

我可以使用下面的代码完成相同的操作。

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
         FirstViewModel firstViewModel = new FirstViewModel()
        {
            Id = 1001,
            FirstName = "Amit",
            MiddleName = string.Empty,
            LastName = "Taparia",
            Race = "Asian",
            IsUSNational = false
        };

        this.ViewModel = firstViewModel as IMvxViewModel;
        var bindings = this.CreateInlineBindingTarget<FirstViewModel>();                    

        this.Root = new RootElement("Main View", null, null).
        {
            new Section("Personal Information")
            {                                        
                new StringElement("Employee#","Enter Employee#").Bind(bindings,vm => vm.Id)
                new EntryElement("FirstName","Enter First Name").Bind(bindings,vm=>vm.FirstName),
                new EntryElement("MiddleName","Enter Middle Name").Bind(bindings, vm=> vm.MiddleName),
                new EntryElement("LastName","Enter Last Name","Amit",null).Bind(bindings, vm => vm.LastName),
                new EntryElement("Race","Enter Race").Bind(bindings,vm => vm.Race),
                new BooleanElement("US National",true,).Bind(bindings, vm => vm.IsUSNational).                
            },               
        };
    }

但是我需要能够使用 json 文件中定义的结构来做同样的事情。我知道我们可以使用 MvvmCross 做这样的事情,但我不知道该怎么做。

我遇到了这三个示例解决方案 a) CustomerManagement b) CustomerManager.AutoView 在这里我确实看到我们有一个 BaseCustomerEditView.cs,其中定义了一个 json 结构,但代码被注释掉了。c) 对话框示例

几个问题

1) 使用 AutoView 和 MvxDialog 有什么区别?它是一回事吗?

2) 使用 json 文件中定义的控制结构是否有任何潜在的限制。我确实看到了一个与对齐有关的问题。我们无法控制 json 文件中定义的控件的对齐方式。

3)如何使用json文件中定义的结构实现渲染/绑定?

期待您的回复。

4

1 回答 1

0

Dialog 提供了“创建对话框和显示基于表格的信息的基础,而无需为用户界面编写数十个委托和控制器”(来自https://github.com/migueldeicaza/MonoTouch.Dialog

AutoViews 的想法是提供一个主要构建在 Dialog 之上的自动生成的 UI 层,尤其是用于原型设计,但也可能用于某些应用程序系列 - 请参阅我应该等待 AutoView 吗?http://slodge.blogspot.co.uk/2012/10/cross-platform-views-for-mvvmcross-ideas.html

AutoViews 的当前状态是在 Android 和 iOS 上有一个可用的初始实现,并且有一个小示例显示了此实现的用法:https ://github.com/slodge/MvvmCross-Tutorials/tree/master /AutoView 示例

演示的主要重点是使用Auto对象来描述 ViewModel 中的 UI - 例如:

    private KeyedDescription GetDialogAutoView()
    {
        var auto = new RootAuto(caption: "Some Dialog Info")
        {
            new SectionAuto(header: "Strings")
                {
                    new EntryAuto(caption: "Name", bindingExpression: () => Name),
                    new EntryAuto(caption: "Password", bindingExpression: () => Password) { Password = true },
                },
            new SectionAuto(header: "Booleans")
                {
                    new BooleanAuto(caption: "Is available", bindingExpression: () => IsAvailable),
                    new CheckboxAuto(caption: "Is active", bindingExpression: () => IsActive),
                },
            new SectionAuto(header: "DateTimes")
                {
                    new DateAuto(caption: "Date of Birth", bindingExpression: () => DateOfBirth),
                    new TimeAuto(caption: "When", bindingExpression: () => PreferredContactTime),
                },
            new SectionAuto(header: "Debug Info")
                {
                    new StringAuto(caption: "Name", bindingExpression: () => Name),
                    new StringAuto(caption: "Password", bindingExpression: () => Password),
                    new StringAuto(caption: "Is available", bindingExpression: () => IsAvailable),
                    new StringAuto(caption: "Is active", bindingExpression: () => IsActive),
                    new StringAuto(caption: "Date of Birth", bindingExpression: () => DateOfBirth),
                    new StringAuto(caption: "When", bindingExpression: () => PreferredContactTime),
                },
        };

        return auto.ToElementDescription();
    }

代替这些Auto对象,可以使用 Json 格式描述,但今天没有太多关于此的文档。一些示例文件确实存在于https://github.com/slodge/MvvmCross-Tutorials/blob/master/Sample%20-%20CustomerManagement/CustomerManagement%20-%20AutoViews - 例如https://github.com/slodge/ MvvmCross-Tutorials/blob/master/Sample%20-%20CustomerManagement/CustomerManagement%20-%20AutoViews/CustomerManagement.Droid/Assets/DefaultViews/NewCustomerViewModel/Dialog.json

这些 Json 文件非常基于反射 - 它们是围绕 Dialog 类的名称和属性构建的。


对于您的具体情况,我想我建议您不要使用 AutoViews Json 格式。

听起来您有更具体的要求 - 您希望 ViewModel 从 Web 下载模型,然后 ViewModel 从这些描述中构建 UI。

因此,我建议您首先在 C# POCO 中描述您自己的业务模型 - 然后可以通过网络连接轻松序列化这些模型(例如使用 Json.Net),然后您的 ViewModel 和 Views 可以确定如何显示它们。

正如您已经从Is it possible to have method binding in mvvmcross using Fluent API 中知道的那样?,如果您愿意,您可以将您的 UI 绑定到 Dictionary 或其他“动态”ViewModel 结构 - 我怀疑这在显示和绑定动态表单数据时会很有用。

您可能可以使用AutoAutoViews 的 the 或 Json 部分作为最后一个显示步骤的一部分 - 但我认为您的网络传输不应该根据 Dialog 元素来完成 - 坚持那里的业务域对象。

于 2013-07-24T11:21:41.280 回答