4

現在持っているソースコードを投稿し、その後問題を説明します。

これは、移行を実行したいウィンドウです。

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

これは関連するビューモデルです

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に関連付けました。それはすべて期待どおりに機能します。ボタンをクリックすると、ビューが変更され、データバインディングが機能し、すべて問題ありません。

私が今やりたいのは、ビューが変わったときにアニメーションを使用することです。

新しいビューのフェードインアニメーションを使用して、2つのビュー間の遷移をアニメーション化するにはどうすればよいですか。古いビューが消え、新しいビューが1秒以内にフェードインします。

あなたが私を助けてくれることを願っています。

よろしくお願いします

パスカル

psデータテンプレートを使用したアプローチが意味をなさず、そのアプローチでアニメーションが不可能な場合は、他のベストプラクティスをオンビューから別のビューに変更できます。私が使用するソリューションは、karl shiffletによるwpfデモアプリから取得されますが、それが私がやろうとしていることに対して正しいソリューションであるかどうかはわかりません。

4

2 に答える 2

0
<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>
于 2011-06-06T08:37:55.293 に答える
-1

ビューをviewmodelにバインドするためのビューモデルロケーターと遷移のためのビジュアルステートマネージャーを使用して、遷移のアプローチを変更しました。

于 2011-08-01T10:03:20.177 に答える