加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – Xamarin与MVVM Light的Android数据绑定

发布时间:2020-12-15 20:58:08 所属栏目:百科 来源:网络整理
导读:原始问题: 尝试使用MVVM Light将我的ViewModel中的值绑定到Xamarin Android中的TextView时遇到问题.值会更改一次然后停止工作.即使对象在ViewModel中更改并且绑定模式设置为默认值(OneWay). 由于业务需求,我需要继续使用MVVM Light和Xamarin Android. 例:
原始问题:

尝试使用MVVM Light将我的ViewModel中的值绑定到Xamarin Android中的TextView时遇到问题.值会更改一次然后停止工作.即使对象在ViewModel中更改并且绑定模式设置为默认值(OneWay).

由于业务需求,我需要继续使用MVVM Light和Xamarin Android.

例:
在我的第一个片段中,我有一个书籍列表,这些书籍被绑定到我的ViewModel中的书籍列表.在我的Viewmodel中,我有一个名为CurrentBook的对象,它随着我列表中的onclick事件而变化.

VM.CurrentBook = Books[index];

在我的第二个片段中,我将标题绑定到TextView

_titleBinding = this.SetBinding(() => VM.CurrentBook.Title,() => TitleTextView.Text);

当前书籍第一次从NULL更改为Book实例时,标题会根据需要更改.第一次更改VM.CurrentBook = Books [index]后;标题与第一本选集书保持一致.

?更新:

我在Milen Pavlov的帮助下尝试过几件事,
我试过换到

VM.SetBinding(() => VM.CurrentBook.Title,TitleTextView,() => TitleTextView.Text,BindingMode.TwoWay);

这会触发错误:

System.Reflection.TargetException: Object of type '[Solution].Client.Shared.ViewModels.BooksViewModel' doesn't match target type '[Solution].Client.Android.BookDetailsFragment'

?我试过的另一件事:

_titleBinding = this.SetBinding(() => VM.CurrentBook.Title,BindingMode.TwoWay);
enter code here

这引发了另一个错误:

System.Reflection.TargetException: Object of type 'Android.Support.V7.Widget.AppCompatTextView' doesn't match target type '[solution].Client.Android.BookDetailsFragment'

解决方法

当xamarin android和mvvm-light上的数据绑定时,我一直在使用这个重载:

VM.SetBinding(() => VM.CurrentBook.Title,BindingMode.TwoWay);

希望能帮助到你.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读