Fixes the rating control

This commit is contained in:
Paul Schneider 2016-10-07 18:14:28 +02:00
commit d642ff3cad
10 changed files with 157 additions and 58 deletions

View file

@ -2,6 +2,7 @@
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:BookAStar;assembly=BookAStar"
xmlns:extensions="clr-namespace:BookAStar.Extensions;assembly=BookAStar"
xmlns:behaviors="clr-namespace:BookAStar.Behaviors;assembly=BookAStar"
xmlns:converters="clr-namespace:BookAStar.Converters;assembly=BookAStar"
x:Class="BookAStar.Views.RatingView">
@ -13,10 +14,10 @@
<behaviors:StarBehavior x:Name="starOne" GroupName="myStar"/>
</Grid.Behaviors>
<Image x:Name="starBlankOne"
Source="{local:ImageResource BookAStar.Images.star_outline.png}" />
Source="{extensions:ImageResource BookAStar.Images.Validation.star_outline.png}" />
<Image x:Name="starSelectedOne"
Source="{local:ImageResource BookAStar.Images.star_selected.png}"
Source="{extensions:ImageResource BookAStar.Images.Validation.star_selected.png}"
IsVisible="{Binding Source={x:Reference starOne},
Path=IsStarred}"/>
</Grid>
@ -25,10 +26,10 @@
<behaviors:StarBehavior x:Name="starTwo" GroupName="myStar"/>
</Grid.Behaviors>
<Image x:Name="starBlankTwo"
Source="{local:ImageResource BookAStar.Images.star_outline.png}" />
Source="{extensions:ImageResource BookAStar.Images.Validation.star_outline.png}" />
<Image x:Name="starSelectedTwo"
Source="{local:ImageResource BookAStar.Images.star_selected.png}"
Source="{extensions:ImageResource BookAStar.Images.Validation.star_selected.png}"
IsVisible="{Binding Source={x:Reference starTwo},
Path=IsStarred}"/>
</Grid>
@ -37,10 +38,10 @@
<behaviors:StarBehavior x:Name="starThree" GroupName="myStar"/>
</Grid.Behaviors>
<Image x:Name="starBlankThree"
Source="{local:ImageResource BookAStar.Images.star_outline.png}" />
Source="{extensions:ImageResource BookAStar.Images.Validation.star_outline.png}" />
<Image x:Name="starSelectedThree"
Source="{local:ImageResource BookAStar.Images.star_selected.png}"
Source="{extensions:ImageResource BookAStar.Images.Validation.star_selected.png}"
IsVisible="{Binding Source={x:Reference starThree},
Path=IsStarred}"/>
</Grid>
@ -49,10 +50,10 @@
<behaviors:StarBehavior x:Name="starFour" GroupName="myStar"/>
</Grid.Behaviors>
<Image x:Name="starBlankFour"
Source="{local:ImageResource BookAStar.Images.star_outline.png}" />
Source="{extensions:ImageResource BookAStar.Images.Validation.star_outline.png}" />
<Image x:Name="starSelectedFour"
Source="{local:ImageResource BookAStar.Images.star_selected.png}"
Source="{extensions:ImageResource BookAStar.Images.Validation.star_selected.png}"
IsVisible="{Binding Source={x:Reference starFour},
Path=IsStarred}"/>
</Grid>
@ -61,10 +62,10 @@
<behaviors:StarBehavior x:Name="starFive" GroupName="myStar"/>
</Grid.Behaviors>
<Image x:Name="starBlankFive"
Source="{local:ImageResource BookAStar.Images.star_outline.png}" />
Source="{extensions:ImageResource BookAStar.Images.Validation.star_outline.png}" />
<Image x:Name="starSelectedFive"
Source="{local:ImageResource BookAStar.Images.star_selected.png}"
Source="{extensions:ImageResource BookAStar.Images.Validation.star_selected.png}"
IsVisible="{Binding Source={x:Reference starFive},
Path=IsStarred}"/>
</Grid>
@ -72,16 +73,14 @@
<StackLayout>
<StackLayout.Resources>
<ResourceDictionary>
<local:RatingText x:Key="ratingText" />
<Style TargetType="Label" BasedOn="{StaticResource baseStyle}">
<converters:RatingText x:Key="ratingText" />
<Style TargetType="Label" BasedOn="{StaticResource LabelStyle}">
<Setter Property="TextColor" Value="#4CAF50" />
</Style>
</ResourceDictionary>
</StackLayout.Resources>
<Label Text="{Binding Source={x:Reference starFive},
Path=Rating, Converter={StaticResource ratingText}}" ></Label>
<Label Text="{Binding Source={x:Reference starFive}, Path=Rating, Converter={StaticResource ratingText}}" ></Label>
</StackLayout>
<Button Text="Submit"/>
</StackLayout>
</ContentView.Content>

View file

@ -1,4 +1,5 @@
using System;
using BookAStar.Behaviors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -10,9 +11,75 @@ namespace BookAStar.Views
{
public partial class RatingView : ContentView
{
protected int rating;
public int Rating {
get
{
return rating;
}
set
{
if (value != rating)
{
if (value < 0 || value > 5)
{
SetValue(RatingProperty, 0);
rating = 0;
}
else
{
SetValue(RatingProperty, value);
rating = value;
}
starBehaviors[4].SetValue(StarBehavior.RatingProperty, rating);
for (int i = 0; i < rating && i < 5; i++)
starBehaviors[i].SetValue(StarBehavior.IsStarredProperty, true);
for (int i = rating; i < 5 && i >= 0; i++)
starBehaviors[i].SetValue(StarBehavior.IsStarredProperty, false);
}
}
}
private bool isStarred;
public bool IsStarred
{
get
{
return isStarred;
}
set
{
SetValue(IsStarredProperty, value);
}
}
StarBehavior[] starBehaviors;
public static BindableProperty IsStarredProperty = BindableProperty.Create(
propertyName: "IsStarred",
returnType: typeof(bool),
declaringType: typeof(RatingView),
defaultValue: false,
defaultBindingMode: BindingMode.TwoWay
);
public static BindableProperty RatingProperty = BindableProperty.Create(
propertyName: "Rating",
returnType: typeof(int),
declaringType: typeof(RatingView),
defaultValue: 0,
defaultBindingMode: BindingMode.TwoWay
);
public RatingView()
{
InitializeComponent();
starBehaviors = new StarBehavior[5]
{
starOne, starTwo, starThree, starFour, starFive
};
}
}
}