MVVMCross Android:绑定值未更新(MVVMCross Android: value with binding not updated)

系统教程 行业动态 更新时间:2024-06-14 17:01:31
MVVMCross Android:绑定值未更新(MVVMCross Android: value with binding not updated)

我用Xamarin(Android)+ Mvvmcross创建了简单的应用程序。 我有我的ViewModel属性数据(键入MyData)。

这是我的VievModel

public class MyViewModel:MvxViewModel { private MyData _data; public MyData Data { get { return _data; } set { _data = value; RaisePropertyChanged(() => Data); } } .... } public class MyData: INotifyPropertyChanged { public string Current { get { return _current; } set { _current = value; Debug.WriteLine(_current); NotifyPropertyChanged("Current"); } } private string _current; public event PropertyChangedEventHandler PropertyChanged; public void NotifyPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } }

我在视图中使用此绑定

xmlns:local="http://schemas.android.com/apk/res-auto" <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" local:MvxBind="Text Data.Current" android:id="@+id/textView" />

这是我的计时器:

private Timer _timer; ..... public void InitEvent(Action action) { _timer.Elapsed += TimerTick; _action = action; } private void TimerTick(object sender, ElapsedEventArgs e) { if (_action != null) _action(); }

在_action中更新proprrty Current。

值属性更新时TextView中的文本不会更改。 问题是什么? 计时器上的值已更改。 Debug.WriteLine(_current) - 显示新的值。 TextView.Text - 旧值,未更新。

I created simple app with Xamarin(Android)+Mvvmcross. I have property Data( type MyData) in my ViewModel.

This is my VievModel

public class MyViewModel:MvxViewModel { private MyData _data; public MyData Data { get { return _data; } set { _data = value; RaisePropertyChanged(() => Data); } } .... } public class MyData: INotifyPropertyChanged { public string Current { get { return _current; } set { _current = value; Debug.WriteLine(_current); NotifyPropertyChanged("Current"); } } private string _current; public event PropertyChangedEventHandler PropertyChanged; public void NotifyPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } }

I use this binding in view

xmlns:local="http://schemas.android.com/apk/res-auto" <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" local:MvxBind="Text Data.Current" android:id="@+id/textView" />

This is my Timer:

private Timer _timer; ..... public void InitEvent(Action action) { _timer.Elapsed += TimerTick; _action = action; } private void TimerTick(object sender, ElapsedEventArgs e) { if (_action != null) _action(); }

In _action updated proprrty Current.

When value property is updated Text in TextView does not changed. What is the problem? The value is changed on the timer. Debug.WriteLine(_current) - shows new value. TextView.Text - old value, not updated.

最满意答案

你的“计时器”是否在后台线程上运行?

如果是这样,那么你需要找到一些方法在UI线程上发出RaisePropertyChanged信号。

一个简单的方法是从MvxNotifyPropertyChanged继承 - 它会自动将通知编组到UI。

另一个是使用IMvxMainThreadDispatcher - 例如

public string Current { get { return _current; } set { _current = value; Debug.WriteLine(_current); Mvx.Resolve<IMvxMainThreadDispatcher>() .RequestMainThreadAction(() => NotifyPropertyChanged("Current")); } }

当然,如果多个线程正在访问set Current那么你可能会碰到奇怪的线程错误......

Is your "timer" running on a background thread?

If it is, then you'll need to find some way to signal the RaisePropertyChanged on the UI thread.

One easy way to do this is to inherit from MvxNotifyPropertyChanged - it will automatically marshal the notification on to the UI.

Another is to use IMvxMainThreadDispatcher - e.g.

public string Current { get { return _current; } set { _current = value; Debug.WriteLine(_current); Mvx.Resolve<IMvxMainThreadDispatcher>() .RequestMainThreadAction(() => NotifyPropertyChanged("Current")); } }

Of course, if multiple threads are accessing set Current then you may also hit weird threading bugs...

更多推荐

本文发布于:2023-04-20 16:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/90b2ba6a7af28314f65165ae0635d797.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:定值   Android   MVVMCross   binding   updated

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!