目标版本16299与17134数据透视/内容演示者的行为差异

时间:2019-06-21 21:59:11

标签: c# xaml uwp uwp-xaml

我无法弄清楚这两个版本之间发生了什么变化以显示此行为,但是我想从一些不同的角度收集一些可能发生的情况的见解。我有一个顶层菜单,其中包含一个子菜单,在其中显示实际内容。

我有一个带有枢轴菜单的MainPage。 MainPages枢轴在其ItemTemplate中包含一个名为“ Tab”的页面。

每个MainPage数据透视选择都有其自己的“ Tab”实例

选项卡包含另一个“数据透视”菜单。在“标签”枢纽菜单ItemTemplate中,我有一个ContentPresenter控件,该控件绑定到名为“ SubTabContent”的UserControl类型的收集项。

在16299中,当选择“子”选项卡时,将触发SubTabContent加载事件的每个实例。 在17134中,将触发所有SubTabContent加载事件,而不选择子选项卡。

这是我要控制的内容,但找不到发生这种情况的原因。是ContentPresenter吗?还是枢轴,还是架构?

重现此步骤。创建两个空白UWP项目。以16299为目标,然后以17134为目标。

对两个应用程序都使用下面的代码/步骤。在SubTabContent用户控件的加载事件中设置一个断点,您将看到两个应用程序在加载方式上的差异。

我需要仅在选择选项卡时才加载子级别的内容。我不希望加载超出此范围。我目前在16299,可以正常工作,并希望使用17134来使用较新的功能。但由于这种加载行为而无法。

在主页上

<Page
    x:Class="_17134PivotExample.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:_17134PivotExample"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <StackPanel>
        <ScrollViewer BorderBrush="Blue" BorderThickness="3">
            <Pivot ItemsSource="{x:Bind menuItems}">
                <Pivot.HeaderTemplate>
                    <DataTemplate x:DataType="local:MenuItem">
                        <TextBlock Text="{x:Bind Header}"/>
                    </DataTemplate>
                </Pivot.HeaderTemplate>
                <Pivot.ItemTemplate>
                    <DataTemplate x:DataType="local:MenuItem">
                        <local:Tab/>
                    </DataTemplate>
                </Pivot.ItemTemplate>
            </Pivot>
        </ScrollViewer>
    </StackPanel>
</Page>



public sealed partial class MainPage : Page
    {
        public ObservableCollection<MenuItem> menuItems { get; set; }
        public MainPage()
        {
            this.InitializeComponent();
            menuItems = new ObservableCollection<MenuItem>();
            for (int i = 0; i < 6; i++)
            {
                menuItems.Add(new MenuItem
                {
                    Header = "Menu Tab + {i++}",
                    Content = new Tab()
                });

            }
        }
    }

在每个应用中添加一个班级

public class MenuItem
    {
        public string Header { get; set; }
        public UserControl Content { get; set; }
    }

添加一个名为“标签”的页面

<Page
    x:Class="_17134PivotExample.Tab"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="using:_17134PivotExample"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
    mc:Ignorable="d">



    <Grid>
        <ScrollViewer  BorderBrush="Black"
            BorderThickness="3">
            <Pivot
            ItemsSource="{x:Bind menuItems}">
                <Pivot.HeaderTemplate>
                    <DataTemplate x:DataType="local:MenuItem">
                        <TextBlock Text="{x:Bind Header}" />
                    </DataTemplate>
                </Pivot.HeaderTemplate>
                <Pivot.ItemTemplate>
                    <DataTemplate x:DataType="local:MenuItem">
                        <ScrollViewer>
                            <ContentPresenter Content="{x:Bind Content}" />
                        </ScrollViewer>
                    </DataTemplate>
                </Pivot.ItemTemplate>
            </Pivot>
        </ScrollViewer>
    </Grid>
</Page>



public sealed partial class Tab : Page
    {
        public ObservableCollection<MenuItem> menuItems { get; set; }
        public Tab()
        {
            this.InitializeComponent();
            menuItems = new ObservableCollection<MenuItem>();
            for (int i = 0; i < 6; i++)
            {
                menuItems.Add(new MenuItem
                {
                    Header = "Sub Menu Tab" + i++,
                    Content = new SubTabContent()
                });

            }
            this.Loaded += Tab_Loaded;
        }

        private void Tab_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            int blah = 1;
        }
    }

添加一个名为SubTabContent的UserControl

<UserControl
    x:Class="_17134PivotExample.SubTabContent"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="using:_17134PivotExample"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Background="Black"
    mc:Ignorable="d">

    <StackPanel>
        <TextBlock Text="New Page" />
        <TextBlock Text="New Page" />
        <TextBlock Text="New Page" />
        <TextBlock Text="New Page" />
        <TextBlock Text="New Page" />
        <TextBlock Text="New Page" />
        <TextBlock Text="New Page" />
        <TextBlock Text="New Page" />
        <TextBlock Text="New Page" />
        <TextBlock Text="New Page" />
    </StackPanel>
</UserControl>



public sealed partial class SubTabContent : UserControl
    {
        public SubTabContent()
        {
            this.InitializeComponent();
            this.Loaded += SubTabContent_Loaded;
        }

        private void SubTabContent_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            int i = 0;
            Debug.WriteLine("Loaded" + i++);
        }
    }

0 个答案:

没有答案