0

I have been using Xamarin Forms to develop iOS and Android applications. I want to access a StackLayout that is within a TabbedPage so I can make it visible or hidden whenever the user changes tabs, but when I try to access the StackLayout I get "This does not exist in the current context". Here is my XAML code and my CS code.

CS

using System;
using System.Collections.Generic;
using Xamarin.Forms;

namespace DebuggerTestAndroidIOS
{
    public partial class PatientTabPage : TabbedPage
    {
        public PatientTabPage ()
        {
            InitializeComponent ();
            ItemsSource = PatientDataModel.tabs;
            //vitalSignsStack.IsVisible = true;

            this.CurrentPageChanged += (object sender, EventArgs e) => {
                var i = this.Children.IndexOf(this.CurrentPage);
                System.Diagnostics.Debug.WriteLine("Page No:"+i);

                if (i == 1){
                    vitalSignsStack.IsVisible = true;
                }           
            };
        }
    }
}

XAML

<?xml version="1.0" encoding="UTF-8"?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
            x:Class="DebuggerTestAndroidIOS.PatientTabPage">
    <TabbedPage.ItemTemplate>
        <DataTemplate>
            <ContentPage Title ="{Binding TabName}">
                <!--Parent Wrapper layout-->
                <StackLayout Orientation="Vertical" BackgroundColor="White">
                   <StackLayout x:Name="vitalSignsStack" Orientation="Horizontal" IsVisible="false">
                        <Image Source="VitalSigns.png" HorizontalOptions="Center"/>
                    </StackLayout>
                </StackLayout><!--End parent wrapper-->
            </ContentPage>
        </DataTemplate>
    </TabbedPage.ItemTemplate>
</TabbedPage>
AGB
  • 2,230
  • 1
  • 14
  • 21
Lala
  • 35
  • 1
  • 1
  • 8
  • switching tabs will hide the content of the non-active tab - so what is the point of manually hiding the StackLayout too? – Jason May 03 '16 at 22:21
  • @Jason the way I have it, all tabs have the same layout. Which is why I hide and unhide whenever user switch tabs. Only half of the screen will be kept constant, while the other half changes in content and layout. – Lala May 03 '16 at 22:30
  • But each tab will have it's own instance of the content. A change made to the content of one tab should not effect the content of the other tabs. – Jason May 03 '16 at 22:33

3 Answers3

1

An element is only going to be accesible within the context of the page that created it - in this case, the ContentPage.

If you want to reach it from outside of the ContentPage, you will need to add a public method or property to the ContentPage that exposes it.

Jason
  • 86,222
  • 15
  • 131
  • 146
1

You cannot access a control inside a DataTemplate with its name. The problem is, that it will be repeated and so this name would exist multiple times, what is not allowed.

But why don't you create a new Page like this:

public partial class PatientTabContentPage : TabbedPage
{
    public PatientTabContentPage ()
    {
        InitializeComponent ();
    }
    public HideVitalSignsStack(bool true){
        vitalSignsStack.IsVisible = true;
    }
}

And change DataTemplate to

<DataTemplate>
    <PatientTabContentPage Title ="{Binding TabName}">
</DataTemplate>

Then hide the stackpanel with

this.CurrentPageChanged += (object sender, EventArgs e) => {
    var page = CurrentPage as PatientTabContentPage;
    var i = this.Children.IndexOf(this.CurrentPage);
    System.Diagnostics.Debug.WriteLine("Page No:"+i);    
    if (i == 1){
        page.HideVvitalSignsStack(true);
    }   
};
Matt
  • 4,612
  • 1
  • 24
  • 44
0

Thanks for your efforts. I tried them, still was not able to access the StackLayouts. I modified a little bit my code, this helped a lot and made everything easier: Creating different layouts for each tab

Lala
  • 35
  • 1
  • 1
  • 8