Home > Software engineering >  How can i Change tab item Content on Xamarin forms shell
How can i Change tab item Content on Xamarin forms shell

Time:05-16

I need to change one of my tabs' content depending on the condition of the user logged in or just a guest this is my shell

<?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:pages="clr-namespace:E_commerce_App.Views.Pages"
       xmlns:views="clr-namespace:E_commerce_App.Views" x:DataType="views:TabContainer"
       x:Class="E_commerce_App.Views.TabContainer"
       TabBarBackgroundColor="#F7F7F7" BackgroundColor="red"
       TabBarTitleColor="red" TabBarUnselectedColor="Black"
       >

<TabBar>
    <Tab Title="Categories" Icon="menu.png">
        <ShellContent>
            <pages:Categories />
        </ShellContent>
    </Tab>
    <Tab Title="Cart" Icon="cart.png">
        <ShellContent >
            <pages:Cart />
        </ShellContent>
    </Tab>
    <Tab Title="Profile" Icon="user.png">
        <ShellContent
            ContentTemplate="{DataTemplate pages:Profile}" />
    </Tab>
    <Tab Title="Profile" Icon="user.png" x:Name="AccountDetailsPage" IsVisible="False" IsEnabled="True">
        <ShellContent Route="AccountDetails"
          ContentTemplate="{DataTemplate views:AccountDetails}">
        </ShellContent>
    </Tab>


</TabBar>

and I want to change the profile tab to account details if I logged in I tried to change app.current but it draw to screens overthem

CodePudding user response:

I Can suggest a way.Use Dynamic Resources. In your xaml file you specify the resource much like you would StaticResource, but use DynamicResource instead. Then in code behind you can modify the resource dictionary and the view should update.

In the xaml

<Shell.Resources>
     <ResourceDictionary>
          <x:String x:Key="TabVisibilty">False</x:String>
     </ResourceDictionary>
</Shell.Resources>

<Tab IsVisible="{DynamicResource TabVisibilty}">
     <ShellContent ContentTemplate="{DataTemplate pages:AccountDetailsPage}" />
</Tab>

Then you Can change it in code behind this way

Resources["TabVisibilty"] = true;

CodePudding user response:

There are many methods to achieve this.

1.You can save your user information first and access the information once page AccountDetails appears.

For example, you can use Xamarin.Essentials: Secure Storage to store user information.

To save a value for a given key in secure storage:

using Xamarin.Essentials; 

try
{
  await SecureStorage.SetAsync("usename", "user1");
}
catch (Exception ex)
{
  // Possible that device doesn't support secure storage on device.
}

To retrieve a value from secure storage once AccountDetails appears:

       protected override void OnAppearing()
    {
        base.OnAppearing();

        try
        {
            var username =  SecureStorage.GetAsync("usename");

            // display your code here
        }
        catch (Exception ex)
        {
            // Possible that device doesn't support secure storage on device.
        }
    }

2.Create a global varible in your app, them you can access this varible in your app.

For example:

create class MyVariables.csand add static variable for your model (e.g. MyViewModel ) :

public  class MyVariables
{
    public static MyViewModel myViewModel { get; set; } = new MyViewModel { Name = "test1" };
}

MyViewModel.cs

public class MyViewModel
{
    public string Name { get; set; }
}

You can modify or access your variable in your app:

 // modify the variable
 MyVariables.myViewModel.Name = "User01";

// access the variable
Debug.WriteLine("the user's name is: "   MyVariables.myViewModel.Name);
  • Related