MvvmCross Android Dialog bind programmatically

回眸只為那壹抹淺笑 提交于 2019-12-10 09:43:46

问题


I want to use the Android.Dialog (Cross.UI) in my MvvmCross project. My first approach was to use AutoViews. As this feature is still fairly young, the alternative was to implement the dialog in touch and Droid platforms.

For now i'm just doing this for Droid and I need to programmatically bind the properties of the ViewModel to the elements of the Dialog.

My View and ViewModel code is the following:

View

    public class DialogConfigurationView : MvxBindingDialogActivityView<DialogConfigurationViewModel>
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            DroidResources.Initialise(typeof(Resource.Layout));

            Root = new RootElement()
                {
                    new Section("Private Configuration")
                        {
                            new EntryElement("Name:"),
                            new EntryElement("Description:"),
                            new BooleanElement("Active?")
                        }
                };
        }
    }

ViewModel

    public class DialogConfigurationViewModel : MvxViewModel
    {
        public ConfigurationSet Configuration
        {
            get { return _configuration; }
            set
            {
                if (_configuration != value)
                {
                    _configuration = value;
                    RaisePropertyChanged(() => Configuration);
                }
            }
        }
        private ConfigurationSet _configuration;
    }

My goal is to have a twoway bind the EntryElement("Name:") with the property ViewModel.Configuration.Name.

Can anyone help me with this? Can this be done?


回答1:


I don't know if there are any monodroid.dialog mvvmcross samples floating around which don't use autoviews!

However.... the basic syntac for binding should be the same as MonoTouch.Dialog - e.g. something like:

                            new Section("Contact Info")
                                {
                                    new StringElement("ID", ViewModel.Customer.ID ?? string.Empty),
                                    new EntryElement("Name", "Name").Bind(this, "{'Value':{'Path':'Customer.Name'}}"),
                                    new EntryElement("Website", "Website").Bind(this, "{'Value':{'Path':'Customer.Website'}}"),
                                    new EntryElement("Primary Phone", "Phone").Bind(this, "{'Value':{'Path':'Customer.PrimaryPhone'}}"),
                                },
                            new Section("Primary Address")
                                {
                                    new EntryElement("Address").Bind(this, "{'Value':{'Path':'Customer.PrimaryAddress.Street1'}}"),
                                    new EntryElement("Address2").Bind(this, "{'Value':{'Path':'Customer.PrimaryAddress.Street2'}}"),
                                    new EntryElement("City").Bind(this, "{'Value':{'Path':'Customer.PrimaryAddress.City'}}"),
                                    new EntryElement("State").Bind(this, "{'Value':{'Path':'Customer.PrimaryAddress.State'}}"),
                                    new EntryElement("Zip").Bind(this, "{'Value':{'Path':'Customer.PrimaryAddress.Zip'}}"),
                                },

from https://github.com/slodge/MvvmCross/blob/vnext/Sample%20-%20CustomerManagement/CustomerManagement/CustomerManagement.Touch/Views/BaseCustomerEditView.cs


Note that in MvvmCross bindings for MonoTouch and MonoDroid, the default binding for things like text edit boxes is generally TwoWay by default.


If you do get a sample running, then please feel free to post it to a gist or to a repo - or to blog about it - looks like we could do with some samples to work from!



来源:https://stackoverflow.com/questions/14384229/mvvmcross-android-dialog-bind-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!