C# WPF Frame 如何在页面上创建幻灯片效果?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34668998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:36:50  来源:igfitidea点击:

C# WPF Frame how to create slide effect on pages?

c#wpfslide

提问by Oh My Dog

I want to make a page slide effect on WPF, Frame control. here we go.

我想在WPF,Frame控件上制作页面幻灯片效果。开始了。

First of all, I put a frame and 2 buttons for navigation:

首先,我放了一个框架和两个导航按钮:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="25"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="25"/>
    </Grid.ColumnDefinitions>
    <Frame x:Name="frame" Source="page1.xaml" Grid.Column="1" />
    <Button Grid.Column="0" Content="&lt;" Click="GoPrevious"/>
    <Button Grid.Column="2" Content="&gt;" Click="GoNext"/>
</Grid>

Then I create 3 Pages in difference colors: Page1.xaml, Page2.xaml and Page3.xaml

然后我创建了 3 个不同颜色的页面:Page1.xaml、Page2.xaml 和 Page3.xaml

now when click navigation button, will just simply navigate to an uri:

现在,当单击导航按钮时,只需导航到一个 uri:

frame.Navigate(new Uri("page2.xaml", UriKind.Relative));

Done. Now I want to make each page slide when navigated (everybody want it). So my plan is to set every page's Loaded EventTrigger:

完毕。现在我想在导航时使每个页面滑动(每个人都想要它)。所以我的计划是设置每个页面的 Loaded EventTrigger:

<Page.Triggers>
    <EventTrigger RoutedEvent="FrameworkElement.Loaded">
        <BeginStoryboard>
            <Storyboard>
                <ThicknessAnimation Duration="0:0:0.75" Storyboard.TargetProperty="Margin" From="500,0,-500,0" To="0" />
            </Storyboard>
        </BeginStoryboard>
    </EventTrigger>
    <EventTrigger RoutedEvent="FrameworkElement.Unloaded">
        <BeginStoryboard>
            <Storyboard>
                <ThicknessAnimation Duration="0:0:0.75" Storyboard.TargetProperty="Margin" From="0" To="-500,0,500,0" />
            </Storyboard>
        </BeginStoryboard>
    </EventTrigger>
</Page.Triggers>

And it works! Everything goes fine until i found the pages are not continuous, means, when you navigate to page2, page1 will unload immediately, than begin page2 loaded event. I tried to set page1's unload event, but not work.

它有效!一切正常,直到我发现页面不连续,这意味着,当您导航到 page2 时,page1 将立即卸载,而不是开始 page2 加载事件。我试图设置 page1 的卸载事件,但不起作用。

what I want is, when page1 begin to exit, page2 should be coming to stage right away, like a train one by one.

我想要的是,当page1开始退出时,page2应该马上就上台了,就像一列火车一样。

So how to do it? Thanks.

那么怎么做呢?谢谢。

回答by AnjumSKhan

I have done it using a separate Frameper Page. As if we try to show a new webpage in a website, it's not possible without using some trick. Same is the case here. And for sliding effect, StackPanelwith Orientation=Horizontallooks ok.

我已经使用单独的Frameper完成了它Page。就像我们尝试在网站中显示一个新网页一样,不使用一些技巧是不可能的。这里的情况也是如此。而对于滑动效果,StackPanelOrientation=Horizontal看起来不错。

Note : In Frame style, I have used main Grid(Grd) to set width, as when we add children(a new Frame) to StackPanel it's Width will increase.

注意:在框架样式中,我使用主网格(Grd)来设置宽度,因为当我们将子项(一个新框架)添加到 StackPanel 时,它的宽度会增加。

You can build upon this concept.

你可以建立在这个概念之上。

<Grid Background="#FF33CF2C" x:Name="Grd">
    <StackPanel x:Name="StkPnl" Orientation="Horizontal">
        <StackPanel.Resources>
            <Style TargetType="Frame">
                <Setter Property="Margin" Value="0,50,0,0"/>
                <Setter Property="Width" Value="{Binding ActualWidth, ElementName=Grd}"/>
                <Setter Property="NavigationUIVisibility" Value="Hidden"/>
            </Style>                
        </StackPanel.Resources>
        <Frame x:Name="FramePage1" Source="/WpfNavigation;component/Sliding/Page1.xaml" />
    </StackPanel>
    <Button Content="Goto Page2" HorizontalAlignment="Left" Margin="114,18,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click">
        <Button.Triggers>
            <EventTrigger RoutedEvent="ButtonBase.Click">
                <BeginStoryboard>
                    <Storyboard Storyboard.TargetName="FramePage1" Storyboard.TargetProperty="Margin">
                        <ThicknessAnimation To="-2000 , 50 , 0, 0" Duration="0:0:5"/>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Button.Triggers>
    </Button>
</Grid>

code-behind

代码隐藏

private void Button_Click(object sender, RoutedEventArgs e)
        {
            Frame f = new Frame();
            f.Source = new Uri("pack://application:,,,/Sliding/Page2.xaml", UriKind.Absolute);

            StkPnl.Children.Add(f);          
        }

Sliding pages using stackpanel

使用堆栈面板滑动页面