引用Xaml代码的其他部分 [英] Referencing other parts of Xaml Code

查看:93
本文介绍了引用Xaml代码的其他部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用触发器更换bool时播放动画。

我按下按钮点击动画,如下图所示:

 <   Grid.Triggers  >  
< EventTrigger SourceName = StartBtn RoutedEvent = < span class =code-keyword> Button.Click >
< EventTrigger.Actions >
< BeginStoryboard x:名称 = MyStoryboard > ;
< 故事板 x:名称 = Pulse_Animation_Story RepeatBehavior = 1x >
< DoubleAnimationUsingKeyFrames Storyboard.TargetProp erty = (UIElement.RenderTransform)。(TransformGroup.Children)[2]。(RotateTransform.Angle ) Storyboard.TargetName = regularPolygon >
< ; EasingDoubleKeyFrame KeyTime < span class =code-keyword> = 0 = 0 / >
< EasingDoubleKeyFrame KeyTime = 0:0:4 = 4320 / < span class =code-keyword>>
< / DoubleAnimationUsingKeyFrames >
< DoubleAnimationUsingKeyFrames Storyboard.TargetProperty = < span class =code-keyword>(UIElement.RenderTransform)。(TransformGroup.Children)[2]。(RotateTransform.Angle) Storyboard.TargetName = regularPolygon1 >
< EasingDoubleKeyFrame KeyTime = 0 = 90 / < span class =code-keyword>>
< EasingDoubleKeyFrame KeyTime = 0:0:4 = 450 / >
< / DoubleAnimationUsingKeyFrames >
< / Storyboard >
< / BeginStoryboard >
< / EventTrigger.Actions >
< / EventTrigger >





但是当我尝试将按钮单击更改为数据触发器,如下所示:



 <   Grid.Style  >  
< 样式 >
< Style.Triggers > ;
< DataTrigger 绑定 = {Binding Path = HasAnimations } = True >
< DataTrigger.EnterActions >
<! - REFERENCE STORY1 HERE - >
< span class =code-keyword>< BeginStoryboard x:Name = MyStoryboard2 >
< Storyboard x:名称 = < span class =code-keyword> Pulse_Animation_Story2 RepeatBehavior = 1x >
< DoubleAnimationUsingKeyFrames Storyboard.TargetProperty = (UIElement.RenderTransform)。(TransformGroup.Children)[2]。( RotateTransform.Angle) Storyboard.TargetName = regularPolygon >
< EasingDoubleKeyFrame KeyTime = 0 = 0 / >
< EasingDoubleKeyFrame KeyTime = 0:0:4 = 4320 / >
< / DoubleAnimationUsingKeyFrames >
< DoubleAnimationUsingKeyFrames Storyboard.TargetProperty = (UIElement.RenderTransform)。(TransformGroup.Children)[2]。(RotateTransform.Angle) Storyboard.TargetName = regularPolygon1 >
< EasingDoubleKeyFrame KeyTime = 0 = 90 / >
< EasingDoubleKeyFrame KeyTime = 0:0:4 = < span class =code-keyword> 450
/ >
< / DoubleAnimationUsingKeyFrames >
< / Storyboard >
< / BeginStoryboard >
< / DataTrigger.EnterActions >
< / DataTrigger >

< / Style.Triggers >
< / Style >
< / Grid.Style >







我收到错误无法在样式设定器上设置TargetName属性



是否可以参考之前的创建故事板来克服这个问题?

或者我如何访问我想要动画的多边形?



谢谢

解决方案

因此,在阅读了许多指南和教程后,试图将成员变量bool链接到数据触发器而没有运气,我创建了自己的解决方案。





如果有人在将来偶然发现这个问题,或者是WPF控件或Xaml的新手,你就可以编辑Xaml.cs文件并想要一个程序化的解决方案....





首先,我把我的故事板拉出来了装配工。并将它倾倒在如下资源中:





xmlns:NameSpace =clr-namespace:LetMakeACLock



xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation

xmlns:x =http://schemas.microsoft .com / winfx / 2006 / xaml

xmlns:d =http://schemas.microsoft.com/expression/blend/2008

xmlns:mc = http://schemas.openxmlformats.org/markup-compatibility/2006

xmlns:ed =http://schemas.microsoft.com/expression/2010/drawing

xmlns:i =http://schemas.microsoft.com/expression/2010/interactivity

xmlns:ei =http://schemas.microsoft.com/expression/ 2010 / interaction

mc:Ignorable =d



d:DesignWidth =100d:DesignHeight =100Width = 100高度=100背景={x:空}BorderBrush =#FF009B76>



< button.resources>

< storyboard x:name =Animate_Handsx:key =Animate_Handsrepeatbehavior =永远>

***动画在这里**







$ b





接下来在cs代码中我创建了我的布尔属性并在set上添加了一个函数:



