Can't change color of a simple Button after Constructor was called on Lolipop

plrosaplrosa CAMember

Hi I am able to set BackGround and Border color cia XAML, but when I try to do it programatically (after constructor is called) it doesn't work. Please help. Here my XAML:

<?xml version="1.0" encoding="utf-8" ?>

<StackLayout Orientation="Vertical">
    <Grid ColumnSpacing="-6" RowSpacing="-6">
        <Grid.RowDefinitions>
            <RowDefinition Height="auto" />
            <RowDefinition Height="auto" />
            <RowDefinition Height="auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Label Grid.Row="0" Grid.Column="0" Text="Current Sequence:" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand"/>
        <Picker Grid.Row="0" Grid.Column="1" x:Name="CurrentSequencePicker" HorizontalOptions="FillAndExpand">
            <Picker.Items>
                <x:String>1</x:String>
                <x:String>2</x:String>
                <x:String>3</x:String>
                <x:String>4</x:String>
                <x:String>5</x:String>
                <x:String>6</x:String>
                <x:String>7</x:String>
                <x:String>8</x:String>
            </Picker.Items>
        </Picker>
        <Label Grid.Row="1" Grid.Column="0" Text="Step Time (ms): " HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand"/>
        <Entry Grid.Row="1" Grid.Column="1" HorizontalOptions="FillAndExpand" Text="1000"/>
        <Label Grid.Row="1" Grid.Column="2" Text="Step Count: " HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand"/>
        <Entry Grid.Row="1" Grid.Column="3" HorizontalOptions="FillAndExpand" Text="100"/>
        <Label Grid.Row="2" Grid.Column="0" Text="Sequence: " HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand"/>
        <Picker Grid.Row="2" Grid.Column="1" x:Name="SequencePicker" SelectedIndexChanged="SequencePicker_SelectedIndexChanged">
            <Picker.Items>
                <x:String>OFF</x:String>
                <x:String>2</x:String>
                <x:String>3</x:String>
                <x:String>4</x:String>
                <x:String>5</x:String>
                <x:String>6</x:String>
                <x:String>7</x:String>
                <x:String>8</x:String>
            </Picker.Items>
        </Picker>
    </Grid>
    <Grid ColumnSpacing="0" RowSpacing="0">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <Button Grid.Row="0" Grid.Column="0" x:Name="Color1Btn" BackgroundColor="White" Text="#1" FontSize="Micro" BorderColor="Black" BorderWidth="1" BorderRadius="3"/>
        <Button Grid.Row="0" Grid.Column="1" x:Name="Color2Btn" BackgroundColor="Cyan" Text="#2" FontSize="Micro" BorderColor="Black" BorderWidth="1" BorderRadius="3"/>
        <Button Grid.Row="0" Grid.Column="2" x:Name="Color3Btn" BackgroundColor="Blue" Text="#3" FontSize="Micro" BorderColor="Black" BorderWidth="1" BorderRadius="3"/>
        <Button Grid.Row="0" Grid.Column="3" x:Name="Color4Btn" BackgroundColor="Magenta" Text="#4" FontSize="Micro" BorderColor="Black" BorderWidth="1" BorderRadius="3"/>
        <Button Grid.Row="0" Grid.Column="4" x:Name="Color5Btn" BackgroundColor="Red" Text="#5" FontSize="Micro" BorderColor="Black" BorderWidth="1" BorderRadius="3"/>
        <Button Grid.Row="0" Grid.Column="5" x:Name="Color6Btn" BackgroundColor="Orange" Text="#6" FontSize="Micro" BorderColor="Black" BorderWidth="1" BorderRadius="3"/>
        <Button Grid.Row="0" Grid.Column="6" x:Name="Color7Btn" BackgroundColor="Yellow" Text="#7" FontSize="Micro" BorderColor="Black" BorderWidth="1" BorderRadius="3"/>
        <Button Grid.Row="0" Grid.Column="7" x:Name="Color8Btn" BackgroundColor="Green" Text="#8" FontSize="Micro" BorderColor="Black" BorderWidth="1" BorderRadius="3"/>
        <Button Grid.Row="1" Grid.Column="0" x:Name="Color9Btn" BackgroundColor="White" Text="#9" FontSize="Micro" BorderColor="Black" BorderWidth="1" BorderRadius="3"/>
        <Button Grid.Row="1" Grid.Column="1" x:Name="Color10Btn" BackgroundColor="White" Text="#10" FontSize="Micro" BorderColor="Black" BorderWidth="1" BorderRadius="3"/>
        <Button Grid.Row="1" Grid.Column="2" x:Name="Color11Btn" BackgroundColor="White" Text="#11" FontSize="Micro" BorderColor="Black" BorderWidth="1" BorderRadius="3"/>
        <Button Grid.Row="1" Grid.Column="3" x:Name="Color12Btn" BackgroundColor="White" Text="#12" FontSize="Micro" BorderColor="Black" BorderWidth="1" BorderRadius="3"/>
        <Button Grid.Row="1" Grid.Column="4" x:Name="Color13Btn" BackgroundColor="White" Text="#13" FontSize="Micro" BorderColor="Black" BorderWidth="1" BorderRadius="3"/>
        <Button Grid.Row="1" Grid.Column="5" x:Name="Color14Btn" BackgroundColor="White" Text="#14" FontSize="Micro" BorderColor="Black" BorderWidth="1" BorderRadius="3"/>
        <Button Grid.Row="1" Grid.Column="6" x:Name="Color15Btn" BackgroundColor="White" Text="#15" FontSize="Micro" BorderColor="Black" BorderWidth="1" BorderRadius="3"/>
        <Button Grid.Row="1" Grid.Column="7" x:Name="Color16Btn" BackgroundColor="White" Text="#16" FontSize="Micro" BorderColor="Black" BorderWidth="1" BorderRadius="3"/>
    </Grid>
    <custom:CustomImage x:Name="imgColorPicker" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Source="ColorPicker.png"/>
    <Button Text="Add" HorizontalOptions="FillAndExpand" VerticalOptions="End"/>
</StackLayout>

Here My class code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;

namespace AppDeTest.CGair
{
public partial class ColorPickerPage : ContentPage
{
private int SelectedButton = -1;

    private const int BUTTON_COLOR_1 = 0;
    private const int BUTTON_COLOR_2 = 1;
    private const int BUTTON_COLOR_3 = 2;
    private const int BUTTON_COLOR_4 = 3;
    private const int BUTTON_COLOR_5 = 4;
    private const int BUTTON_COLOR_6 = 5;
    private const int BUTTON_COLOR_7 = 6;
    private const int BUTTON_COLOR_8 = 7;
    private const int BUTTON_COLOR_9 = 8;
    private const int BUTTON_COLOR_10 = 9;
    private const int BUTTON_COLOR_11 = 10;
    private const int BUTTON_COLOR_12 = 11;
    private const int BUTTON_COLOR_13 = 12;
    private const int BUTTON_COLOR_14 = 13;
    private const int BUTTON_COLOR_15 = 14;
    private const int BUTTON_COLOR_16 = 15;

    public ColorPickerPage()
    {
        InitializeComponent();

        SelectColorButton(BUTTON_COLOR_1);
        Color1Btn.Clicked += (sender, args) =>
        {
            SelectColorButton(BUTTON_COLOR_1);
        };
        Color2Btn.Clicked += (sender, args) =>
        {
            SelectColorButton(BUTTON_COLOR_2);
        };
        Color3Btn.Clicked += (sender, args) =>
        {
            SelectColorButton(BUTTON_COLOR_3);
        };
        Color4Btn.Clicked += (sender, args) =>
        {
            SelectColorButton(BUTTON_COLOR_4);
        };
        Color5Btn.Clicked += (sender, args) =>
        {
            SelectColorButton(BUTTON_COLOR_5);
        };
        Color6Btn.Clicked += (sender, args) =>
        {
            SelectColorButton(BUTTON_COLOR_6);
        };
        Color7Btn.Clicked += (sender, args) =>
        {
            SelectColorButton(BUTTON_COLOR_7);
        };
        Color8Btn.Clicked += (sender, args) =>
        {
            SelectColorButton(BUTTON_COLOR_8);
        };
        Color9Btn.Clicked += (sender, args) =>
        {
            SelectColorButton(BUTTON_COLOR_9);
        };
        Color10Btn.Clicked += (sender, args) =>
        {
            SelectColorButton(BUTTON_COLOR_10);
        };
        Color11Btn.Clicked += (sender, args) =>
        {
            SelectColorButton(BUTTON_COLOR_11);
        };
        Color12Btn.Clicked += (sender, args) =>
        {
            SelectColorButton(BUTTON_COLOR_12);
        };
        Color13Btn.Clicked += (sender, args) =>
        {
            SelectColorButton(BUTTON_COLOR_13);
        };
        Color14Btn.Clicked += (sender, args) =>
        {
            SelectColorButton(BUTTON_COLOR_14);
        };
        Color15Btn.Clicked += (sender, args) =>
        {
            SelectColorButton(BUTTON_COLOR_15);
        };
        Color16Btn.Clicked += (sender, args) =>
        {
            SelectColorButton(BUTTON_COLOR_16);
        };

        CurrentSequencePicker.SelectedIndex = 0;

        // creating the TapGestureRecognizer
        TapGestureRecognizer tapImgColorPicker = new TapGestureRecognizer();
        imgColorPicker.GestureRecognizers.Add(tapImgColorPicker);
    }

