繁体   English   中英

UWP中一个CS文件的不同XAML布局结构

[英]Different XAML Layout Structure for one CS file in UWP

我们需要在UWP应用程序中为相同的.cs创建4种不同的.xaml布局。 这些布局的结构取决于来自db的值“questionType”。

布局的每个变体应包含相同的控件但位于不同的位置。(即每个变体应包含一个图像,一个richTextEditor和包含4个无线电的radioGroup)

例如,如果questionType = 1,图像应该位于屏幕的左侧,如果questionType = 2,那么它应该位于富文本编辑器的顶部,并且无线电应该水平放置...

我们已经考虑过并尝试过的事情:

  1. 到目前为止,我们已经尝试过可视状态管理器,但不幸的是,通过使用它我们只能更改对齐而不是控件的位置。

  2. 我们还检查了条件xaml,但它似乎只是版本适应性。

  3. 能见度变化的面板。 但我们决定不采用这种解决方案,因为它非常难看。

任何将我们指向正确方向的人都将不胜感激。

谢谢。

编辑:

<VisualState x:Name="Layout1">
       <Storyboard>
         <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.HorizontalAlignment)"
           Storyboard.TargetName="ContentRoot">
           ...             
         </ObjectAnimationUsingKeyFrames>          
       </Storyboard>
     </VisualState> 

VisualStateManager可以更改您在元素上定义的任何属性,而不仅仅是Alignments。

为简单起见,我使用两个Border来表示ImageRichTextBox 最初,Image位于左侧,然后我使用VisualStateManager转到另一个可视状态,其中Image位于顶部。 请注意,属性(Grid.Column)(Grid.Row)的更改就像(FrameworkElement.HorizontalAlignment)

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" /> 
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" /> 
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>

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

    <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="imageControl"
                        Storyboard.TargetProperty="(Grid.Row)">
                        <DiscreteObjectKeyFrame KeyTime="0" Value="0" />
                    </ObjectAnimationUsingKeyFrames>
                </Storyboard>
            </VisualState>
        </VisualStateGroup>            
    </VisualStateManager.VisualStateGroups>
</Grid>    

在后面的代码中,根据值questionType更改VisualState

if (questionType == 1) 
   return; //Layout1 is the default state
else if (questionType ==  2)
    VisualStateManager.GoToState(this, "Layout2", true);                         

还有其他方法,例如使用StackPanel来托管控件,最初是水平定向的,并将其更改为垂直定向在另一个可视状态。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM