Xamarin.forms Button Isenable failed

68 Views Asked by At

I'm new to Xamarin Forms and I'm designing a quiz game. I want to make the button to be disabled after the user clicks it to prevent the user from choosing it again. I tried to use Isenable but it's not working. if I miss putting some code, please point it out.

XAML CODE
 <StackLayout>
        <StackLayout>
            <Button x:Name="C11" Text="$1" WidthRequest="50" HeightRequest="100"  Clicked="C11_Clicked" />
            <Button x:Name="C12" Text="$1" WidthRequest="50" HeightRequest="100"  Clicked="C12_Clicked"/>
        </StackLayout>
       

    </StackLayout>
C# code
 private void C11_Clicked(object sender, EventArgs e)
        {
            C11.IsEnabled = false;

            Navigation.PushModalAsync(new C11());
 
        }

updated part

 private void continue_Clicked(object sender, EventArgs e)
    
        {
            MainPage m = new MainPage();
            m.C11btn.IsEnabled = false;
            Preferences.Set("ButtonEnableFlag", false);
            Navigation.PushModalAsync(new MainPage());
           
        }

        protected override void OnAppearing()
        {
            base.OnAppearing();
            var enableValue = Preferences.Get("ButtonEnableFlag", true);
            MainPage m = new MainPage();
           
            m.C11btn.IsEnabled = enableValue;
        }
1

There are 1 best solutions below

4
On

may i ask how to permanently disabled the button?

If you want to make the button disabled permanently, you need to save a custom status flag in device. Next time entering to this view, the button will be disabled by this custom status flag programmatically.

For example, we can use Xamarin.Essentials: Preferences to sace the custom status flag and override OnAppearing method as follows:

public partial class PageThird : ContentPage
{
    public PageThird()
    {
        InitializeComponent();
    }

    private void Button_Clicked(object sender, EventArgs e)
    {
        MyButton.IsEnabled = false;
        Preferences.Set("ButtonEnableFlag", false);
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();
        var enableValue = Preferences.Get("ButtonEnableFlag", true);
        MyButton.IsEnabled = enableValue;
    }
}

Here is the Xaml code of this page:

<ContentPage.Content>
    <StackLayout Padding="20">
        <Label Text="Welcome to PagePersonal!"
            VerticalOptions="CenterAndExpand" 
            HorizontalOptions="CenterAndExpand" />
        <Button x:Name="MyButton" Text="Disable" Clicked="Button_Clicked"/>
    </StackLayout>
</ContentPage.Content>

Now the button will be disabled permanently.

enter image description here

If you want to disable the button in another page, you only need to set the custom status flag in another page. Later when you back to needed page, the button will be disabled.

The another page code:

public partial class PageSecond : ContentPage
{
    public PageSecond()
    {
        InitializeComponent();
    }

    private async void Button_Clicked(object sender, EventArgs e)
    {
        await Navigation.PushModalAsync(new PageThird());
    }

    private void Disable_Clicked(object sender, EventArgs e)
    {
        Preferences.Set("ButtonEnableFlag", false);
    }

    private void Enable_Clicked(object sender, EventArgs e)
    {
        Preferences.Set("ButtonEnableFlag", true);
    }
}

Then it will work, no code need to change in the needed page.

enter image description here