    public void SelectColorButton(int index)
    {
        Color1Btn.BorderColor = Color.Black;
        Color2Btn.BorderColor = Color.Black;
        Color3Btn.BorderColor = Color.Black;
        Color4Btn.BorderColor = Color.Black;
        Color5Btn.BorderColor = Color.Black;
        Color6Btn.BorderColor = Color.Black;
        Color7Btn.BorderColor = Color.Black;
        Color8Btn.BorderColor = Color.Black;
        Color9Btn.BorderColor = Color.Black;
        Color10Btn.BorderColor = Color.Black;
        Color11Btn.BorderColor = Color.Black;
        Color12Btn.BorderColor = Color.Black;
        Color13Btn.BorderColor = Color.Black;
        Color14Btn.BorderColor = Color.Black;
        Color15Btn.BorderColor = Color.Black;
        Color16Btn.BorderColor = Color.Black;

        switch (index)
        {
            case BUTTON_COLOR_1:
                Color1Btn.BorderColor = Color.Red;
                SelectedButton = index;
                break;
            case BUTTON_COLOR_2:
                Color2Btn.BorderColor = Color.Red;
                SelectedButton = index;
                break;
            case BUTTON_COLOR_3:
                Color3Btn.BorderColor = Color.Red;
                SelectedButton = index;
                break;
            case BUTTON_COLOR_4:
                Color4Btn.BorderColor = Color.Red;
                SelectedButton = index;
                break;
            case BUTTON_COLOR_5:
                Color5Btn.BorderColor = Color.Red;
                SelectedButton = index;
                break;
            case BUTTON_COLOR_6:
                Color6Btn.BorderColor = Color.Red;
                SelectedButton = index;
                break;
            case BUTTON_COLOR_7:
                Color7Btn.BorderColor = Color.Red;
                SelectedButton = index;
                break;
            case BUTTON_COLOR_8:
                Color8Btn.BorderColor = Color.Red;
                SelectedButton = index;
                break;
            case BUTTON_COLOR_9:
                Color9Btn.BorderColor = Color.Red;
                SelectedButton = index;
                break;
            case BUTTON_COLOR_10:
                Color10Btn.BorderColor = Color.Red;
                SelectedButton = index;
                break;
            case BUTTON_COLOR_11:
                Color11Btn.BorderColor = Color.Red;
                SelectedButton = index;
                break;
            case BUTTON_COLOR_12:
                Color12Btn.BorderColor = Color.Red;
                SelectedButton = index;
                break;
            case BUTTON_COLOR_13:
                Color13Btn.BorderColor = Color.Red;
                SelectedButton = index;
                break;
            case BUTTON_COLOR_14:
                Color14Btn.BorderColor = Color.Red;
                SelectedButton = index;
                break;
            case BUTTON_COLOR_15:
                Color15Btn.BorderColor = Color.Red;
                SelectedButton = index;
                break;
            case BUTTON_COLOR_16:
                Color16Btn.BorderColor = Color.Red;
                SelectedButton = index;
                break;
            default:
                SelectedButton = -1;
                break;
        }
    }

