【问题标题】:Xamarin.Android MvvmCross Data-Binding IssueXamarin.Android MvvmCross 数据绑定问题
【发布时间】:2017-01-11 11:36:15
【问题描述】:

我正在尝试创建一个使用 Xamarin Geofence 插件的简单应用程序。 (https://github.com/domaven/xamarin-plugins/tree/master/Geofence)。

我正在使用 MvvmCross 进行模型绑定,并且 App View 对来自 Geofence 实例的事件感兴趣。

在我的 ViewModel 上,我实现了 IGeofenceListener 接口,因此当任何事件被触发时,我可以直接更改 ViewModel 中的绑定属性的值,这些属性是 View 中的目标。

public class MainViewModel : MvxViewModel, IGeofenceListener
    {

        double _latitude;
        public double Latitude
        {
            get
            {
                return _latitude;
            }
            set
            {
                _latitude = value;
                RaisePropertyChanged(() => Latitude);

            }
        }


        double _longitude;
        public double Longitude
        {
            get
            {
                return _longitude;
            }
            set
            {
                _longitude = value;
                RaisePropertyChanged(() => Longitude);
            }
        }

        string _name;

        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
                RaisePropertyChanged(() => Name);
            }
        }

        public void OnAppStarted()
        {
            Debug.WriteLine(string.Format("{0} - {1}", CrossGeofence.Id, "App started"));
        }

        public void OnError(string error)
        {
            Debug.WriteLine(string.Format("{0} - {1}: {2}", CrossGeofence.Id, "Error", error));
        }

        public void OnLocationChanged(GeofenceLocation location)
        {
            Longitude = location.Longitude;
            Latitude = location.Latitude;
            Name = CrossGeofence.Id;

            Debug.WriteLine(string.Format("{0} - Long: {1} || Lat: {2}", CrossGeofence.Id, location.Longitude, location.Latitude));
        }

        public void OnMonitoringStarted(string identifier)
        {
            Debug.WriteLine(string.Format("{0} - Monitoring started in region: {1}", CrossGeofence.Id, identifier));
        }

        public void OnMonitoringStopped()
        {
            Debug.WriteLine(string.Format("{0} - {1}", CrossGeofence.Id, "Monitoring stopped for all regions"));
        }

        public void OnMonitoringStopped(string identifier)
        {
            Debug.WriteLine(string.Format("{0} - {1}: {2}", CrossGeofence.Id, "Monitoring stopped in region", identifier));
        }

        public void OnRegionStateChanged(GeofenceResult result)
        {
            Longitude = result.Longitude;
            Latitude = result.Latitude;

            Debug.WriteLine(string.Format("{0} - {1}", CrossGeofence.Id, result.ToString()));

        }
    }

如您所见,在某些事件上,我会更新 ViewModel 的属性,然后为 View 调用 RaisePropertyChanged 事件。

我添加了调试跟踪以确保这些事件实际被触发。

我可以在我的输出窗口中看到触发的事件。当我调试应用程序时,我可以看到ViewModel 上的属性已更新。只是RaisePropertyChanged 事件实际上并未更新View

这是我的查看代码:-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00007f"
    android:layout_marginTop="10dp">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="20dp"
        local:MvxBind="Text Longitude"
        android:id="@+id/textViewLongitude"
        android:textColor="@android:color/white" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="20dp"
        local:MvxBind="Text Latitude"
         android:id="@+id/textViewLatitude" 
        android:textColor="@android:color/white" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="20dp"
        local:MvxBind="Text Name"
        android:textColor="@android:color/white"
        android:id="@+id/textViewName" />
</LinearLayout>

这是我的核心库中的应用设置代码:-

public class App : MvxApplication
    {
        public App()
        {
            Mvx.RegisterSingleton<IMvxAppStart>(new MvxAppStart<MainViewModel>());
        }
    }

这是主要的活动 MvxClass :-

[Activity(Label = "Geofence.Android", MainLauncher = true)]
    public class MainActivity : MvxActivity<MainViewModel>
    {
        protected override void OnViewModelSet()
        {

            SetContentView(Resource.Layout.Main);
        }


        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);


            CrossGeofence.Initialize<MainViewModel>();

            CrossGeofence.Current.StopMonitoring("LHR");

            CrossGeofence.Current.StartMonitoring(new Plugin.Abstractions.GeofenceCircularRegion("Location", 54.9672132, -1.5992939, 2000)
            {
                NotifyOnStay = true,
                NotifyOnEntry = true,
                NotifyOnExit = true,
                ShowNotification = true,
                ShowEntryNotification = false,
                ShowExitNotification = false,
                ShowStayNotification = true,
                NotificationStayMessage = "stay message!",
                NotificationEntryMessage = "entry message!",
                NotificationExitMessage = "exit message!",
                StayedInThresholdDuration = TimeSpan.FromSeconds(1),
            });
        }

    }

【问题讨论】:

    标签: xamarin data-binding xamarin.android mvvmcross


    【解决方案1】:

    这是因为CrossGeofence.Initialize&lt;MainViewModel&gt;(); 创建了一个新的 ViewModel,而不是 MvvmCross 创建的那个。当您在调试器中检查 Activity 的 ViewModel 时,您可以看到这一点。

    解决方案

    使用GeofenceListener 属性。

    CrossGeofence.GeofenceListener = (IGeofenceListener)ViewModel;
    CrossGeofence.Initialize<MainViewModel>();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-13
      • 2016-11-13
      • 1970-01-01
      • 1970-01-01
      • 2015-05-20
      • 2016-10-18
      • 1970-01-01
      • 2015-07-04
      相关资源
      最近更新 更多