gpt4 book ai didi

binding - 以编程方式 MvvmCross Android Dialog Bing - ViewModel 未更新

转载 作者:行者123 更新时间:2023-12-04 05:14:31 25 4
gpt4 key购买 nike

这是关于 MvvmCross Android Dialog Bing Programmatically 的帖子的后续内容。

我已经在 Droid 项目中实现了 Dialog 的绑定(bind):

    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 bind 仅用于测试目的。

现在的问题是绑定(bind)仅在 OneWay 中有效。对象是 如果我更改 View 中的某些内容,则不会更新 ,但 如果对象发生更改,则通知 View .这发生在上述两种绑定(bind)中(在绑定(bind)模式下使用或不使用 TwoWay)。

这是唯一剩下的一个完整的 Droid.Dialog 项目,使用 MvvmCross 框架,使用由 viewModels 控制的绑定(bind)和多个 View 。

根据我能够调试的内容(在 VS2010 中只有 Droid 代码,没有 PCL),每次我更改 EntryElement 中的文本时,调用 OnTextChanged 方法并更新属性值。

入口元素.cs
    public virtual void OnTextChanged(string newText)
{
//Log.Info("Just playing","New text:" + newText);
OnUserValueChanged(newText);
}

值元素.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);
}
}

配置集.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;
}

机器人

编辑配置查看
    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()
{

}
}

最佳答案

https://github.com/zleao/MvvmCross.Dialog 上看到您的 repo 后的第二个答案

感谢您的附加信息。

我还没有运行您的示例,但是查看重现问题的简单代码很有帮助。

我认为问题可能出在您的安装文件中 - https://github.com/zleao/MvvmCross.Dialog/blob/master/MvvmCross.Dialog.UI.Droid/Setup.cs

那里的安装程序继承自 MvxBaseAndroidBindingSetup这是 Cirrious.MvvmCross.Binding.Droid 中所有内容的基本设置类它本身继承自 MvxBaseAndroidSetup来自 Cirrious.MvvmCross.Droid
由于除了“只是绑定(bind)”之外您还使用对话框代码,因此您需要进一步设置 - 您需要添加 MvxBaseAndroidDialogBindingSetup来自 Cirrious.MvvmCross.Dialog.Droid .此类添加了许多重要步骤,包括为 Value 注册双向绑定(bind)。全部ValueElement实例 - 见:

    protected override void FillTargetFactories(
Cirrious.MvvmCross.Binding.Interfaces.Bindings.Target.Construction.IMvxTargetBindingFactoryRegistry registry)
{
registry.RegisterFactory(new MvxPropertyInfoTargetBindingFactory(typeof (ValueElement), "Value",
(element, propertyInfo) =>
new MvxElementValueTargetBinding(element,
propertyInfo)));
base.FillTargetFactories(registry);
}

https://github.com/slodge/MvvmCross/blob/vnext/Cirrious/Cirrious.MvvmCross.Dialog.Droid/MvxBaseAndroidDialogBindingSetup.cs

所以 - 要尝试解决问题,请尝试从 MvxBaseAndroidDialogBindingSetup 继承设置。

有关 MvvmCross 层的更多信息,请参阅 http://slodge.blogspot.co.uk/2012/12/a-short-guide-to-layers-of-mvvmcross.html

我希望这有助于并解决问题。

感谢您提供的出色细节水平。

但请注意,与 Touch Dialog 代码相比,Droid.Dialog 代码还很年轻——因此您可能会在此过程中遇到真正的错误和问题。当你点击它们时,请在这里提问,或者如果它们是错误,请在 https://github.com/slodge/MvvmCross/issues?state=open 上的问题上记录它们

斯图尔特

关于binding - 以编程方式 MvvmCross Android Dialog Bing - ViewModel 未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14462431/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com