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

♀尐吖头ヾ 提交于 2019-12-10 10:55:09

问题


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

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

now when click navigation button, will just simply navigate to an 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:

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

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

So how to do it? Thanks.


回答1:


I have done it using a separate Frame per 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, StackPanel with Orientation=Horizontal looks ok.

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.

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);          
        }



来源:https://stackoverflow.com/questions/34668998/c-sharp-wpf-frame-how-to-create-slide-effect-on-pages

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!