Skip to content

Instantly share code, notes, and snippets.

@davidortinau
Last active March 1, 2017 02:49
Show Gist options
  • Save davidortinau/2356fcfd123f827ab23c8522b0a72ee2 to your computer and use it in GitHub Desktop.
Save davidortinau/2356fcfd123f827ab23c8522b0a72ee2 to your computer and use it in GitHub Desktop.
// In your Forms shared code
using System;
using Xamarin.Forms;
namespace FormsPlayground
{
public class DisabledTitleColorEffect : RoutingEffect
{
string _disabledColor;
public string DisabledColor
{
get
{
return _disabledColor;
}
set
{
_disabledColor = value;
}
}
public DisabledTitleColorEffect() : base ("FormsPlayground.DisabledTitleColorEffect")
{
}
}
}
// In your iOS project
using System;
using System.Diagnostics;
using System.Linq;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly:ResolutionGroupName ("FormsPlayground")]
[assembly: ExportEffect (typeof (FormsPlayground.iOS.DisabledTitleColorEffect), "DisabledTitleColorEffect")]
namespace FormsPlayground.iOS
{
public class DisabledTitleColorEffect : PlatformEffect
{
protected override void OnAttached ()
{
try {
var effect = (FormsPlayground.DisabledTitleColorEffect)Element.Effects.FirstOrDefault (e => e is FormsPlayground.DisabledTitleColorEffect);
var btn = (UIButton)Control;
btn.SetTitleColor(Color.FromHex(effect.DisabledColor).ToUIColor(), UIControlState.Disabled);
} catch (Exception ex) {
Debug.WriteLine ("Cannot set property on attached control. Error: ", ex.Message);
}
}
protected override void OnDetached ()
{
}
}
}
<?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:local="clr-namespace:FormsPlayground" x:Class="FormsPlayground.FormsPlaygroundPage">
<!-- other things -->
<Button x:Name="btnForgotPassword"
BackgroundColor="Transparent" Text="other text" TextColor="#80868C" Clicked="Handle_Clicked"
VerticalOptions="Start">
<Button.Effects>
<local:DisabledTitleColorEffect DisabledColor="#CC0000"/>
</Button.Effects>
</Button>
<!-- other things -->
</ContentPage>
@robertmclaws
Copy link

This was actually very helpful, thanks very much! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment