Windows store apps 如何为XAML样式的边框定义情节提要

Windows store apps 如何为XAML样式的边框定义情节提要,windows-store-apps,winrt-xaml,windows-8.1,Windows Store Apps,Winrt Xaml,Windows 8.1,有没有办法以XAML样式定义TargetType边框的情节提要? 以下是我努力实现的目标: <Style x:Key="MyBorderStyle" TargetType="Border"> <Setter Property="VisualStateManager.VisualStateGroups"> <Setter.Value> <VisualStateGroup>

有没有办法以XAML样式定义TargetType边框的情节提要? 以下是我努力实现的目标:

<Style x:Key="MyBorderStyle" TargetType="Border">

    <Setter Property="VisualStateManager.VisualStateGroups">

        <Setter.Value>
            <VisualStateGroup>
                <VisualStateGroup.States>
                    <VisualState x:Name="Normal">
                        <Storyboard RepeatBehavior="Forever">
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Stroke">
                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PaperDark}"/>
                            </ObjectAnimationUsingKeyFrames>
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="StrokeThickness">
                                <DiscreteObjectKeyFrame KeyTime="0" Value="2"/>
                            </ObjectAnimationUsingKeyFrames>
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="StrokeDashArray">
                                <DiscreteObjectKeyFrame KeyTime="0" Value="3, 3"/>
                                <DiscreteObjectKeyFrame KeyTime="00:00:0.200" Value="3, 3"/>
                                <DiscreteObjectKeyFrame KeyTime="00:00:0.400" Value="3, 3"/>
                            </ObjectAnimationUsingKeyFrames>
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="StrokeDashOffset">
                                <DiscreteObjectKeyFrame KeyTime="0" Value="3"/>
                                <DiscreteObjectKeyFrame KeyTime="00:00:0.200" Value="0"/>
                                <DiscreteObjectKeyFrame KeyTime="00:00:0.400" Value="3"/>
                            </ObjectAnimationUsingKeyFrames>
                        </Storyboard>
                    </VisualState>
                </VisualStateGroup.States>
            </VisualStateGroup>
        </Setter.Value>
    </Setter>        
</Style>

但这是行不通的

我还尝试了更多的方法,但都没能成功。 我想在MyStyles.XAML中定义此样式,然后在MainPage.XAML中使用它,如:

<Border Style="{StaticResource MyBorderStyle}">
Some code...
</Border>

一些代码。。。

请帮助。

为什么需要在样式中使用它? 只需给故事板命名并在参考资料下声明即可

  <Page.Resources>
      <Storyboard x:Name="Bordersb" RepeatBehavior="Forever">
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Stroke" Storyboard.TargetName="PART_BORDER">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PaperDark}"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="StrokeThickness">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="2"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="StrokeDashArray" Storyboard.TargetName="PART_BORDER">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="3, 3"/>
                            <DiscreteObjectKeyFrame KeyTime="00:00:0.200" Value="3, 3"/>
                            <DiscreteObjectKeyFrame KeyTime="00:00:0.400" Value="3, 3"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="StrokeDashOffset" Storyboard.TargetName="PART_BORDER">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="3"/>
                            <DiscreteObjectKeyFrame KeyTime="00:00:0.200" Value="0"/>
                            <DiscreteObjectKeyFrame KeyTime="00:00:0.400" Value="3"/>
                        </ObjectAnimationUsingKeyFrames>
         </Storyboard>
  </Page.Resources>

<Border x:Name="PART_BORDER>
 Some code...
</Border>

也许你需要用和来代替

事实上,把它擦掉。视觉状态需要定义为模板的一部分,因此只需重新定义整个模板。否则-是的,您需要在XAML或代码隐藏中定义
情节提要
,并手动运行它们,而不是依赖视觉状态


如果您真的不想更改整个模板,也许您可以在运行时定义一个资源并修改代码隐藏中的可视状态,但这听起来有点老套,可靠性可能有问题。

只需将其包装在
网格中
并在那里定义状态即可。如果要重用它,请将其包装在
UserControl
中。像这样(有效):


5.
15
25,5
45
请记住,使用“混合”定义视觉状态

