2017-01-11 4 views
0

私はXamarin Geofenceプラグインを使用する単純なアプリケーションを作成しようとしています。 (https://github.com/domaven/xamarin-plugins/tree/master/Geofence)。Xamarin.Android MvvmCrossデータバインディングの問題

私はモデルバインディングにMvvmCrossを使用しています.Apple ViewはGeofenceインスタンスからのイベントに興味があります。私のViewModelに

のいずれかのイベントが起動されたとき、私は直接Viewに標的にされている私のViewModelにおける結合特性の値を変更することができるように、私はIGeofenceListenerインタフェースを実装しています。あなたが見ることができるように

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のプロパティが更新されているのがわかります。実際にViewを更新していないのはRaisePropertyChangedイベントです。ここで

は私のビューのコードである: -

<?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> 

これは私のコアライブラリ内のAppセットアップコードです: - ここに

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

    } 

答えて

1

これは、CrossGeofence.Initialize<MainViewModel>();がMvvmCrossによって作成されたものではない新しいViewModelを作成するためです。デバッガでアクティビティのViewModelを検査すると、そのことがわかります。

ソリューション

GeofenceListenerプロパティを使用します。

CrossGeofence.GeofenceListener = (IGeofenceListener)ViewModel; 
CrossGeofence.Initialize<MainViewModel>(); 
関連する問題