private bool m_playAnimation;

public bool playAnimation

{

get {return m_playAnimation; }

set

{

m_playAnimation = value;

if(m_playAnimation)

{

play();

}

}

}



和我的播放功能我把:



private void play()

{

故事板CurrentAnimation =(故事板)this.Resources [0 ];

CurrentAnimation.Begin();

}



资源接受int索引或字符串相关到x:名字



由于我是Xaml和WPF的新手,这可能不是最好的解决方案,但考虑到时间限制,它对我来说效果不错。 />


我仍​​然在寻找如何将成员bool设置为数据触发器的答案,就像我想要的那样。但我怀疑只是设置一个bool不会引发触发器需要的事件。


这种方法怎么样: http://msdn.microsoft.com/en-us/library/vstudio/aa970679%28v=vs.90%29.aspx [ ^ ]

Hi, I'm trying to get an animation to play when a bool changes using triggers.
I have my Animation working on a button click as shown below:

<Grid.Triggers>
                <EventTrigger SourceName="StartBtn" RoutedEvent="Button.Click">
                    <EventTrigger.Actions>
                        <BeginStoryboard x:Name="MyStoryboard">
                            <Storyboard x:Name="Pulse_Animation_Story" RepeatBehavior="1x">
                                <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)" Storyboard.TargetName="regularPolygon">
                                    <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
                                    <EasingDoubleKeyFrame KeyTime="0:0:4" Value="4320"/>
                                </DoubleAnimationUsingKeyFrames>
                                <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)" Storyboard.TargetName="regularPolygon1">
                                    <EasingDoubleKeyFrame KeyTime="0" Value="90"/>
                                    <EasingDoubleKeyFrame KeyTime="0:0:4" Value="450"/>
                                </DoubleAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger.Actions>
                </EventTrigger>



However when I attempt to change the button click to a data trigger like so:

<Grid.Style>
               <Style >
                   <Style.Triggers>
                       <DataTrigger Binding="{Binding Path=HasAnimations}" Value="True">
                           <DataTrigger.EnterActions>
                               <!--REFERENCE STORY1 HERE-->
                               <BeginStoryboard x:Name="MyStoryboard2">
                                   <Storyboard x:Name="Pulse_Animation_Story2" RepeatBehavior="1x">
                                       <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)" Storyboard.TargetName="regularPolygon">
                                           <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
                                           <EasingDoubleKeyFrame KeyTime="0:0:4" Value="4320"/>
                                       </DoubleAnimationUsingKeyFrames>
                                       <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)" Storyboard.TargetName="regularPolygon1">
                                           <EasingDoubleKeyFrame KeyTime="0" Value="90"/>
                                           <EasingDoubleKeyFrame KeyTime="0:0:4" Value="450"/>
                                       </DoubleAnimationUsingKeyFrames>
                                   </Storyboard>
                               </BeginStoryboard>
                           </DataTrigger.EnterActions>
                       </DataTrigger>

                   </Style.Triggers>
               </Style>
           </Grid.Style>




I get the error "TargetName property cannot be set on a Style Setter"

Is it possible to reference the previously created storyboard to overcome this issue?
Or how do i access my polygons which I want to animate?

Thank you

解决方案

So after reading many guides and tutorials trying to link a member variable bool to a data trigger with no luck I created my own solution.


If anyone stumbles upon this in the future or is new to WPF controls or Xaml and you are able to edit the Xaml.cs file and want a programmatic solution....


First of all I pulled my story board out of the trigger. and dumped it in resources like so:


xmlns:NameSpace="clr-namespace:LetMakeACLock"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
mc:Ignorable="d"

d:DesignWidth="100" d:DesignHeight="100" Width="100" Height="100" Background="{x:Null}" BorderBrush="#FF009B76">

<button.resources>
<storyboard x:name="Animate_Hands" x:key="Animate_Hands" repeatbehavior="Forever">
***DO ANIMATION HERE**







Next in the cs code I created my boolean property and added a function on set:

private bool m_playAnimation;
public bool playAnimation
{
get { return m_playAnimation; }
set
{
m_playAnimation= value;
if(m_playAnimation)
{
play();
}
}
}

and in my play function I put:

private void play()
{
Storyboard CurrentAnimation = (Storyboard)this.Resources[0];
CurrentAnimation.Begin();
}

Resource accepts an int index or a string relating to x:Name

Since I'm new to Xaml and WPF this may not be the best solution, but given time restraints it works well enough for me.

I am still looking for an answer on how to set a member bool to a data trigger like the one I wanted. but I suspect merely setting a bool does not raise an event that the trigger would require.


What about this approach: http://msdn.microsoft.com/en-us/library/vstudio/aa970679%28v=vs.90%29.aspx[^]?


这篇关于引用Xaml代码的其他部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