【问题标题】:How to convert VisualState xaml to c#如何将 VisualState xaml 转换为 c#
【发布时间】:2017-02-13 11:19:00
【问题描述】:

XAML 中的视觉状态

<VisualStateManager.VisualStateGroups>
           <VisualStateGroup x:Name="SectionHeader">
               <VisualState x:Name="SectionHeaderNormal">
                   <VisualState.StateTriggers>
                       <AdaptiveTrigger MinWindowWidth="1200"/>
                   </VisualState.StateTriggers>
                   <Storyboard>
                       <ObjectAnimationUsingKeyFrames Storyboard.TargetName="txtUser" Storyboard.TargetProperty="Style">
                           <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource usernameStyle}"/>
                       </ObjectAnimationUsingKeyFrames>
                       <ObjectAnimationUsingKeyFrames Storyboard.TargetName="txtName" Storyboard.TargetProperty="Style">
                           <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource nameStyle}"/>
                       </ObjectAnimationUsingKeyFrames>
                   </Storyboard>
               </VisualState>
        </VisualStateGroup>
</VisualStateManager.VisualStateGroups>

我需要通过 C# 代码以编程方式创建此 XAML 代码。

当前代码

            var vsg = new VisualStateGroup();
            var vs = new VisualState();
            vs.StateTriggers.Add(new AdaptiveTrigger
            {
                MinWindowWidth = 1200.0
            });

如何创建故事板并添加这些属性?

【问题讨论】:

  • 试一试,然后在遇到困难时在此处寻求具体问题的帮助。
  • XAML 本质上将对象序列化为 XML,因此您视为元素或属性的任何内容也作为类或属性存在。您已经使用了 StateTriggers 属性。还有一个Storyboard 属性。你试过这个吗?你遇到问题了吗?
  • 如果您查看VisualState 的文档,您可以看到它有[ContentPropertyAttribute("Storyboard")],因此您可以将情节提要分配给同名的属性,即使在XAML 中它没有被包装在&lt;VisualState.Storyboard&gt;...&lt;/VisualState.Storyboard&gt;

标签: c# xaml uwp visualstatemanager


【解决方案1】:

如果您想在后面的代码中使用VisualState,您应该可以将Setter 添加到VisualStateSetterBaseCollection。此外,我们应该能够将VisualState 添加到VisualStateGroup。然后我们可以将VisualStateGroup 添加到我们的控件中。

要在后面的代码中获取Style,我们应该能够使用FrameworkElement.Resources

例如:

<Page.Resources>
    <Style x:Key="usernameStyle" TargetType="TextBlock">
        <Setter Property="Foreground" Value="Red" />
    </Style>
</Page.Resources>

<Grid Name="MyGrid" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <TextBlock Name="txtUser" Text="555555" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
</Grid>

背后的代码:

public MainPage()
{
    this.InitializeComponent();
    var vsg = new VisualStateGroup();
    var vs = new VisualState();
    Style appButtonStyle = (Style)this.Resources["usernameStyle"];
    vs.StateTriggers.Add(new AdaptiveTrigger
    {
        MinWindowWidth = 1080
    });

    vs.Setters.Add(new Setter
    {
        Target = new TargetPropertyPath
        {
            Path = new PropertyPath("(TextBlock.Style)"),
            Target = txtUser
        },
        Value = appButtonStyle
    });

    vsg.States.Add(vs);

    VisualStateManager.GetVisualStateGroups(MyGrid).Add(vsg);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-26
    • 1970-01-01
    • 2012-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多