且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 WinRT XAML 中更改 TextBlock 的值时为其设置动画?

更新时间:2023-01-04 08:49:54

同样,不确定我们是否 100% 同意.但是,您仍然可以这样做:

Again, not sure if we are 100% agreeing. But, still, here's how you can do it:

public class MyViewModel : INotifyPropertyChanged
{
    public string Text { get; set; }
    public event PropertyChangedEventHandler PropertyChanged;
}

public void Loaded()
{
    var myBox = new TextBox();
    var myAni = new Storyboard();
    var MyVvm = new MyViewModel();

    // sensible approach
    myBox.TextChanged += (s, e) => myAni.Begin();

    // forced approach
    MyVvm.PropertyChanged += (s, e) =>
    {
        if (e.PropertyName.Equals("Text"))
            myAni.Begin();
    };
}

最终,您是自己应用的开发者.不是我.

In the end, you are the developer of your own app. not me.

如果你愿意使用行为,你也可以这样做:

If you are willing to use behaviors, you can also do the same thing this way:

<Page.Resources>
    <Storyboard x:Name="FadeAway">
        <DoubleAnimation Duration="0:0:1" To="0" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="textBox" d:IsOptimized="True"/>
    </Storyboard>
</Page.Resources>

<TextBox x:Name="textBox">
    <Interactivity:Interaction.Behaviors>
        <Core:EventTriggerBehavior EventName="TextChanged">
            <Media:ControlStoryboardAction Storyboard="{StaticResource FadeAway}"/>
        </Core:EventTriggerBehavior>
    </Interactivity:Interaction.Behaviors>
</TextBox>

我想您可以使用行为来获得纯"MVVM 方法.它为您提供 100% XAML,这让一些开发人员感到温暖和模糊;我明白了.而且,我喜欢行为.看.我不想在这里和你争论,只是顶部的方法肯定没有错误".

I suppose you can have your "pure" MVVM approach using a behavior. It gets you 100% XAML, and that makes some developers feel warm and fuzzy; I get that. And, I like behaviors. Look. I don't want to argue with you here, it's just that the top approach is certainly not "wrong".

了解有关行为的更多信息:http:///blog.jerrynixon.com/2013/10/everything-i-know-about-behaviors-in.html

Learn more about behaviors: http://blog.jerrynixon.com/2013/10/everything-i-know-about-behaviors-in.html

祝你好运.