阅读:


祝你好运

我之前尝试过这个选项,但没有成功。我想知道目标属性“Stroke”或“StrokeDashArray”等是否可用于XAML元素我知道它可用于矩形,并且它可用于矩形。@ashishnirkhe yea,Border没有Stroke,StrokeThickness等。。财产。使用BoderThickness、BorderBrush等。。属性。@asitis,但他想让它们产生特定的效果。除了边框没有模板作为快速提醒外,厚度不能用框架以外的任何东西来设置动画。谢谢@Jerry:)我实际上是在尝试边框样式的字幕类型(边框由破折号组成,并不断移动,就像我们在Photoshop中选择元素时得到的一样)。我能够在xaml元素矩形的笔划中得到它:),但不确定如何在边框中得到它,因为我没有看到StrokeDashArray或StrokeDashOffset作为边框。我知道。我回答了你的另一个问题。只有图形对象具有这些特性。确定。不过,如果能看到那些带有边框的房产,那就太好了。谢谢Jerry@ashishnirkhe边框内的矩形基本上是一样的。你做了正确的事情,没有在这里使用分立的故事板
 Bordersb.Begin();
<Grid>
    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="VisualStateGroup">
            <VisualState x:Name="VisualState0">
                <Storyboard>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.Background)" Storyboard.TargetName="border">
                        <DiscreteObjectKeyFrame KeyTime="0">
                            <DiscreteObjectKeyFrame.Value>
                                <SolidColorBrush Color="Red"/>
                            </DiscreteObjectKeyFrame.Value>
                        </DiscreteObjectKeyFrame>
                    </ObjectAnimationUsingKeyFrames>
                    <ColorAnimation Duration="0" To="#FF2E00FF" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="border" d:IsOptimized="True"/>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderThickness)" Storyboard.TargetName="border">
                        <DiscreteObjectKeyFrame KeyTime="0">
                            <DiscreteObjectKeyFrame.Value>
                                <Thickness>5</Thickness>
                            </DiscreteObjectKeyFrame.Value>
                        </DiscreteObjectKeyFrame>
                    </ObjectAnimationUsingKeyFrames>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.CornerRadius)" Storyboard.TargetName="border">
                        <DiscreteObjectKeyFrame KeyTime="0">
                            <DiscreteObjectKeyFrame.Value>
                                <CornerRadius>15</CornerRadius>
                            </DiscreteObjectKeyFrame.Value>
                        </DiscreteObjectKeyFrame>
                    </ObjectAnimationUsingKeyFrames>
                </Storyboard>
            </VisualState>
            <VisualState x:Name="VisualState1">
                <Storyboard>
                    <ColorAnimation Duration="0" To="#FF2EFF00" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="border" d:IsOptimized="True"/>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.Background)" Storyboard.TargetName="border">
                        <DiscreteObjectKeyFrame KeyTime="0">
                            <DiscreteObjectKeyFrame.Value>
                                <SolidColorBrush Color="#FF0017FF"/>
                            </DiscreteObjectKeyFrame.Value>
                        </DiscreteObjectKeyFrame>
                    </ObjectAnimationUsingKeyFrames>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderThickness)" Storyboard.TargetName="border">
                        <DiscreteObjectKeyFrame KeyTime="0">
                            <DiscreteObjectKeyFrame.Value>
                                <Thickness>25,5</Thickness>
                            </DiscreteObjectKeyFrame.Value>
                        </DiscreteObjectKeyFrame>
                    </ObjectAnimationUsingKeyFrames>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.CornerRadius)" Storyboard.TargetName="border">
                        <DiscreteObjectKeyFrame KeyTime="0">
                            <DiscreteObjectKeyFrame.Value>
                                <CornerRadius>45</CornerRadius>
                            </DiscreteObjectKeyFrame.Value>
                        </DiscreteObjectKeyFrame>
                    </ObjectAnimationUsingKeyFrames>
                </Storyboard>
            </VisualState>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>
    <Border x:Name="border" BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="234" VerticalAlignment="Top" Width="303" Margin="75,75,0,0"/>
</Grid>