【发布时间】:2013-01-22 15:45:41
【问题描述】:
这是关于MvvmCross Android Dialog Bing Programmatically的帖子的跟进
我已经在一个 Droid 项目中实现了对话框的绑定:
this.Root = new RootElement("Customer Info")
{
new Section("Private Configuration")
{
new EntryElement("Pin:").Bind(this, "{'Value':{'Path':'Configuration.Pin'}}"),
new EntryElement("Name:").Bind(this, "{'Value':{'Path':'Configuration.Name', 'Mode':'TwoWay'}}"),
};
};
我在Configuration.Name 绑定中添加了TwoWay,仅用于测试目的。
现在的问题是绑定只在 OneWay 中起作用。 如果我更改视图中的某些内容,则该对象不会更新,但如果对象发生更改,则会通知视图。这发生在上述两种绑定中(在绑定 Mode 中有或没有 TwoWay)。
这是唯一剩下的一个完整的 Droid.Dialog 项目,使用绑定和多个视图,由 viewModels 控制,使用 MvvmCross 框架。
根据我能够调试的情况(在 VS2010 中只有 Droid 代码,没有 PCL),每次我更改 EntryElement 中的文本时,都会调用 OnTextChanged 方法并且属性 Value 正在更新。
EntryElement.cs
public virtual void OnTextChanged(string newText)
{
//Log.Info("Just playing","New text:" + newText);
OnUserValueChanged(newText);
}
ValueElement.cs
protected void OnUserValueChanged(TValueType newValue)
{
Value = newValue;
FireValueChanged();
}
protected virtual void FireValueChanged()
{
var handler = ValueChanged;
if (handler != null)
handler(this, EventArgs.Empty);
}
这是我在 core 和 droid 项目中的代码
核心
BaseViewModel.cs
public class BaseViewModel : MvxViewModel, IMvxServiceConsumer
{
protected IConfigurationDataStore ConfigDataStore
{
get
{
if (_configDataStore == null)
_configDataStore = this.GetService<IConfigurationDataStore>();
return _configDataStore;
}
}
private IConfigurationDataStore _configDataStore;
}
EditConfigurationViewModel.cs
public class EditConfigurationViewModel : BaseViewModel, IEditConfigurationViewModel
{
public ConfigurationSet Configuration
{
get { return _configuration; }
set
{
if (_configuration != value)
{
_configuration = value;
RaisePropertyChanged(() => Configuration);
}
}
}
private ConfigurationSet _configuration;
public EditConfigurationViewModel(string id)
{
Guid value;
if (string.IsNullOrEmpty(id) || !Guid.TryParse(id, out value))
{
Configuration = new ConfigurationSet();
}
else
{
Configuration = ConfigDataStore.GetConfiguration(value);
}
}
public void SaveConfiguration()
{
ConfigDataStore.UpdateConfiguration(Configuration);
}
}
ConfigurationSet.cs
public class ConfigurationSet : MvxNotifyPropertyChanged
{
public string Pin
{
get { return _pin; }
set
{
if (_pin != value)
{
_pin = value;
RaisePropertyChanged(() => Pin);
}
}
}
private string _pin;
public string Name
{
get { return _name; }
set
{
//if (_name != value)
//{
_name = value;
RaisePropertyChanged(()=> Name);
//}
}
}
private string _name;
public string PrivateDescription
{
get { return _privateDescription; }
set
{
if (_privateDescription != value)
{
_privateDescription = value;
RaisePropertyChanged(() => PrivateDescription);
}
}
}
private string _privateDescription;
}
机器人
EditConfigurationView
public class EditConfigurationView : MvxBindingDialogActivityView<EditConfigurationViewModel>, IMvxServiceConsumer
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
DroidResources.Initialise(typeof(Resource.Layout));
Root = new RootElement()
{
new Section("Private Configuration")
{
new EntryElement("Pin:").Bind(this, "{'Value':{'Path':'Configuration.Pin'}}"),
new EntryElement("Name:").Bind(this, "{'Value':{'Path':'Configuration.Name'}}"),
new EntryElement("Description:").Bind(this, "{'Value':{'Path':'Configuration.PrivateDescription'}}")
}
};
}
public override void OnBackPressed()
{
ViewModel.SaveConfiguration();
base.OnBackPressed();
}
protected override void OnViewModelSet()
{
}
}
【问题讨论】:
标签: binding notifications viewmodel mvvmcross