通过代码后面添加的VisualStateManager不会触发Windows 10

时间:2016-08-01 15:28:01

标签: c# uwp windows-10-universal windows-10-mobile visualstatemanager

我有一个通过后面的代码动态创建的按钮。原因是因为每次按钮可见时我都想重复EntranceThemeTransition

<Grid x:Name="gridRoot">
    <Grid x:Name="gridNextKitty"  Visibility="{Binding IsNextKittyButtonVisible, Converter={StaticResource BooleanToVisibilityConverter}}">
        <Grid.ChildrenTransitions>
            <TransitionCollection>
                <EntranceThemeTransition FromHorizontalOffset="1000" FromVerticalOffset="0" IsStaggeringEnabled="False"/>
            </TransitionCollection>
        </Grid.ChildrenTransitions>
        <!--<Button x:Name="btnNextKitty" Content="Next Kitty" FontSize="40" Command="{Binding BtnNextKittyClick}" Style="{StaticResource ButtonMenuStyle}" Background="#74b025" Width="300" Height="80" Margin="24" HorizontalAlignment="Center" VerticalAlignment="Center"/>-->
    </Grid>
</Grid>

按钮添加如下

            if (viewModel.IsNextKittyButtonVisible)
            {
                btnNextKitty = new Button()
                {
                    Content = "Next Kitty",
                    FontSize = 40,
                    Command = viewModel.BtnNextKittyClick,
                    Style = (Style)Application.Current.Resources["ButtonMenuStyle"],
                    Background = new SolidColorBrush(Color.FromArgb(255, 116, 176, 37)),
                    Width = 300,
                    Height = 80,
                    Margin = new Thickness(24),
                    HorizontalAlignment = HorizontalAlignment.Center,
                    VerticalAlignment = VerticalAlignment.Center
                };

                gridNextKitty.Children.Add(btnNextKitty);
                addVisualStateManager();

            }
            else
            {
                gridNextKitty.Children.Remove(btnNextKitty);
                VisualStateManager.GetVisualStateGroups(gridRoot).Remove(vsg);
            }

我添加了VisualStateManager后面的代码,但是StateTriggers始终为真,即使MinWindowWidth&lt; 500。

    private void addVisualStateManager()
    {
        vsg = new VisualStateGroup();
        VisualState vs = new VisualState();
        vs.StateTriggers.Add(new AdaptiveTrigger
        {
            MinWindowWidth = 500,                
        });

        vs.Setters.Add(new Setter
        {
            Target = new TargetPropertyPath
            {
                Path = new PropertyPath("(FrameworkElement.Width)"),
                Target = btnNextKitty
            },

            Value = "240"
        });

        vsg.States.Add(vs);
        VisualStateManager.GetVisualStateGroups(gridRoot).Add(vsg);
    }

如何通过后面的代码使触发器工作?

2 个答案:

答案 0 :(得分:0)

您应该为0到500添加宽度的默认状态。 例如,空视觉状态。

vsg.States.Add(vs);

vs = new VisualState();
vs.StateTriggers.Add(new AdaptiveTrigger
{
    MinWindowWidth = 0,
});

vsg.States.Add(vs);

答案 1 :(得分:0)

我选择了不同的路线。而不是使用EntranceThemeTransition我决定使用Storyboard动画。

<Grid.Resources>
    <Storyboard x:Name="storyboardbtnNextKitty">
        <DoubleAnimation Storyboard.TargetName="btnNextKitty" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateX)" From="1000" To="0"  Duration="0:0:0.5">
            <DoubleAnimation.EasingFunction>
                <CubicEase EasingMode="EaseOut"/>
            </DoubleAnimation.EasingFunction>
        </DoubleAnimation>
    </Storyboard>
</Grid.Resources>

在后面的代码中,我订阅了ViewModel_PropertyChanged事件。当IsNextKittyButtonVisible为真时,我只是播放动画。

if (viewModel.IsNextKittyButtonVisible)
{
    storyboardbtnNextKitty.Begin();
}

该按钮在XAML中创建,而不是后面的代码。