C# 以编程方式添加控件时VisualState不工作
我有一个自定义工具栏,可以显示“忙”状态作为一个动画时钟。当我通过XAML(上半部分)添加这个动画时钟控件时,它工作得很好,我可以看到旋转的箭头。当我以编程方式添加控件时,它不会“旋转” 这是按钮的样式:C# 以编程方式添加控件时VisualState不工作,c#,silverlight,xaml,C#,Silverlight,Xaml,我有一个自定义工具栏,可以显示“忙”状态作为一个动画时钟。当我通过XAML(上半部分)添加这个动画时钟控件时,它工作得很好,我可以看到旋转的箭头。当我以编程方式添加控件时,它不会“旋转” 这是按钮的样式: <Style TargetType="controls:IdattToolbarBusy"> <Setter Property="Template"> <Setter.Value> &
<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>
最后一行代码是我试图修复一个问题时添加的,但仍然不起作用。当控件最初创建时,一切正常,它在Silverlight4中。现在我们升级到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中,我看不到我的组。我认为(猜测)Rob Jacobs是正确的——我会尝试在“OnApplyTemplate”或“Loaded”中添加GoToState
调用
如果那不起作用。。。您确定“正常”不是控件的默认状态,因此在尝试更改时不会被更新吗?似乎应该为默认控件状态保留“正常”,并且您应该将状态重命名为“忙碌”或其他状态。如果不添加它,而是切换其可见性,您会得到相同的结果吗?我个人也会选择其他一些方法来做你想做的事情,我们仍然可以交换,但我很好奇这里的区别。Chris,toolbar基于ContentControl和添加到toolbar.items的top one(动画)时钟。然后我将StackPanel放在里面,并将第二个(非工作)添加到这个StackPanel程序中。没有办法轻易测试你的提议。你知道为什么它不起作用吗?我正在发布“编辑到问题”以提供有关此忙碌按钮的更多详细信息…我认为问题可能是该控件尚未添加到可视化树中,因此VSM无法找到它。对于aa,您试图获取控件上的状态(此),但它们实际上在子网格中。就是这样。使用OnApplyTemplate是有效的。Normal是默认状态,但由于某些原因,它并非在所有情况下都自动启动。
public class IdattToolbarBusy : IdattToolbarButton
{
public IdattToolbarBusy()
{
this.DefaultStyleKey = typeof(IdattToolbarBusy);
var aa = VisualStateManager.GetVisualStateGroups(this);
VisualStateManager.GoToState(this, "Normal", true);
}
}
public class IdattToolbarBusy : IdattToolbarButton
{
public IdattToolbarBusy()
{
this.DefaultStyleKey = typeof(IdattToolbarBusy);
var aa = VisualStateManager.GetVisualStateGroups(this);
}
public override void OnApplyTemplate()
{
VisualStateManager.GoToState(this, "Normal", true);
}
}