我有自定义工具栏,可以将“忙”状态显示为动画时钟。当我通过XAML(顶部)添加这个动画时钟控件时,它工作正常,我可以看到旋转箭头。当我以编程方式添加控件时 - 它不会“旋转”
这是按钮的样式:
<Style TargetType="controls:IdattToolbarBusy">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="controls:IdattToolbarBusy">
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal">
<Storyboard>
<DoubleAnimation x:Name="minutesArrowAnimation"
Storyboard.TargetName="minutesArrowTransform"
Storyboard.TargetProperty="Angle"
Duration="0:0:2" RepeatBehavior="Forever" To="360" />
<DoubleAnimation x:Name="hoursArrowAnimation"
Storyboard.TargetName="hoursArrowTransform"
Storyboard.TargetProperty="Angle"
Duration="0:0:3" RepeatBehavior="Forever" To="360" />
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<StackPanel x:Name="PART_Container" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="5">
<Image Source="/IDATT.Infrastructure.SL;component/Images/img_status_busy.png" x:Name="PART_IconImage" Stretch="None" VerticalAlignment="Top" HorizontalAlignment="Center" />
<TextBlock x:Name="PART_CaptionTextBlock" FontSize="9" Padding="2,0,0,0" VerticalAlignment="Center" TextAlignment="Center" Text="{TemplateBinding Caption}" />
</StackPanel>
<Rectangle x:Name="minutesHand" Fill="#FFFF0000" Height="6" HorizontalAlignment="Left"
Margin="16, 9, 0, 0" RenderTransformOrigin="0.45,1.34"
Stroke="#FF000000" VerticalAlignment="Top" Width="1" >
<Rectangle.RenderTransform>
<RotateTransform x:Name="minutesArrowTransform"/>
</Rectangle.RenderTransform>
</Rectangle>
<Rectangle x:Name="hoursHand" Fill="#FFFF0000" Height="4" HorizontalAlignment="Left"
Margin="16, 12, 0, 0" RenderTransformOrigin="0.45,1.34"
Stroke="#FF000000" VerticalAlignment="Top" Width="2" >
<Rectangle.RenderTransform>
<RotateTransform x:Name="hoursArrowTransform"/>
</Rectangle.RenderTransform>
</Rectangle>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
当我使用代码添加控件时 - 它看起来像这样:
var busyIndicator = new IdattToolbarBusy { Caption = "Busy" };
this.StatusStackPanel.Children.Add(busyIndicator);
VisualStateManager.GoToState(busyIndicator, "Normal", true);
我尝试修复问题时添加了最后一行代码,但它仍无效。当最初创建控件时,一切都工作正常,它在Silverlight 4中。现在我们升级到SL 5并且它没有旋转。
我认为这是因为它由于某种原因不会触发“正常”状态,但无法弄清楚如何修复它。
编辑:
这是繁忙指标的代码:
public class IdattToolbarBusy : IdattToolbarButton
{
public IdattToolbarBusy()
{
this.DefaultStyleKey = typeof(IdattToolbarBusy);
var aa = VisualStateManager.GetVisualStateGroups(this);
VisualStateManager.GoToState(this, "Normal", true);
}
}
我为VisualStateManager放置了这行代码,但它不起作用。但是.. aa
变量有0个状态组。所以,似乎基于XAML的插入工作,但在代码VSM中看不到我的组..
答案 0 :(得分:1)
我认为(猜测)Rob Jacobs是正确的 - 我会尝试在&#34; OnApplyTemplate&#34;或者#34; Loaded&#34;中添加GoToState
调用。
public class IdattToolbarBusy : IdattToolbarButton
{
public IdattToolbarBusy()
{
this.DefaultStyleKey = typeof(IdattToolbarBusy);
var aa = VisualStateManager.GetVisualStateGroups(this);
}
public override void OnApplyTemplate()
{
VisualStateManager.GoToState(this, "Normal", true);
}
}
如果这不起作用......你确定&#34;正常&#34;是不是控件的默认状态,因此在您尝试更改时不会更新?看起来很正常&#34;正常&#34;应保留为默认控件状态,您应该将状态重命名为&#34; Busy&#34;或其他什么。