XAML Xamarin VisualState.StateTriggers未在类型VisualState中找到

oewdyzsn  于 2023-04-27  发布在  其他
关注(0)|答案(1)|浏览(108)

我尝试在Xamarin中使用OrientationStateTrigger,这样我就可以根据设备的方向设置元素的样式。
我从这个question复制了一个例子
这是我的Xaml:

<ContentView.Content>
        <Grid x:Name="grid_master">
            <VisualStateManager.VisualStateGroups>
                <VisualStateGroup>
                    <VisualState
                    x:Name="Landscape">
                        <VisualState.StateTriggers>
                            <OrientationStateTrigger Orientation="Landscape" />
                        </VisualState.StateTriggers>
                        <VisualState.Setters>
                            <Setter Property="BackgroundColor" Value="Blue" />
                        </VisualState.Setters>
                    </VisualState>
                    <VisualState
                    x:Name="Portrait">
                        <VisualState.StateTriggers>
                            <OrientationStateTrigger Orientation="Portrait" />
                        </VisualState.StateTriggers>
                        <VisualState.Setters>
                            <Setter Property="BackgroundColor" Value="Red" />
                        </VisualState.Setters>
                    </VisualState>
                </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>

只有编译器不能识别VisualState.StateTriggers行,当鼠标悬停在该行上时,工具提示显示
'在类型' VisualState '中找不到可附加属性'StateTriggers'。'
我正在使用XamarinForms版本3.1.0.697729,所以它应该在这个版本中可用,对吗?
有谁能告诉我哪里出错了吗?谢谢

pdkcd3nj

pdkcd3nj1#

Xamarin.Forms.3.1.0.697729的版本有点过时,最新的是Xamarin.Forms.5.0.0.2578
下面的示例代码我已经在Xamarin.Forms.5.0.0.2578上测试过了,当从Landscape切换到Portrait时,背景可以按照预期变化。您可以升级它来看看它是否有效。

<!-- OrientationStateTrigger demo --> 
<Style x:Key="OrientationStateTriggerPageStyle"
               TargetType="ContentPage">
       <Setter Property="VisualStateManager.VisualStateGroups">
                <VisualStateGroupList>
                    <VisualStateGroup>
                        <VisualState x:Name="Portrait">
                            <VisualState.StateTriggers>
                                <OrientationStateTrigger Orientation="Portrait" />
                            </VisualState.StateTriggers>
                            <VisualState.Setters>
                                <Setter Property="BackgroundColor"
                                        Value="Blue" />
                            </VisualState.Setters>
                        </VisualState>
                        <VisualState x:Name="Landscape">
                            <VisualState.StateTriggers>
                                <OrientationStateTrigger Orientation="Landscape" />
                            </VisualState.StateTriggers>
                            <VisualState.Setters>
                                <Setter Property="BackgroundColor"
                                        Value="Red" />
                            </VisualState.Setters>
                        </VisualState>
                    </VisualStateGroup>
                </VisualStateGroupList>
         </Setter>
</Style>

<ContentPage
          -- omitted for brevity

             Style="{StaticResource OrientationStateTriggerPageStyle}">

    <Grid Margin="20">
        <Label Text="The page background color depends upon the device orientation."
               HorizontalOptions="Center"
               VerticalOptions="Center" />
    </Grid>

</ContentPage>

相关问题