在N+1视频#34 (进展)中,提到了一个使用CreateBindingSet()的例子,适用于Android版本,这并不典型。但是叙述者也简要提到了同样可以在Windows平台上实现相同的功能。
然而,尽管我尝试了很多次,但我无法将一个View的属性绑定到它的ModelView上,在Windows Phone上总是会得到NullReferenceException。
我最接近的代码如下,包括来自ReSharper的建议。这是我的FirstView.xaml.cs:
然而,尽管我尝试了很多次,但我无法将一个View的属性绑定到它的ModelView上,在Windows Phone上总是会得到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模拟器上创建绑定集的工作示例。 有人成功做到了吗? 谢谢。