From 77aa610c3ab93bd9c030a837f0b1065a536c41c5 Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Sat, 8 Oct 2016 17:15:52 +0200 Subject: [PATCH] Fixes the RatingView Binding property `Rating` --- BookAStar/BookAStar/Behaviors/StarBehavior.cs | 25 ++++--- BookAStar/BookAStar/Pages/DashboardPage.xaml | 8 +-- .../ViewModels/DashboardViewModel.cs | 52 ++++++++++++-- BookAStar/BookAStar/Views/RatingView.xaml.cs | 71 +++++-------------- 4 files changed, 82 insertions(+), 74 deletions(-) diff --git a/BookAStar/BookAStar/Behaviors/StarBehavior.cs b/BookAStar/BookAStar/Behaviors/StarBehavior.cs index 1a5460f6..d7022d6c 100644 --- a/BookAStar/BookAStar/Behaviors/StarBehavior.cs +++ b/BookAStar/BookAStar/Behaviors/StarBehavior.cs @@ -31,6 +31,9 @@ namespace BookAStar.Behaviors typeof(int), typeof(StarBehavior), default(int), BindingMode.TwoWay); + + + public static event EventHandler StarTapped; public int Rating { @@ -87,7 +90,8 @@ namespace BookAStar.Behaviors BindableProperty.Create("IsStarred", typeof(bool), typeof(StarBehavior), - false); + false, + propertyChanged: OnIsStarredChanged); public bool IsStarred { @@ -113,11 +117,12 @@ namespace BookAStar.Behaviors } bool itemReached = false; - int count = 1, position = 0; + int count = 0, position = 0; // all positions to left IsStarred = true and all position to the right is false foreach (var item in behaviors) - { - if (item != behavior && !itemReached) + { + count++; + if (item != behavior && !itemReached) { item.IsStarred = true; } @@ -131,16 +136,14 @@ namespace BookAStar.Behaviors if ((bool)newValue) position = count; else position = count - 1; - } if (item != behavior && itemReached) item.IsStarred = false; item.Rating = position; - count++; } - - + // Assert count > 0 + behaviors[count - 1].Rating = position; } @@ -164,9 +167,11 @@ namespace BookAStar.Behaviors void OnTapRecognizerTapped(object sender, EventArgs args) { + // Assert sender is mine bool currentIsStarred = (bool) GetValue(IsStarredProperty); - SetValue(IsStarredProperty, !currentIsStarred); - OnIsStarredChanged(this,currentIsStarred,!currentIsStarred); + IsStarred = !currentIsStarred; + if (StarTapped!=null) + StarTapped.Invoke(this, new EventArgs()); } } } diff --git a/BookAStar/BookAStar/Pages/DashboardPage.xaml b/BookAStar/BookAStar/Pages/DashboardPage.xaml index b475686f..933000c2 100644 --- a/BookAStar/BookAStar/Pages/DashboardPage.xaml +++ b/BookAStar/BookAStar/Pages/DashboardPage.xaml @@ -43,15 +43,15 @@ > - +