I am implementing a custom control with renderer based on a view. I add a button to the view and handler the click event. In the click event, I want to set the background color. But when I do it seems to cause ALL the other controls to get their background color set to black. I am not sure why this is happening, and it seems like a bug to me. Setting the text color, or other elements of the control don't seem to cause any problems. Can someone explain what is happening?
I am using v2.0.1.6495 of Xamarin.Forms
[assembly: ExportRenderer(typeof(App.Controls.ImageButton), typeof(App.Droid.Renderers.ImageButtonRenderer))]
namespace App.Droid.Renderers
{
public class ImageButtonRenderer : ViewRenderer
{
Android.Widget.Button btn;
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.View> e)
{
base.OnElementChanged(e);
try
{
if (Control == null)
{
btn = new Android.Widget.Button(Context);
SetNativeControl(btn);
}
if (e.OldElement != null)
{
btn.Click -= OnBtnClick;
}
if (e.NewElement != null)
{
btn.click += onbtnclick;
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(@" ERROR: ", ex.Message);
}
}
void OnBtnClick(object sender, EventArgs e)
{
if (btn != null)
{
btn.SetBackgroundColor(Android.Graphics.Color.Blue);
}
}
}
}
and the XAML
<?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:converters="clr-namespace:App.Converters;assembly=App"
xmlns:ctrls="clr-namespace:App.Controls;assembly=App"
x:Class="App.Views.MainPage">
<ContentPage.Resources>
<ResourceDictionary>
<converters:ImageMapConverter x:Key="ImgConv"></converters:ImageMapConverter>
</ResourceDictionary>
</ContentPage.Resources>
<Grid BackgroundColor="White" Padding="10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10*"/>
<ColumnDefinition Width="80*"/>
<ColumnDefinition Width="10*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="15*"/>
<RowDefinition Height="65*"/>
<RowDefinition Height="5*"/>
<RowDefinition Height="5*"/>
</Grid.RowDefinitions>
<Image Grid.Column="1" Grid.Row="0" Aspect="AspectFit" HorizontalOptions="Fill" VerticalOptions="Fill" Source="{Binding ImageMapper, Converter={StaticResource ImgConv}, ConverterParameter='App.Images.logo.png'}" />
<Grid Grid.Column="0" Grid.Row="3" Grid.ColumnSpan="3">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="5*"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="5*"/>
</Grid.ColumnDefinitions>
<ctrls:ImageButton Grid.Column="0" />
<Image Grid.Column="2" Aspect="AspectFit" HorizontalOptions="Fill" VerticalOptions="Fill" Source="{Binding ImageMapper, Converter={StaticResource ImgConv}, ConverterParameter='App.Images.cog.png'}" />
</Grid>
</Grid>