带动画的 MVVM(我应该使用 VisualStateManager 吗?)

发布于 2024-09-03 11:24:37 字数 707 浏览 6 评论 0原文

我在资源部分有一个 View.xaml ,其中包含以下设置:

<DataTemplate DataType="{x:Type ViewModels:MyFirstViewModel}">
    <Views:MyFirstView Content="{Binding}" />
</DataTemplate>

<DataTemplate DataType="{x:Type ViewModels:MySecondViewModel}">
    <Views:MySecondView Content="{Binding}"/>
</DataTemplate>

在 View.xaml 的内容中,我有:

<!-- SelectedMyViewModel is either set to MyFirstViewModel or MySecondViewModel -->
<ContentControl Content="{Binding SelectedMyViewModel}" />

当 SelectedMyViewModel 更改时,我想要一个动画,以便当前视图淡出并新视图淡入...

不知何故,我觉得这应该可以通过 VisualStateManager 实现 - 但我不知道如何实现!

这是一个 WPF 4.0 项目...

I've got a View.xaml with the following set in Resources-section:

<DataTemplate DataType="{x:Type ViewModels:MyFirstViewModel}">
    <Views:MyFirstView Content="{Binding}" />
</DataTemplate>

<DataTemplate DataType="{x:Type ViewModels:MySecondViewModel}">
    <Views:MySecondView Content="{Binding}"/>
</DataTemplate>

In the content of the View.xaml I have:

<!-- SelectedMyViewModel is either set to MyFirstViewModel or MySecondViewModel -->
<ContentControl Content="{Binding SelectedMyViewModel}" />

When the SelectedMyViewModel changes I'd like to have a animation, so that the current view is faded out and the new view is faded in...

Somehow I feel this should be possible via the VisualStateManager - but I can't figure out how!

This is a WPF 4.0 project...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仄言 2024-09-10 11:24:37

您也许可以找到此问题的答案来帮助您WPF 淡入淡出动画

他们正在对可见性进行淡入/淡出。可能可以将触发器从...更改

<Trigger Property="Visibility" Value="Visible">

为...

<Trigger Property="Content" Value="{x:Null}">

所以我的想法是,在 ViewModel 之间转换时,您会做一些类似

public void SwapViewModels()
{
    // SelectedMyViewModel contains a MyFirstViewModel
    // Setting to null fires the Animation to fadeout
    SelectedMyViewModel = null;

    // Setting the Value to anything but null, should fire the fadein animation
    SelectedMyViewModel = new MySecondViewModel();
}

我没有测试代码的事情,但希望它能为您提供一个起点。

You maybe able to you the answer in this question to assist you WPF Fade Animation.

They are doing a FadeIn/FadeOut on visibility. It might be possible to change the Trigger from...

<Trigger Property="Visibility" Value="Visible">

to...

<Trigger Property="Content" Value="{x:Null}">

So my idea would be that in transitioning between ViewModels you would do something like

public void SwapViewModels()
{
    // SelectedMyViewModel contains a MyFirstViewModel
    // Setting to null fires the Animation to fadeout
    SelectedMyViewModel = null;

    // Setting the Value to anything but null, should fire the fadein animation
    SelectedMyViewModel = new MySecondViewModel();
}

I haven't tested the code but hopefully it gives you a starting point.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文