在 N+1 视频 #34(进度)中,有一个 Android 版本使用 CreateBindingSet() 的示例,并不典型。但叙述者也简单地提到,在Windows平台上也可以做到这一点。
然而,尽管我尝试了很多,但我无法将 View 的属性绑定到 Windows Phone 上的 ModelView。我总是得到一个NullReferenceException。
我最接近的是下面的代码,包括来自 ReSharper 的建议。这是我的 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。具体位置如下:
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 模拟器上创建绑定集的工作示例。有没有人让这个工作?谢谢。