简体   繁体   English

mvvmcross Windows Phone 8自定义绑定

[英]mvvmcross windows phone 8 custom binding

I want to do a custom binding. 我想做一个自定义绑定。 When a property in the the ServerViewModel changes, I want to call a function in the corresponding ServerView. 当ServerViewModel中的属性更改时,我想在相应的ServerView中调用一个函数。 Based on N-28 I can do this for Android, but how to do it for Windows phone 8? 基于N-28,我可以在Android上执行此操作,但如何在Windows Phone 8上执行此操作?

Core: ServerViewModel.cs 核心:ServerViewModel.cs

private bool _textUpdate;
public bool TextUpdate
{
  get { return _textUpdate; }
  set
  {
 _  textUpdate = value;
    if (value) {
      RaisePropertyChanged(() => TextUpdate);
    }
  }
}

Android: Setup.cs: Android:Setup.cs:

protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry registry)
{
  registry.RegisterCustomBindingFactory<ServerView>(
      "SERVERVIEW",
      dcs => new ServerViewTargetBinding(dcs));
  base.FillTargetFactories(registry);

Android ServerViewTargetBinding.cs Android ServerViewTargetBinding.cs

public class ServerViewTargetBinding : MvxAndroidTargetBinding
{
  public ServerViewTargetBinding(ServerView target)
  : base(target)
  {
    //only one way target.MyCountChanged += TargetOnMyCountChanged;
  }

  protected override void SetValueImpl(object target, object value)
  {
    throw new NotImplementedException();
  }

  public override void SetValue(object value)
  {
    var target = Target as ServerView;

    if (target == null)
     return;

    target.ServerCallback((bool)value);
  }

  public override Type TargetType
  {
    get { return typeof(ServerView); }
  }

  public override MvxBindingMode DefaultMode
  {
    get { return MvxBindingMode.OneWay; }
  }
}

Android ServerView.cs Android ServerView.cs

set.Bind(this).For("SERVERVIEW").To(vm => vm.TextUpdate);

public void ServerCallback(bool value)
{
  if (_isUpdating)
    return;

  _isUpdating = true;
  try{
    _text.SetText(value);
  }
  finally{
    _isUpdating = false;
  }
}

If you want to use mvvmcross bindings - including custom bindings - in windows, then you need to include the bindingex packages - see the n=35 video for an example in WindowsStore (phone is similar). 如果要在Windows中使用mvvmcross绑定(包括自定义绑定),则需要包括bindingex软件包-有关WindowsStore中的示例,请参见n = 35视频(电话类似)。 After you've done that, then you can add custom bindings and use them in Bi.nd statements in your xaml. 完成之后,您可以添加自定义绑定,并在xaml的Bi.nd语句中使用它们。

Alternatively, you might be able to achieve your required effect using Attached Properties - see http://msdn.microsoft.com/en-us/library/ms749011(v=vs.110).aspx 另外,您也许可以使用附加属性来达到所需的效果-请参阅http://msdn.microsoft.com/zh-cn/library/ms749011(v=vs.110).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM