Add ChangePickerColorEffect for Android

This commit is contained in:
Alessandro Caliaro 2016-12-21 10:01:44 +01:00
Родитель 8722a4692e
Коммит 44d4e4079c
12 изменённых файлов: 299 добавлений и 7709 удалений

Просмотреть файл

@ -0,0 +1,84 @@
using Android.Graphics;
using Android.Runtime;
using Android.Support.V7.Widget;
using Android.Widget;
using FormsCommunityToolkit.Effects.Droid.Effects;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using Color = Xamarin.Forms.Color;
using Picker = Android.Widget.EditText;
[assembly: ExportEffect(typeof(ChangeColorPickerEffect), nameof(ChangeColorPickerEffect))]
namespace FormsCommunityToolkit.Effects.Droid.Effects
{
[Preserve (AllMembers = true)]
public class ChangeColorPickerEffect : PlatformEffect
{
private Color _color;
//private Color _falseColor;
//private Color _oldColor;
protected override void OnAttached ()
{
_color = (Color)Element.GetValue(ChangePickerColorEffect.ColorProperty);
//((Android.Widget.EditText)Control).SetTextColor(Android.Graphics.Color.Red);
//((Picker)Control).SetBackgroundColor(Android.Graphics.Color.Coral);
((Picker)Control).SetHintTextColor(_color.ToAndroid());
/*
if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.JellyBean) {
_trueColor = (Color)Element.GetValue (ChangeColorEffect.TrueColorProperty);
_falseColor = (Color)Element.GetValue (ChangeColorEffect.FalseColorProperty);
((SwitchCompat)Control).CheckedChange += OnCheckedChange;
//Supported formats for Parse are: #RRGGBB #AARRGGBB 'red', 'blue', 'green', 'black', 'white', 'gray', 'cyan', 'magenta', 'yellow', 'lightgray', 'darkgray'
((SwitchCompat)Control).ThumbDrawable.SetColorFilter (_falseColor.ToAndroid (), PorterDuff.Mode.Multiply);
}
*/
//TODO: Glenn - From lollipop mr1 you can also use the TintList instead of working with events... not sure what the best approach is
//if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.LollipopMr1)
//{
// ColorStateList buttonStates = new ColorStateList(new int[][]
// {
// new int[] {Android.Resource.Attribute.StateChecked},
// new int[] {-Android.Resource.Attribute.StateEnabled},
// new int[] {}
// },
// new int[]
// {
// Android.Graphics.Color.Red,
// Android.Graphics.Color.Blue,
// Android.Graphics.Color.Green
// }
// );
// ((SwitchCompat)Control).ThumbDrawable.SetTintList(buttonStates);
//}
}
/*
private void OnCheckedChange (object sender, CompoundButton.CheckedChangeEventArgs checkedChangeEventArgs)
{
if (checkedChangeEventArgs.IsChecked) {
((SwitchCompat)Control).ThumbDrawable.SetColorFilter (_trueColor.ToAndroid (), PorterDuff.Mode.Multiply);
//((SwitchCompat) Control).TrackDrawable.SetColorFilter(Android.Graphics.Color.Green, PorterDuff.Mode.Multiply);
} else {
((SwitchCompat)Control).ThumbDrawable.SetColorFilter (_falseColor.ToAndroid (), PorterDuff.Mode.Multiply);
//((SwitchCompat)Control).TrackDrawable.SetColorFilter(Android.Graphics.Color.Green, PorterDuff.Mode.Multiply);
}
}
*/
protected override void OnDetached ()
{
/*
if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.JellyBean && Android.OS.Build.VERSION.SdkInt < Android.OS.BuildVersionCodes.LollipopMr1) {
((Switch)Control).CheckedChange -= OnCheckedChange;
}
*/
//((Picker)Control).T
}
}
}

Просмотреть файл

@ -1,8 +1,4 @@
using System;
using Android.App;
using Android.Content.Res;
using Android.Graphics;
using Android.Graphics.Drawables;
using Android.Runtime;
using Android.Support.V7.Widget;
using Android.Widget;
@ -12,13 +8,13 @@ using Xamarin.Forms.Platform.Android;
using Color = Xamarin.Forms.Color;
using Switch = Android.Widget.Switch;
[assembly: ExportEffect (typeof (ChangeColorSwitchEffect), nameof (ChangeColorSwitchEffect))]
[assembly: ExportEffect(typeof(ChangeColorSwitchEffect), nameof(ChangeColorSwitchEffect))]
namespace FormsCommunityToolkit.Effects.Droid.Effects
{
/// <summary>
/// http://stackoverflow.com/questions/11253512/change-on-color-of-a-switch
/// </summary>
[Preserve (AllMembers = true)]
/// <summary>
/// http://stackoverflow.com/questions/11253512/change-on-color-of-a-switch
/// </summary>
[Preserve (AllMembers = true)]
public class ChangeColorSwitchEffect : PlatformEffect
{
private Color _trueColor;

Просмотреть файл

@ -106,6 +106,7 @@
<Compile Include="Effects\SizeFontToFitEffect.cs" />
<Compile Include="Effects\CustomFontEffect.cs" />
<Compile Include="Effects\SelectAllTextEntryEffect.cs" />
<Compile Include="Effects\ChangeColorPickerEffect.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Просмотреть файл

@ -0,0 +1,46 @@
using System.Linq;
using Xamarin.Forms;
namespace FormsCommunityToolkit.Effects
{
public static class ChangePickerColorEffect
{
public static readonly BindableProperty ColorProperty = BindableProperty.CreateAttached ("Color", typeof (Color?), typeof (ChangePickerColorEffect), Color.Transparent, propertyChanged: OnColorChanged);
// public static readonly BindableProperty TrueColorProperty = BindableProperty.CreateAttached ("TrueColor", typeof (Color?), typeof (ChangeColorEffect), Color.Transparent, propertyChanged: OnColorChanged);
private static void OnColorChanged (BindableObject bindable, object oldValue, object newValue)
{
var control = bindable as Picker;
if (control == null)
return;
var color = (Color)newValue;
var attachedEffect = control.Effects.FirstOrDefault (e => e is ChangeColorPickerEffect);
if (color != Color.Transparent && attachedEffect == null) {
control.Effects.Add (new ChangeColorPickerEffect ());
} else if (color == Color.Transparent && attachedEffect != null) {
control.Effects.Remove (attachedEffect);
}
}
public static Color GetColor (BindableObject view)
{
return (Color)view.GetValue (ColorProperty);
}
public static void SetColor (BindableObject view, string color)
{
view.SetValue (ColorProperty, color);
}
}
public class ChangeColorPickerEffect : RoutingEffect
{
public ChangeColorPickerEffect() : base("FormsCommunityToolkit.Effects.ChangeColorPickerEffect")
{
}
}
}

Просмотреть файл

@ -48,6 +48,7 @@
<Compile Include="CustomFontEffect.cs" />
<Compile Include="ItalicPlaceholderEffect.cs" />
<Compile Include="SelectAllTextEntryEffect.cs" />
<Compile Include="ChangePickerColorEffect.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="Xamarin.Forms.Core">

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Просмотреть файл

@ -54,6 +54,9 @@
<Compile Include="Views\LabelPage.xaml.cs">
<DependentUpon>LabelPage.xaml</DependentUpon>
</Compile>
<Compile Include="Views\PickerPage.xaml.cs">
<DependentUpon>PickerPage.xaml</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Views\MainPage.xaml">
@ -67,6 +70,9 @@
<EmbeddedResource Include="Views\LabelPage.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Include="Views\PickerPage.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />

Просмотреть файл

@ -16,6 +16,7 @@
<Button x:Name="ViewButton" Text="View effects" Clicked="OnViewButtonClicked" />
<Button x:Name="SwitchButton" Text="Switch effects" Clicked="OnSwitchButtonClicked" />
<Button x:Name="LabelButton" Text="Label effects" Clicked="OnLabelButtonClicked" />
<Button x:Name="PickerButton" Text="Picker effects" Clicked="OnPickerButtonClicked" />
</StackLayout>
</ContentPage>

Просмотреть файл

@ -29,5 +29,11 @@ namespace FormsCommunityToolkit.Effects.SampleApp.Views
{
Navigation.PushAsync(new LabelPage());
}
}
private void OnPickerButtonClicked(object sender, EventArgs args)
{
Navigation.PushAsync(new PickerPage());
}
}
}

Просмотреть файл

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:effects="clr-namespace:FormsCommunityToolkit.Effects;assembly=FormsCommunityToolkit.Effects"
x:Class="FormsCommunityToolkit.Effects.SampleApp.Views.PickerPage">
<ContentPage.Content>
<StackLayout Padding="20">
<Picker x:Name="ChangeColorPicker" effects:ChangePickerColorEffect.Color = "Red" Title = "PICKER TITLE IS RED" />
</StackLayout>
</ContentPage.Content>
</ContentPage>

Просмотреть файл

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using Xamarin.Forms;
namespace FormsCommunityToolkit.Effects.SampleApp.Views
{
public partial class PickerPage : ContentPage
{
public PickerPage()
{
InitializeComponent();
}
}
}