0

我一直在使用 Xamarin Forms 开发 iOS 和 Android 应用程序。我想访问 TabbedPage 中的 StackLayout,以便在用户更改选项卡时使其可见或隐藏,但是当我尝试访问 StackLayout 时,我得到“这在当前上下文中不存在”。这是我的 XAML 代码和我的 CS 代码。

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>
4

3 回答 3

1

您不能使用其名称访问 DataTemplate 中的控件。问题是,它会被重复,所以这个名字会存在多次,这是不允许的。

但是你为什么不创建一个这样的新页面:

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

并将 DataTemplate 更改为

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

然后隐藏堆栈面板

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);
    }   
};
于 2016-05-03T23:05:21.420 回答
1

一个元素只能在创建它的页面的上下文中访问 - 在本例中为 ContentPage。

如果要从 ContentPage 外部访问它,则需要向公开它的 ContentPage 添加公共方法或属性。

于 2016-05-03T22:20:33.180 回答
0

感谢您的努力。我试过了,仍然无法访问 StackLayouts。我修改了一点我的代码,这有很大帮助并使一切变得更容易:为每个选项卡创建不同的布局

于 2016-05-06T02:03:38.587 回答