Xamarin.Forms ControlTemplate 绑定在首次绘制控件后失败

问题描述

我有一个 ControlTemplate修改 Xamarin.Forms 中单选按钮的外观。
我从https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/radiobutton#redefine-radiobutton-appearance

借来/更新

看起来很棒,页面创建时的外观也很完美。
我希望单选按钮中的图像根据绑定而改变,但它没有这样做。
绑定属性修改OnPropertyChanged()调用,但绑定没有被触发。

这是一般外观(其中两个 RadioButton 并排)

enter image description here

同样,外观和 Converter 以及一切正常,但仅限于第一次绘制页面时。

这是控制模板

<ControlTemplate x:Key="RadioButtonTemplate">
    <Frame 
               BackgroundColor="#30888888"
               HasShadow="False"
               HeightRequest="{TemplateBinding HeightRequest}"
               WidthRequest="{TemplateBinding WidthRequest}"
               HorizontalOptions="Start"
               VerticalOptions="StartAndExpand"
               Padding="0">
        <visualstatemanager.VisualStateGroups>
            <VisualStateGroupList>
                <VisualStateGroup x:Name="CheckedStates">
                    <VisualState x:Name="Checked">
                        <VisualState.Setters>
                            <Setter Property="BorderColor" Value="Accent" />
                            <Setter TargetName="check"
                                        Property="Opacity"
                                        Value="1" />
                            <Setter TargetName="checkrim"
                                        Property="Opacity"
                                        Value="1" />
                        </VisualState.Setters>
                    </VisualState>
                    <VisualState x:Name="Unchecked">
                        <VisualState.Setters>
                            <Setter Property="BackgroundColor" Value="{DynamicResource TransparentBackgroundColour}" />
                            <Setter Property="BorderColor" Value="Transparent" />
                            <Setter TargetName="check"
                                        Property="Opacity"
                                        Value="0" />
                            <Setter TargetName="checkrim"
                                        Property="Opacity"
                                        Value="0" />
                        </VisualState.Setters>
                    </VisualState>
                </VisualStateGroup>
            </VisualStateGroupList>
        </visualstatemanager.VisualStateGroups>
        <Grid Margin="4"
                WidthRequest="100">
            <ContentPresenter Margin="6" />

            <Grid WidthRequest="20"
               HeightRequest="20"
                    Padding="0"
               HorizontalOptions="Start"
               VerticalOptions="Start">

                <Ellipse x:Name="unckecd"
                            stroke="White"
                            strokeThickness="2"
                            Fill="Transparent"
                            Margin="0"
                            WidthRequest="20" HeightRequest="20"
                            HorizontalOptions="Center" VerticalOptions="Center" />

                <Ellipse x:Name="checkrim"
                            stroke="Accent"
                            strokeThickness="2"
                            Fill="Transparent"
                            Margin="0"
                            WidthRequest="20" HeightRequest="20"
                            HorizontalOptions="Center" VerticalOptions="Center" />

                <Ellipse x:Name="check"
                            Fill="Accent"
                            Margin="1,1,0"
                            WidthRequest="10" HeightRequest="10"
                            HorizontalOptions="Center" VerticalOptions="Center" />
            </Grid>
        </Grid>
    </Frame>
</ControlTemplate>

还有我的Style的{​​{1}}

RadioButton

还有我的<Style x:Key="FancyRadioButton" targettype="RadioButton"> <Setter Property="ControlTemplate" Value="{DynamicResource RadioButtonTemplate}" /> </Style>

RadioButton

我只想让 <RadioButton Style="{StaticResource FancyRadioButton}" Value="{x:Static gdl90Types:UdpTransmitMode.UdpUnicast}" WidthRequest="150" HeightRequest="103"> <RadioButton.Content> <StackLayout VerticalOptions="Start" Spacing="0" Margin="0,-3"> <Image Source="{Binding Source=ColourPresetSetting,ConverterParameter=unicast,Converter={StaticResource ResourceImageFromThemeConverter}}" Margin="22,0" Aspect="AspectFit" /> <Label Text="UDP UNICAST" TextColor="{DynamicResource MediumContrastTextColour}" /> </StackLayout> </RadioButton.Content> 中的 Image 注意到属性 RadioButton 发生变化,以更新图像。
它在第一次绘制时执行,但之后不再执行。

我是否遗漏了一些明显的东西?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)