绑定到ViewModel

时间:2018-01-12 23:21:41

标签: c# mvvm data-binding xamarin.ios mvvmcross

我添加了一个没有运气的linkerPleaseInclude。

下面是我目前实施的一些代码,我没看到我做错了什么。

绑定时,以下行会引发异常。

        this.CreateBinding(MyTextView).For(v => v.Text).To<MyViewModel>(vm => vm.QuestionText).Apply();

视图中的财产声明

          MyTextView = new UITextView(new CGRect(0, 0, myWidth, 105))
        {
            TranslatesAutoresizingMaskIntoConstraints = false,
            TextColor = UIColor.Black,
            Font = UIFont.FromName("Avenir-Heavy", 15f),
            TextAlignment = UITextAlignment.Left,
            BackgroundColor = UIColor.Clear,
            //Alpha = .3f,
            ScrollEnabled = true,
            Editable = false,
            IndicatorStyle = UIScrollViewIndicatorStyle.White
        };

继承ViewModel中的财产声明

string _questionText;
public string QuestionText
{
  get { return _questionText; }
  set
  {
    _questionText = value;
    RaisePropertyChanged(() => QuestionText);
  }
 }

这是例外:

  

未处理的异常:System.ArgumentNullException:缺少源   MvxWeakEventSubscription中的事件信息参数名称:sourceEventInfo   在   MvvmCross.Platform.WeakSubscription.MvxWeakEventSubscription 2[TSource,TEventArgs]..ctor (UIKit.NSTextStorage source, System.Reflection.EventInfo sourceEventInfo, System.EventHandler 1 [TEventArgs] targetEventHandler)   [0x00017]在&lt; 6adc0d5857264558a9d45778a78ae02a&gt;中:0 at   MvvmCross.Platform.WeakSubscription.MvxWeakEventSubscription 2[TSource,TEventArgs]..ctor (UIKit.NSTextStorage source, System.String sourceEventName, System.EventHandler 1 [TEventArgs] targetEventHandler)[0x00012] in   &lt; 6adc0d5857264558a9d45778a78ae02a&gt;:0 at   MvvmCross.Platform.WeakSubscription.MvxWeakSubscriptionExtensionMethods.WeakSubscribe [TSource,TEventArgs]   (TSource源代码,System.String eventName,   System.EventHandler 1[TEventArgs] eventHandler) [0x00000] in <6adc0d5857264558a9d45778a78ae02a>:0 at MvvmCross.Binding.iOS.Target.MvxUITextViewTextTargetBinding.SubscribeToEvents () [0x00053] in <614c9ef828c14ba687a40ec2656f480f>:0 at MvvmCross.Binding.Bindings.MvxFullBinding.CreateTargetBinding (System.Object target) [0x00057] in <866b1e46764b48aab0d408952a6f006f>:0 at MvvmCross.Binding.Bindings.MvxFullBinding..ctor (MvvmCross.Binding.MvxBindingRequest bindingRequest) [0x0002f] in <866b1e46764b48aab0d408952a6f006f>:0 at MvvmCross.Binding.Binders.MvxFromTextBinder.BindSingle (MvvmCross.Binding.MvxBindingRequest bindingRequest) [0x00000] in <866b1e46764b48aab0d408952a6f006f>:0 at MvvmCross.Binding.Binders.MvxFromTextBinder+<>c__DisplayClass2_0.<Bind>b__0 (MvvmCross.Binding.Bindings.MvxBindingDescription description) [0x00018] in <866b1e46764b48aab0d408952a6f006f>:0 at System.Linq.Enumerable+SelectArrayIterator 2 [TSource,TResult] .MoveNext   ()[0x0003a]在&lt; 8bc31b0df50a4d32b3f1d5af764165ad&gt;:0 at   MvvmCross.Binding.BindingContext.MvxBindingContextOwnerExtensions.AddBindings   (MvvmCross.Binding.BindingContext.IMvxBindingContextOwner视图,   System.Object target,System.Collections.Generic.IEnumerable 1[T] bindings, System.Object clearKey) [0x0001d] in <866b1e46764b48aab0d408952a6f006f>:0 at MvvmCross.Binding.BindingContext.MvxBindingContextOwnerExtensions.AddBindings (MvvmCross.Binding.BindingContext.IMvxBindingContextOwner view, System.Object target, System.Collections.Generic.IEnumerable 1 [T]   bindingDescriptions,System.Object clearKey)[0x00018] in   &lt; 866b1e46764b48aab0d408952a6f006f&gt;:0 at   MvvmCross.Binding.BindingContext.MvxBindingContextOwnerExtensions.AddBinding   (MvvmCross.Binding.BindingContext.IMvxBindingContextOwner视图,   System.Object目标,MvvmCross.Binding.Bindings.MvxBindingDescription   bindingDescription,System.Object clearKey)[0x0000b] in   &lt; 866b1e46764b48aab0d408952a6f006f&gt;:0 at   MvvmCross.Binding.BindingContext.MvxBaseFluentBindingDescription`1 [t目标]。适用   ()[0x0001f] in&lt; 866b1e46764b48aab0d408952a6f006f&gt;:0 at   BALAHA.iOS.Views.RecordingView.SetupBindings()[0x00418] in   /Users/BALAHA_mbp/Projects/BALAHA/BALAHA/iOS/Views/RecordingView.cs:799   在BALAHA.iOS.Views.RecordingView.ViewDidLoad()[0x0002f] in   /Users/BALAHA_mbp/Projects/BALAHA/BALAHA/iOS/Views/RecordingView.cs:114   at(包装器托管到原生)UIKit.UIApplication:UIApplicationMain   UIKit.UIApplication.Main上的(int,string [],intptr,intptr)   (System.String [] args,System.IntPtr principal,System.IntPtr   代表)[0x00005] in   /Users/builder/data/lanes/5665/f70a1348/source/xamarin-macios/src/UIKit/UIApplication.cs:79   在UIKit.UIApplication.Main(System.String [] args,System.String   principalClassName,System.String delegateClassName)[0x00038] in   /Users/builder/data/lanes/5665/f70a1348/source/xamarin-macios/src/UIKit/UIApplication.cs:63   在BALAHA.iOS.Application.Main(System.String [] args)[0x00001]中   /Users/BALAHA_mbp/Projects/BALAHA/BALAHA/iOS/Main.cs:17 2018-01-12   18:23:28.985 BALAHA.iOS [888:372317]未处理的管理异常:   在MvxWeakEventSubscription中缺少源事件信息

3 个答案:

答案 0 :(得分:1)

将此添加到LinkerPleaseInclude解决了我的问题:

public void Include(UITextView textView)
{
    textView.Text = textView.Text + "";            
    textView.Changed += (sender, args) => { textView.Text = ""; };            
    textView.TextStorage.DidProcessEditing += (sender, e) => textView.Text = "";
}   

答案 1 :(得分:0)

读取错误消息似乎问题是由链接器引起的。您似乎没有在Xamarin.iOS项目中包含LinkerPleaseInclude.cs文件。请参阅其他类似问题hereherehere

在链接过程中,Xamarin.iOS会从二进制文件中删除所有未使用的内容。这意味着从未访问过的字段和属性对于反射是不可见的。 LinkerPleaseInclude.cs的示例可以是found here。它包含最常用的控件及其属性,但您可以添加只会触及链接期间不想丢失的属性的方法。

答案 2 :(得分:0)

由于实施此解决方案的正确方法对我不起作用,我必须执行以下操作:

在视图中:

string _viewModelText;
    public string ViewModelText
    {
        get { return _viewModelText; }
        set
        {
            _viewModelText = value;

            if (_viewModelText != null)
            {
                MyTextView.Text = _viewModelText;
            }
        }
    }

    this.CreateBinding(this).For(v => v.ViewModelText).To<MyViewModel>(vm => vm.QuestionText).OneWay().Apply();

这解决了这个问题,虽然它以一种迂回的方式完成了工作。

更新

虽然上述方法有效但原始异常的原因是ViewModel属性的使用不当。

ViewModel的属性应按如下方式实现:

    string _questionText;
    public string QuestionText
    {
        get { return _questionText; }
        set { SetProperty(ref _questionText, value); }
    }

然后,您可以将Textview的文本属性绑定为:

    this.CreateBinding(MyTextView).For(v => v.Text).To<MyViewModel>(vm => vm.QuestionText).Apply();