Forum Xamarin Xamarin.Forms

Change ShellContent programatically

Hi there I want to have 2 routes depending on a preference setting, how can I change the XAML line ContentTemplate="{DataTemplate views:CatsPage}" from the code behind?

     <ShellContent Title="Cats"
                      Icon="cat.png"
                      ContentTemplate="{DataTemplate views:CatsPage}" />
        <ShellContent Title="Dogs"
                      Icon="dog.png"
                      ContentTemplate="{DataTemplate views:DogsPage}" />

Best Answer

  • jointothedarksidejointothedarkside Member ✭✭
    Accepted Answer

    Well figured it out wasn't that hard just did:

    <ShellContent Title="Dogs"
                      Icon="dog.png"
                      ContentTemplate="{DataTemplate views:DogsPage}" 
              x:Name="DogsContent"/>
    

    and the code behind just did:
    private DataTemplate DogsData;

    if (Preferences.Get("Spanish", true))
            {
                DogsData = new DataTemplate(typeof(MyView));
                DogsContent.ContentTemplate = DogsData;
        }...
    

Answers

  • jointothedarksidejointothedarkside Member ✭✭
    Accepted Answer

    Well figured it out wasn't that hard just did:

    <ShellContent Title="Dogs"
                      Icon="dog.png"
                      ContentTemplate="{DataTemplate views:DogsPage}" 
              x:Name="DogsContent"/>
    

    and the code behind just did:
    private DataTemplate DogsData;

    if (Preferences.Get("Spanish", true))
            {
                DogsData = new DataTemplate(typeof(MyView));
                DogsContent.ContentTemplate = DogsData;
        }...
    
  • JarvanJarvan Member, Xamarin Team Xamurai
    edited September 2019

    Congrats! Please mark your solution as the answer, it'll help others who face the similar issue.

  • @Jarvan said:
    Congrats! Please mark you solution as the answer, it'll help others who face the similar issue.

    Thanks @Jarvan forgot about it but there's as an answer now, have a great day, also any better way to solve it is welcome

  • toyinstoyins USMember ✭✭✭

    Is this being changed in the constructor of the shell? Or are there are other places where the values of the shell content can be changed. I tried this in the constructor of the Shell and it doesn't seem to work.

Sign In or Register to comment.