    public void SetButtonColor(int index, Color color)
    {
        switch (index)
        {
            case BUTTON_COLOR_1:
                Color1Btn.BackgroundColor = color;
                break;
            case BUTTON_COLOR_2:
                Color2Btn.BackgroundColor = color;
                break;
            case BUTTON_COLOR_3:
                Color3Btn.BackgroundColor = color;
                break;
            case BUTTON_COLOR_4:
                Color4Btn.BackgroundColor = color;
                break;
            case BUTTON_COLOR_5:
                Color5Btn.BackgroundColor = color;
                break;
            case BUTTON_COLOR_6:
                Color6Btn.BackgroundColor = color;
                break;
            case BUTTON_COLOR_7:
                Color7Btn.BackgroundColor = color;
                break;
            case BUTTON_COLOR_8:
                Color8Btn.BackgroundColor = color;
                break;
            case BUTTON_COLOR_9:
                Color9Btn.BackgroundColor = color;
                break;
            case BUTTON_COLOR_10:
                Color10Btn.BackgroundColor = color;
                break;
            case BUTTON_COLOR_11:
                Color11Btn.BackgroundColor = color;
                break;
            case BUTTON_COLOR_12:
                Color12Btn.BackgroundColor = color;
                break;
            case BUTTON_COLOR_13:
                Color13Btn.BackgroundColor = color;
                break;
            case BUTTON_COLOR_14:
                Color14Btn.BackgroundColor = color;
                break;
            case BUTTON_COLOR_15:
                Color15Btn.BackgroundColor = color;
                break;
            case BUTTON_COLOR_16:
                Color16Btn.BackgroundColor = color;
                break;
        }
    }

    public void SetButtonText(int index, string text)
    {
        switch (index)
        {
            case BUTTON_COLOR_1:
                Color1Btn.Text = text;
                break;
            case BUTTON_COLOR_2:
                Color2Btn.Text = text;
                break;
            case BUTTON_COLOR_3:
                Color3Btn.Text = text;
                break;
            case BUTTON_COLOR_4:
                Color4Btn.Text = text;
                break;
            case BUTTON_COLOR_5:
                Color5Btn.Text = text;
                break;
            case BUTTON_COLOR_6:
                Color6Btn.Text = text;
                break;
            case BUTTON_COLOR_7:
                Color7Btn.Text = text;
                break;
            case BUTTON_COLOR_8:
                Color8Btn.Text = text;
                break;
            case BUTTON_COLOR_9:
                Color9Btn.Text = text;
                break;
            case BUTTON_COLOR_10:
                Color10Btn.Text = text;
                break;
            case BUTTON_COLOR_11:
                Color11Btn.Text = text;
                break;
            case BUTTON_COLOR_12:
                Color12Btn.Text = text;
                break;
            case BUTTON_COLOR_13:
                Color13Btn.Text = text;
                break;
            case BUTTON_COLOR_14:
                Color14Btn.Text = text;
                break;
            case BUTTON_COLOR_15:
                Color15Btn.Text = text;
                break;
            case BUTTON_COLOR_16:
                Color16Btn.Text = text;
                break;
        }
    }

    public void SequencePicker_SelectedIndexChanged(object sender, EventArgs e)
    {
        var picker = sender as Picker;
        switch(picker.SelectedIndex)
        {
            case 0:
                SetButtonText(SelectedButton, "#"+ (SelectedButton+1));
                SetButtonColor(SelectedButton, Color.White);
                break;
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
            case 7:
                SetButtonText(SelectedButton, "S" + (picker.SelectedIndex+1));
                SetButtonColor(SelectedButton, Color.White);
                break;
        }
    }
}

}

Posts

  • plrosaplrosa CAMember

    OK some more details. If I put my ContentPage on the first page in app.cs, everything is fine, but as soon as I add a button on first page to open ColorPickerPage or anyothers pages, it's impossible to redraw anything on the screen (even an Entry) once teh page fully load. Any idea ?

  • plrosaplrosa CAMember

    When using await Navigation.PushModalAsync(new ColorPickerPage()); instead of await Navigation.PushAsync(new ColorPickerPage());, it work perfectly, but I would like to keep the back button

Sign In or Register to comment.