简体   繁体   中英

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. Based on N-28 I can do this for Android, but how to do it for Windows phone 8?

Core: ServerViewModel.cs

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

Android: Setup.cs:

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

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

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). After you've done that, then you can add custom bindings and use them in Bi.nd statements in your xaml.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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