Ebikeneser
Ebikeneser

Reputation: 2374

Access controls in Xamarin Forms

I am a newbie to Xamarin Forms.

I want to enable a ShellSection (tab) in my app once a specific stage has been completed.

Currently I use an AppShell class to handle navigation controls (currently PageOneTab is not enabled) -

<?xml version="1.0" encoding="utf-8" ?>
<Shell xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:MyApp="clr-namespace:MyApp"
             x:Class="MyApp.AppShell"
             FlyoutBehavior="Disabled"
             Navigating="Handle_Navigating">
    <ShellItem>

        <ShellSection x:Name="HomeTab" Icon="home">
            <ShellContent>
                <MyApp:HomePage/>
            </ShellContent>
        </ShellSection>

        <ShellSection x:Name="PageOneTab" x:FieldModifier="public"  IsEnabled="False">
            <ShellContent>
                <MyApp:PageOnePage/>
            </ShellContent>
        </ShellSection>

    </ShellItem>
</Shell>

In the HomePage ContentPage I have a button click -

public HomePage()
{
    InitializeComponent();

}

private async void HomePageButton_Clicked(object sender, EventArgs e)
{
    await Navigation.PushAsync(new HandlingPage());
}

And in my HandlingPage ContentPage based on the result, I want to enable the ShellSection PageOneTab.

public HandlingPage()
{
    InitializeComponent();

    // do stuff....

    // then set -
    StatusTab.IsEnabled = true;
}

I thought by adding x:FieldModifier="public" to the control I would be able to access it from the HandlingPage however it is not available, in fact the only place I can access the control currently I believe is in the code behind of AppShell.xaml.cs by doing StatusTab.IsEnabled = true;. I believe this may be down to BindingContext however not sure.

Upvotes: 0

Views: 266

Answers (1)

Cherry Bu - MSFT
Cherry Bu - MSFT

Reputation: 10356

If you want to change one ShellSection's IsEnable status, I suggest you can use MessageCenter to do this.

My ShellItems:

 <ShellItem>
    <ShellSection
        x:Name="itempage"
        Title="Browse"
        Icon="tab_feed.png">
        <ShellContent>
            <local:ItemsPage />
        </ShellContent>
    </ShellSection>
    <ShellSection
        x:Name="aboutpage"
        Title="About"
        Icon="tab_about.png"
        IsEnabled="False">
        <ShellContent>
            <local:AboutPage />
        </ShellContent>
    </ShellSection>
</ShellItem>

And subscribe to a message in AppShell.cs:

 public AppShell()
    {
        InitializeComponent();

        MessagingCenter.Subscribe<AppShell>(this, "Hi", (sender) =>
        {
            aboutpage.IsEnabled = true;
        });
    }

Sending one message in HandlingPage Contentpage:

 private void btn1_Clicked(object sender, EventArgs e)
    {
        MessagingCenter.Send<AppShell>(new AppShell(),"Hi");
    }

This is the article about MessageCenter, you can take a look:

https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/messaging-center

Upvotes: 2

Related Questions