これは、プログラムによるMvvmCross AndroidDialogBingに関する投稿のフォローアップです。
DroidプロジェクトにDialogのバインディングを実装しました。
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'}}"),
};
};
テスト目的で、バインドにTwoWayを追加しました。Configuration.Name
現在の問題は、バインドがOneWayでのみ機能していることです。ビューで何かを変更してもオブジェクトは更新されませんが、オブジェクトが変更されるとビューに通知されます。これは、上記の両方のバインドで発生します(バインドモードでのTwoWayの有無にかかわらず)。
これは、MvvmCrossフレームワークを使用して、viewModelsによって制御される、バインドおよび複数のビューで動作する完全なDroid.Dialogプロジェクトを持つために残された唯一のものです。
デバッグできたもの(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);
}
これが私がコアプロジェクトとドロイドプロジェクトに持っているコードです
芯
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()
{
}
}