XAML 插入"Label.Text"的VisualState.Setters属性时页面崩溃

slwdgvem  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(193)

我有一个.NET MAUI xaml页面与标签和开关。当我尝试使用VisualState-setters更改标签文本值开关开/关,它的工作与仿真器和电话调试器模式,但当我下载到我的Android手机的页面崩溃时,试图打开!!这是代码的一部分重现问题:

<HorizontalStackLayout Grid.Row="2" Grid.Column="0" Margin="5" Spacing="7" BackgroundColor="White" HeightRequest="50">
                                
                <!-- Automatic-Manual -->
                <VerticalStackLayout Margin="2" >
                    <Label x:Name="LblAutoMan" Text="Automatic" VerticalOptions="Start" HorizontalOptions="Center" HeightRequest="20" WidthRequest="65"/>
                    <Switch IsToggled="True" HorizontalOptions="Start" VerticalOptions="Center" HeightRequest="10">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup Name="CommonStates">
                                <VisualState Name="On">
                                    <VisualState.Setters>
                                        <Setter Property="ThumbColor" Value="Green" />
                                        <Setter TargetName="LblAutoMan" Property="Label.Text" Value="Automatic" /> 
                                    </VisualState.Setters>
                                </VisualState>
                                <VisualState Name="Off">
                                    <VisualState.Setters>
                                        <Setter Property="ThumbColor" Value="Red" />
                                        <Setter TargetName="LblAutoMan" Property="Label.Text" Value="Manual" /> 
                                    </VisualState.Setters>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                    </Switch>
                </VerticalStackLayout>
               
            </HorizontalStackLayout>

如果我同时注解两行:<Setter TargetName="LblAutoMan"
有人能解决这个错误吗?

dffbzjpn

dffbzjpn1#

根据这个issue,你需要把你的<VisualStateGroup>包含在一个<VisualStateGroupList>标签中:

<HorizontalStackLayout Grid.Row="2" Grid.Column="0" Margin="5" Spacing="7" BackgroundColor="White" HeightRequest="50">
                            
    <!-- Automatic-Manual -->
    <VerticalStackLayout Margin="2" >
        <Label x:Name="LblAutoMan" Text="Automatic" VerticalOptions="Start" HorizontalOptions="Center" HeightRequest="20" WidthRequest="65"/>
        <Switch IsToggled="True" HorizontalOptions="Start" VerticalOptions="Center" HeightRequest="10">
            <VisualStateManager.VisualStateGroups>
                <VisualStateGroupList>
                    <VisualStateGroup Name="CommonStates">
                        <VisualState Name="On">
                            <VisualState.Setters>
                                <Setter Property="ThumbColor" Value="Green" />
                                <Setter TargetName="LblAutoMan" Property="Label.Text" Value="Automatic" /> 
                            </VisualState.Setters>
                        </VisualState>

                        <VisualState Name="Off">
                            <VisualState.Setters>
                                <Setter Property="ThumbColor" Value="Red" />
                                <Setter TargetName="LblAutoMan" Property="Label.Text" Value="Manual" /> 
                            </VisualState.Setters>
                        </VisualState>
                    </VisualStateGroup>
                </VisualStateManager.VisualStateGroups>
            <VisualStateGroupList>
        </Switch>
    </VerticalStackLayout>
</HorizontalStackLayout>

相关问题