Xamarin.Android MvvmCross数据绑定问题

时间:2017-01-11 11:36:16

标签: xamarin data-binding xamarin.android mvvmcross

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

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

在我的ViewModel上,我实现了IGeofenceListener接口,以便在触发任何事件时,我可以直接更改ViewModelView中绑定的绑定属性的值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())); } } 1}}。

 
RaisePropertyChanged

正如您所看到的,在某些事件中,我正在更新ViewModel的属性,然后为View调用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>());
        }
    }

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

[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),
            });
        }

    }

这是主要活动MvxClass: -

$newarray = New-Object System.Collections.ArrayList
$newmultiarray = New-Object System.Collections.ArrayList
$a = ,("test", "3333", "peter", "peter2")
$a += ,("rrrr", "8888", "max", "rhzzh")

$stringlist = $a | Sort-Object @{Expression={$_[0]}; Ascending=$True} | %{ "$($_[0]),$($_[1]),$($_[2]),$($_[3])" }

$arraymulti =$stringlist -split "[\r\n]"

foreach ($i in $arraymulti){
    $array = $i -split ","
        foreach ($y in $array){
            [void]$newarray.Add($y)
        }
    [void]$newmultiarray.Add($newarray)
    $newarray = New-Object System.Collections.ArrayList
    }

$newmultiarray

1 个答案:

答案 0 :(得分:1)

这是因为CrossGeofence.Initialize<MainViewModel>();创建了一个新的ViewModel,它不是由MvvmCross创建的ViewModel。当您在调试器中检查活动的ViewModel时,可以看到这一点。

<强>解决方案

使用GeofenceListener属性。

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