Winui 3桌面MVVM;在属性更改上的动画文本块?

发布于 2025-01-20 14:15:02 字数 1744 浏览 3 评论 0原文

嗨,我正在尝试弄清楚在属性更新时如何对文本块进行动画动画。 Winui3/XAML的新手,但我发现 https://lealen.microsoft.com/en-us/dotnet/desktop/wpf/wpf/advanced/how-to-to-to-to-to-papply-animation-to-toxt-toxt-toxt ,它提供了许多例子工作,但他们都使用textblock。我已经尝试将其更改为TargetUpdupped,sourceupdepted,framework.x,binding.x等,所有这些都崩溃了,因此我不确定要放在那里。 LOT在网上的例子,但是我发现的似乎是WPF或UWP,仍然崩溃了。

My code which is pretty much same as MS's examples sans binding:

<TextBlock Text="{x:Bind Path=Viewmodel.UpdatedText, Mode=OneWay}">
  <TextBlock.Foreground>
    <SolidColorBrush x:Name="MySolidColorBrush" Color="Maroon" />
  </TextBlock.Foreground>

  <TextBlock.Triggers>
    <EventTrigger RoutedEvent="TextBlock.Loaded">
      <BeginStoryboard>
        <Storyboard>
          <ColorAnimation AutoReverse="True" Storyboard.TargetName="MySolidColorBrush" Storyboard.TargetProperty="Color" From="DarkOrange" To="SteelBlue" Duration="0:0:1" />
        </Storyboard>
      </BeginStoryboard>
    </EventTrigger>
  </TextBlock.Triggers>
</TextBlock>

Which does work, when it loads, but I would like to animate it when the text is updated.

我发现的一个例子之一是当值更改时,textblock and Animate textblock 使用。也加载。在评论中,它提到“您必须将notifyOntargetUpdupped属性设置为绑定中的true”,但似乎不存在NotifyOntargetUpduped(在Winui?)中。

答案可能很简单,但我对此有些迷失。我还使用MVVM模式,因此避免后面的代码,但是看来这简单的东西不需要它来查看微软的示例吗?虽然我请随时纠正我。谢谢!

Hi I'm trying to figure out how to animate a TextBlock when it's property updates. New to WinUI3/XAML but I've found https://learn.microsoft.com/en-us/dotnet/desktop/wpf/advanced/how-to-apply-animations-to-text which gives a lot of examples that work, but they all use TextBlock.Loaded. I've tried changing it to TargetUpdated, SourceUpdated, FrameWork.x, Binding.x etc, all crash so I'm not sure what to put in there. Lot's of example on the net, but ones I can find seem to be either WPF or UWP which still crash.

My code which is pretty much same as MS's examples sans binding:

<TextBlock Text="{x:Bind Path=Viewmodel.UpdatedText, Mode=OneWay}">
  <TextBlock.Foreground>
    <SolidColorBrush x:Name="MySolidColorBrush" Color="Maroon" />
  </TextBlock.Foreground>

  <TextBlock.Triggers>
    <EventTrigger RoutedEvent="TextBlock.Loaded">
      <BeginStoryboard>
        <Storyboard>
          <ColorAnimation AutoReverse="True" Storyboard.TargetName="MySolidColorBrush" Storyboard.TargetProperty="Color" From="DarkOrange" To="SteelBlue" Duration="0:0:1" />
        </Storyboard>
      </BeginStoryboard>
    </EventTrigger>
  </TextBlock.Triggers>
</TextBlock>

Which does work, when it loads, but I would like to animate it when the text is updated.

One of the examples I found was Animate textblock when the value changes, but it uses .Loaded as well. In the comments it mentions "You have to set the NotifyOnTargetUpdated property to true in the Binding", but NotifyOnTargetUpdated doesn't seem to exist (in WinUI?).

Answer is probably simple but I'm a bit lost on this. I'm also using MVVM pattern so avoiding code behind, but it seems like something this simple shouldn't need it looking at Microsoft's examples? Though If I'm please feel free to correct me. Thanks!

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

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

发布评论

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

评论(1

记忆で 2025-01-27 14:15:02

在这样的情况下,试图“避免代码”通常是一个错误。与视图相关的逻辑应使用编程语言在视图(或控件)中实现。这确实以任何方式破坏MVVM模式,因为该模式并不是要首先试图从视图中消除代码。

使用 markup 语言(例如XAML)实现Fary Advanced UI逻辑通常是一个对抗者,在UWP和Winui 3的情况下,甚至不可能使用触发器来实现您想要的东西。

引用 reactiveiveui docs 简洁的语言比XAML,虽然您可以在不编写任何代码的情况下创建整个复杂的视图,但结果将是无与伦比的,难以阅读的混乱。”

我的建议是,以编程为动画,直接在视图的代码范围内或作为附件行为或自定义控件的一部分。

Trying to "avoid code-behind" in cases like this is usually a mistake. View-related logic should be implemented in the view (or control) using a programming language. This does not break the MVVM pattern in any way as the pattern is not about trying to eliminate code from the views in the first place.

Using a markup language such as XAML to implement farily advanced UI logic is generally an antipattern and in the case of UWP and WinUI 3, it's not even possible to use triggers to accomplish what you want.

To quote the ReactiveUI docs, "C# is a much more expressive, more concise language than XAML, and while it may be possible for you to create an entire complex view without writing any code, the result will be an unmaintainable, difficult to read mess."

My suggestion would be to implement the animation programmtically, either directly in the code-behind of the view or as part of an attached behaviour or custom control.

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