如何在Windows Phone上执行CreateBindingSet()? [英] How to do CreateBindingSet() on Windows Phone?

查看:54
本文介绍了如何在Windows Phone上执行CreateBindingSet()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在N + 1视频#34(进度)中,有一个使用Android版本的CreateBindingSet()的示例,这不是典型的例子.但是叙述者还简短地提到可以在Windows平台上完成同样的操作.

In the N+1 video #34 (Progress), there was an example of using CreateBindingSet() for the Android version, which is not typical. But the narrator also mentioned briefly that the same can be done on the Windows platform.

但是,尽管尝试了很多,但我无法在Windows Phone上将View的属性绑定到其ModelView.我总是收到 NullReferenceException .

As much as I tried, however, I am unable to get a View's property to be bound to its ModelView on the Windows Phone. I always get a NullReferenceException.

我最接近的是以下代码,包括ReSharper的建议.这是我的FirstView.xaml.cs:

The closest I came was the code below, including suggestions from ReSharper. Here's my FirstView.xaml.cs:

using Cirrious.MvvmCross.Binding.BindingContext;
using Whatever.ViewModels;

namespace Whatever {

// inheriting from IMvxBindingContextOwner was suggested by ReSharper also

public partial class FirstView : BaseView, IMvxBindingContextOwner {

  public class MyBindableMediaElement
  {
        private string _theMediaSource = "whatever";                                                                                                                     
        public string TheMediaSource
        {
            get
            {
                return _theMediaSource;
            }
            set
            {
                _theMediaSource = value;
            }
        }
   }

  public FirstView()
   {
        InitializeComponent();

        _mediaElement = new MyBindableMediaElement(this.theMediaElement);

        var set = this.CreateBindingSet<FirstView, FirstViewModel>();
        // the corresponding view model has a .SongToPlay property with get/set defined
        set.Bind(_mediaElement).For(v => v.TheMediaSource).To(vm => vm.SongToPlay);
        set.Apply();
   }

    public IMvxBindingContext BindingContext { get; set; }      // this was suggested by ReSharper
}

创建视图后,我会在MvxBaseFluentBindingDescription.cs中得到一个NullReferenceException.确切位置如下:

I get a NullReferenceException in MvxBaseFluentBindingDescription.cs as soon as the view is created. The exact location is below:

    protected static string TargetPropertyName(Expression<Func<TTarget, object>> targetPropertyPath)
    {
        var parser = MvxBindingSingletonCache.Instance.PropertyExpressionParser;        // <----- exception here**
        var targetPropertyName = parser.Parse(targetPropertyPath).Print();
        return targetPropertyName;
    }

我还没有看到在Windows Phone模拟器上创建绑定集的有效示例.有没有人得到这个工作?谢谢.

I have not seen a working example of creating a binding set on a Windows Phone emulator. Has anyone gotten this to work? Thanks.

推荐答案

我可以确认叙述者说的那句话太过分轻描淡写了,而没有真正考虑他可能会怎么做...

I can confirm that the narrator said that remark a little too flippantly without actually thinking about how he might do it...

但是,如果您愿意的话,肯定可以使CreateBindingSet在Windows中工作.

However, with a little effort, you definitely can get the CreateBindingSet to work in Windows if you want to.

在开始之前,请考虑一些替代方法-特别是,我怀疑大多数人将使用Windows DependencyProperty绑定或某些带有PropertyChanged事件订阅的手工编写代码.

Before you start, do consider some alternatives - in particular, I suspect most people will use either Windows DependencyProperty binding or some hand-crafted code-behind with a PropertyChanged event subscription.

如果您确实想将CreateBindingSet代码添加到Windows项目,则:

If you do want to add CreateBindingSet code to a Windows project then:

  1. 将Binding和BindingEx程序集添加到Ui项目中-最简单的方法是使用nuget添加BindingEx程序包.
  2. 在您的Setup类中,重写InitializeLastChance并利用此机会创建MvxWindowsBindingBuilder实例并在该生成器上调用DoRegistration.前两个步骤都在n = 35 Tibet绑定视频中进行了介绍-这是第二步,它将初始化绑定框架并帮助您克服当前的'NullReferenceException'(有关代码,请参见
  1. Add the Binding and BindingEx assemblies to your Ui project - the easiest way to do this is using nuget to add the BindingEx package.
  2. In your Setup class, override InitializeLastChance and use this opportunity to create a MvxWindowsBindingBuilder instance and to call DoRegistration on that builder. Both these first two steps are covered in the n=35 Tibet binding video - and it's this second step that will initialise the binding framework and help you get past your current 'NullReferenceException' (for the code, see BindMe.Store/Setup.cs)
  3. In your view, you'll need to implement the IMvxBindingContextOwner interface and you'll need to ensure the binding context gets created. You should be able to do this as simply as BindingContext = new MvxBindingContext();
  4. In your view, you'll need to make sure the binding context is given the same DataContext (view model) as the windows DataContext. For a Phone Page, the easiest way to do this is probably just to add BindingContext.DataContext = this.ViewModel; to the end of your phone page's OnNavigatedTo method. Both steps 3 and 4 could go in your BaseView if you intend to use Mvx Binding in other classes too.
  5. With this done, you should be able to use the CreateBindingSet code - although do make sure that all binding is done after the new MvxBindingContext() has been created.

我现在没有Windows机器,因此我担心此答案代码未经测试-如果有效或无效,请再次发布.

I've not got a windows machine with me right now so I'm afraid this answer code comes untested - please do post again if it does or doesn't work.

这篇关于如何在Windows Phone上执行CreateBindingSet()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