110,566
社区成员
发帖
与我相关
我的任务
分享
<Style TargetType="Button" x:Key="BigButton" BasedOn="{StaticResource BaseButton}">
<Setter Property="Height" Value="80"/>
<Setter Property="Width" Value="80"/>
<Style.Triggers>
<EventTrigger RoutedEvent="ButtonBase.PreviewMouseDown">
<BeginStoryboard Storyboard="{StaticResource OnButtonClickStoryboard}"/>
</EventTrigger>
</Style.Triggers>
</Style>
<Storyboard x:Key="OnButtonClickStoryboard">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)">
<EasingDoubleKeyFrame KeyTime= "0:0:0.2" Value= "1.1" />
<EasingDoubleKeyFrame KeyTime= "0:0:0.4" Value= "1" />
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty= "(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)">
<EasingDoubleKeyFrame KeyTime= "0:0:0.2" Value= "1.1" />
<EasingDoubleKeyFrame KeyTime= "0:0:0.4" Value= "1" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
<Button Style="{StaticResource BigButton}" Name="btnUSBCANConfig" Click="btnUSBCANConfig_Click" RenderTransformOrigin="0.5,0.5" >
<Button.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Button.RenderTransform>
控制器设置
</Button>
Storyboard1.Completed += new EventHandler(Storyboard1_Completed);
}
void Storyboard1_Completed(object sender, EventArgs e)
{
buttonclick...
}
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Storyboard x:Key="OnButtonClickStoryboard" Completed="StoryboardCompleted(所有按钮?)">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)">
<EasingDoubleKeyFrame KeyTime= "0:0:0.2" Value= "1.1" />
<EasingDoubleKeyFrame KeyTime= "0:0:0.4" Value= "1" />
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty= "(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)">
<EasingDoubleKeyFrame KeyTime= "0:0:0.2" Value= "1.1" />
<EasingDoubleKeyFrame KeyTime= "0:0:0.4" Value= "1" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</ResourceDictionary>
好像不允许这种写法,必须指定应用的class去找到对应的事件
ResourceDictionary”根元素需要 x:Class 特性来支持 XAML 文件中的事件处理程序。请移除 Completed 事件的事件处理程序,或将 x:Class 特性添加到根元素
但这样就太麻烦了
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="分拣墙系统.Style.ButtonStyle">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../Style/FontStyle.xaml"/>
<ResourceDictionary Source="../Style/TextBlockStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style TargetType="{x:Type Button}" x:Key="BaseButton" BasedOn="{StaticResource BaseContentControl}">
<Setter Property="Background" Value="White"/>
<Setter Property="RenderTransformOrigin" Value="0.5,0.5"/>
<Setter Property="RenderTransform">
<Setter.Value>
<TransformGroup>
<ScaleTransform/>
<!--<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>-->
</TransformGroup>
</Setter.Value>
</Setter>
<EventSetter Event="Click" Handler="OnButtonClick"/>
</Style>
public partial class ButtonStyle
{
static EventHandler OnButtonStoryboardCompleted = null;
public void OnButtonClick(object sender, RoutedEventArgs e)
{
Button button = sender as Button;
Storyboard storyboard = new Storyboard();
// 动画...
Storyboard.SetTarget(doubleAnimationUsingKeyFrames1, button);
Storyboard.SetTarget(doubleAnimationUsingKeyFrames2, button);
storyboard.Begin();
}
private void Storyboard_Completed(object sender, EventArgs e)
{
Timeline timeline = ((sender as ClockGroup).Children[0]).Timeline;
Button button = Storyboard.GetTarget(timeline) as Button;
OnButtonStoryboardCompleted?.Invoke(button, e);
}
static public void RegisterStoryboardCompleted(EventHandler eventHandler)
{
var invocationList = OnButtonStoryboardCompleted?.GetInvocationList();
if (invocationList==null||!invocationList.Contains(eventHandler))
{
ButtonStyle.OnButtonStoryboardCompleted += eventHandler;
}
}
}
应用
<Button Style="{StaticResource BigButton}" Name="btnUSBCANConfig" >控制器设置</Button>
美中不足的是原本我想用下面这种方法去调用按钮的点击事件
ButtonAutomationPeer buttonAutomationPeer = new ButtonAutomationPeer(button);
IInvokeProvider iInvokeProvider = buttonAutomationPeer.GetPattern(PatternInterface.Invoke) as IInvokeProvider;
iInvokeProvider.Invoke() // 调用按钮click实际后台逻辑
会动画和click死循环,因为等于一直触发click了,于是只能上面代码那样调用一个回调函数OnButtonStoryboardCompleted
然后某个窗口里
ButtonStyle.RegisterStoryboardCompleted(OnButtonStoryboardCompleted);
void OnButtonStoryboardCompleted(object sender, EventArgs e)
{
if (sender == btnUSBCANConfig)
{
}
else if (sender==btnAddressConfig)
{
}
}
然后再xaml里删除掉所欲按钮的click事件
显得有点繁琐了,
如果能在动画完成后,invoke按钮click的时候,暂时禁止动画,然后阻塞invoke,invoke click执行完后恢复按钮的动画,下次手动click按钮时正常动画就好了