C# Page.xaml中使用UserControl的VisualStateManager在UWP应用程序中不工作

C# Page.xaml中使用UserControl的VisualStateManager在UWP应用程序中不工作,c#,uwp,uwp-xaml,C#,Uwp,Uwp Xaml,在我的UWP应用程序中,在代码隐藏中 VisualStateManager.GoToState(this,"Layout2",false); 返回假;当我在page.xaml中使用使用UserControl的VisualStateManager xaml块时 这就是我的意思。这是使用用户控件的page.xaml: <local:MainUserControl x:Name="mainControl"> <local:MainUserControl.QuestionC

在我的UWP应用程序中,在代码隐藏中

VisualStateManager.GoToState(this,"Layout2",false); 
返回;当我在page.xaml中使用使用UserControl的VisualStateManager xaml块时

这就是我的意思。这是使用用户控件的page.xaml:

<local:MainUserControl x:Name="mainControl">
    <local:MainUserControl.QuestionContent>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="200"/>
                <RowDefinition Height="200"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="200"/>
                <ColumnDefinition Width="200"/>
            </Grid.ColumnDefinitions>

            <Border x:Name="imageControl" Background="Red" Height="200" Width="200" Grid.Row="0" Grid.Column="0"/>
            <Border x:Name="richTextBoxControl" Background="Yellow" Grid.Row="0" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>


            <VisualStateManager.VisualStateGroups>
                <VisualStateGroup>
                    <VisualState x:Name="Layout1"/>
                    <VisualState x:Name="Layout2">
                        <Storyboard>
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="imageControl" Storyboard.TargetProperty="(Grid.Column)">
                                <DiscreteObjectKeyFrame KeyTime="0" Value="1"/>
                            </ObjectAnimationUsingKeyFrames>
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="richTextBoxControl" Storyboard.TargetProperty="(Grid.Column)">
                                <DiscreteObjectKeyFrame KeyTime="0" Value="0"/>
                            </ObjectAnimationUsingKeyFrames>
                        </Storyboard>
                    </VisualState>
                </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>

        </Grid>

    </local:MainUserControl.QuestionContent>
</local:MainUserControl>
我也检查了两次我的名称空间,但无法解决这个问题

请注意,当我删除

<local:MainUserControl x:Name="mainControl">
<local:MainUserControl.QuestionContent> 

}

您需要将
VisualStateManager.VisualStateGroups
作为根面板
网格的立即子标记,如下所示:

<Grid>
    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup>
            <VisualState x:Name="Layout1"/>
            <VisualState x:Name="Layout2">
                <Storyboard>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="imageControl" Storyboard.TargetProperty="(Grid.Column)">
                        <DiscreteObjectKeyFrame KeyTime="0" Value="1"/>
                    </ObjectAnimationUsingKeyFrames>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="richTextBoxControl" Storyboard.TargetProperty="(Grid.Column)">
                        <DiscreteObjectKeyFrame KeyTime="0" Value="0"/>
                    </ObjectAnimationUsingKeyFrames>
                </Storyboard>
            </VisualState>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>
    <local:MainUserControl x:Name="mainControl">
        <local:MainUserControl.QuestionContent>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="200"/>
                    <RowDefinition Height="200"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="200"/>
                    <ColumnDefinition Width="200"/>
                </Grid.ColumnDefinitions>
                <Border x:Name="imageControl" Background="Red" Height="200" Width="200" Grid.Row="0" Grid.Column="0"/>
                <Border x:Name="richTextBoxControl" Background="Yellow" Grid.Row="0" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
            </Grid>
        </local:MainUserControl.QuestionContent>
    </local:MainUserControl>
</Grid>


然后,在这个XAML页面的代码隐藏中,您可以调用
VisualStateManager.GoToState(这个“Layout2”,false)
更改视觉状态。

我需要一个诊断程序来诊断此问题。或者我至少需要实现
MainUserControl
的方法。因为我创建了一个“UserControl”并设置了与“local:MainUserControl.QuestionContent”相同的内容,所以我成功地调用了
VisualStateManager.GoToState(this.mainControl,“Layout2”,false)
方法。@XavierXie MSFT感谢您的回复。请检查问题的更新部分。我刚刚添加了UserControl内容
<UserControl
x:Class="MyProject.Pages.MainUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyProject.Pages"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">


<Grid Background="#ededed">
    <Grid.RowDefinitions>
        <RowDefinition Height="227"/>
        <RowDefinition Height="625"/>
        <RowDefinition Height="227"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="692"/>
        <ColumnDefinition Width="1000"/>
        <ColumnDefinition Width="228"/>
    </Grid.ColumnDefinitions>

    <StackPanel Grid.Row="1" Grid.Column="1" Background="White" >

        <StackPanel x:Name="panelQuestion">
            <ContentPresenter Content="{x:Bind QuestionContent}"/>
        </StackPanel>

    </StackPanel>

</Grid>
namespace MyProject.Pages
{
public sealed partial class MainUserControl : UserControl
{

    public StackPanel cPanelQuestion;

    public MainUserControl()
    {
        this.InitializeComponent();
        cPanelQuestion = this.panelQuestion;

    }

    public object QuestionContent
    {
        get { return (object)GetValue(QuestionContentProperty); }
        set { SetValue(QuestionContentProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Body.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty QuestionContentProperty =
        DependencyProperty.Register("QuestionContent", typeof(object), typeof(MainUserControl), null);



}
<Grid>
    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup>
            <VisualState x:Name="Layout1"/>
            <VisualState x:Name="Layout2">
                <Storyboard>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="imageControl" Storyboard.TargetProperty="(Grid.Column)">
                        <DiscreteObjectKeyFrame KeyTime="0" Value="1"/>
                    </ObjectAnimationUsingKeyFrames>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="richTextBoxControl" Storyboard.TargetProperty="(Grid.Column)">
                        <DiscreteObjectKeyFrame KeyTime="0" Value="0"/>
                    </ObjectAnimationUsingKeyFrames>
                </Storyboard>
            </VisualState>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>
    <local:MainUserControl x:Name="mainControl">
        <local:MainUserControl.QuestionContent>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="200"/>
                    <RowDefinition Height="200"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="200"/>
                    <ColumnDefinition Width="200"/>
                </Grid.ColumnDefinitions>
                <Border x:Name="imageControl" Background="Red" Height="200" Width="200" Grid.Row="0" Grid.Column="0"/>
                <Border x:Name="richTextBoxControl" Background="Yellow" Grid.Row="0" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
            </Grid>
        </local:MainUserControl.QuestionContent>
    </local:MainUserControl>
</Grid>