wpf - 使用数据模板将 View 与 View 模型相关联时,如何动画化从一个 View 到另一个 View 的过渡

标签 wpf animation mvvm mvvm-light transition

我将发布我目前拥有的源代码,然后解释我的问题。

这是我希望过渡发生的窗口

<Window x:Class="MyApp.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MyApp" Height="600" Width="800">
<StackPanel>
    <Button Content="View1" Command="{Binding View1Command}"/>
    <Button COntent="View2" Command="{Binding View2Command}"/>
    <ContentControl Content="{Binding MyContent}"/>
</StackPanel>

这是关联的 View 模型
public class MainViewModel
{
    public RelayCommand View1Command{ get; private set;}
    public RelayCommand View2Command{ get; private set;}

    //INotifyPropertyChanged is implemented of course
    public ViewModelBase MyContent { get; set;}

    public MainViewModel()
    {
        View1Command = new RelayCommand(() => { MyContent = new ViewModel1();});
        View2Command = new RelayCommand(() => { MyContent = new ViewModel2();});
    }
}

在 app.xaml 中,我使用数据模板将 ViewModel1 与 View1 和 ViewModel2 与 View2 关联。这一切都按预期工作。单击按钮时, View 会发生变化,数据绑定(bind)有效,一切正常。

我现在喜欢做的是在 View 更改时使用动画。

我必须做些什么来为两个 View 之间的过渡设置动画,让我们说新 View 的动画淡入淡出。旧 View 消失,新 View 在一秒钟内淡入。

希望您能够帮助我。

最好的祝福

帕斯卡

p.s.如果我使用数据模板的方法没有意义并且动画无法使用该方法,我愿意接受其他最佳实践从 onw View 更改为另一个 View 。我使用的解决方案取自 karl shifflet 的 wpf 演示应用程序,但我不知道这是否是我尝试做的事情的正确解决方案。

最佳答案

<Window.Resources>
<Storyboard x:Key="OnClick1">
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="CC1">
                <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="1"/>
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="CC2">
                <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0"/>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>
        <Storyboard x:Key="OnClick2">
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="CC1">
                <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="CC2">
                <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>
</Window.Resources>
<Window.Triggers>       
        <EventTrigger RoutedEvent="ButtonBase.Click" SourceName="button">
            <BeginStoryboard x:Name="OnClick1_BeginStoryboard" Storyboard="{StaticResource OnClick1}"/>
            <BeginStoryboard x:Name="OnClick2_BeginStoryboard" Storyboard="{StaticResource OnClick2}"/>
        </EventTrigger>
    </Window.Triggers>
    <StackPanel> 
            <Button x:Name="button" Content="View1" Command="{Binding View1Command}"/>  
            <Button Content="View2" Command="{Binding View2Command}"/> 
            <Grid>
                <ContentControl Name="CC1" Opacity="1" Content="{Binding MyContent}"/> 
                <ContentControl Name="CC2" Opacity="0" Content="{Binding MyContent}"/> 
            </Grid>
            </StackPanel>

关于wpf - 使用数据模板将 View 与 View 模型相关联时,如何动画化从一个 View 到另一个 View 的过渡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3494421/

相关文章:

c# - 如何在 MVVM 中启动非模态对话框?

WPF单击标签更改复选框isChecked属性

html - 在不扭曲图像的情况下动画div高度

css - 如何在最后一帧停止动画

r - R中的动画gadm map

wpf - 无法清除对ListView的选择

wpf - 将父 View DataContext绑定(bind)到DataTemplate subview DataContext

WPF 复选框检查在滚动时移动

wpf - 在WPF文本框中绑定(bind)单元标签

c# - 如何在运行时动态地将值传递给 ObjectDataProvider.MethodParameters