[UWP] FlipView在调整大小时创建额外的FlipViewItem [英] [UWP] FlipView creating additional FlipViewItem on resize

查看:55
本文介绍了[UWP] FlipView在调整大小时创建额外的FlipViewItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个  FlipView   with
项目和项目模板。最初加载控件时,会创建三个FlipView项。我注意到如果我在那之后调整窗口大小,那么  FlipView  决定
以创建额外的第四项。为了演示这种奇怪的行为,我创建了以下小例子:

I have a FlipView with items and and an item template. Initially when the control is loaded, three FlipView items are created. I noticed that if I resize the window after that, the FlipView decides to create an additional fourth item. To demonstrate this strange behaviour I've created the following small example:

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

<Grid>
    <FlipView x:Name="items">
        <FlipView.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding}" Loaded="TextBlock_Loaded"/>
            </DataTemplate>
        </FlipView.ItemTemplate>
    </FlipView>
</Grid>




这是代码隐藏:

And this is the code-behind:

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        InitializeComponent();
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        items.ItemsSource = new string[] { "Text 1", "Text 2", "Text 3", "Text 4" };
    }

    private void TextBlock_Loaded(object sender, RoutedEventArgs e)
    {
        var textBlock = sender as TextBlock;
        System.Diagnostics.Debug.WriteLine(textBlock.Text);
    }
}



加载页面后,您将看到"文本1","文本2","文本" 3英寸在程序输出中。然后,如果您仍然在第一个项目上尝试调整窗口大小,则"文本4"也会出现在输出中。

When the page is loaded, you will see "Text 1", "Text 2", "Text 3" in the program output. Then if you try to resize the window while still on the first item, "Text 4" will also appear in the output.


我的问题是:  为什么会发生这种情况,我能做些什么来阻止它吗?

推荐答案

嗨Panayot Vasilev,

Hi Panayot Vasilev,

>>为什么会发生这种情况,我可以做些什么来阻止它吗?

>>Why does this happen and can I do anything to prevent it?

这是由虚拟化引起的,FlipView将不会在第一次加载所有项目。单击下一个项目按钮时,它将加载下一个项目。请参阅此链接中的ListView和GridView数据虚拟化:  https://msdn.microsoft.com/en-us/windows/uwp/debug-test-perf/listview-and-gridview-data-optimization

It is caused by the virtualization, the FlipView will not load all of the items by first time. When you click the next item button, it will load the next item. Please refer ListView and GridView data virtualization in this link: https://msdn.microsoft.com/en-us/windows/uwp/debug-test-perf/listview-and-gridview-data-optimization.

如果要禁用虚拟化,您应该可以使用Grid或StackPanel来替换VirtualizingStackPanel。

If you want to disable the virtualization, you should able to use Grid or StackPanel to replace the VirtualizingStackPanel.

例如:

<FlipView x:Name="items">
    <FlipView.ItemsPanel>
        <ItemsPanelTemplate>
            <Grid></Grid>
        </ItemsPanelTemplate>
    </FlipView.ItemsPanel>
    <FlipView.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock Text="{Binding}" Loaded="TextBlock_Loaded" />
            </StackPanel>
        </DataTemplate>
    </FlipView.ItemTemplate>
</FlipView>

最好的问候,

Jayden Gu

Jayden Gu


这篇关于[UWP] FlipView在调整大小时创建额外的FlipViewItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