【问题标题】:Xamarin -- Radiobutton visual state doesn't update after setting the value in a viewmodel when using Syncfusion's SfPopupLayoutXamarin - 使用 Syncfusion 的 SfPopupLayout 时在视图模型中设置值后,单选按钮的视觉状态不会更新
【发布时间】:2022-01-22 22:26:19
【问题描述】:

在使用数据绑定时,我遇到了单选按钮不会在视觉上更新但值正确的问题。我有一个 RadiobuttonGroup.GroupName 和一个 RadioButtonGroup.SelectedValue。 SelectedValue 通过 {Binding Selection} 将数据绑定到我的 ViewModel。选择也在我的 ViewModel 中声明。

每当我将 RadioButton 的选择更改为未选择的按钮时,OnPropertyChanged();三次熄灭。 (我想这是由于视图中有三个按钮,这里可能是错误的。)导致值被选中并被移交给我的数据绑定选择。但是按钮的视觉状态不会改变。单选按钮位于 SfPopupLayout 弹出窗口中。弹出窗口第一次初始化并在视图中提供时,它总是按预期工作。但是随着随后的每一次服务,它在视觉上都会出错。导致必须多次单击单选按钮才能更改视觉状态。

实际上并没有发生太多事情,只是选择存储在我的 ViewModel 中。我已经检查了 GitHub 上有关 RadioButtons 和数据绑定的 Xamarin-Examples-Demos,但我无法重现我在演示中遇到的相同问题。

XAML 代码 sn-p;

<StackLayout HeightRequest="160"
             Grid.Row="2"
             RadioButtonGroup.GroupName="WeekSelection"
             RadioButtonGroup.SelectedValue="{Binding Selection}">
                <RadioButton Padding="5"
                             BackgroundColor="{DynamicResource BlockBackgroundColor}"       
                             Content="{markup:Translate Week_Selection}"
                             Value="{markup:Translate Week_Selection}"/>
                <BoxView Style="{StaticResource SeperatorLineStyle}"/>
                <RadioButton Padding="5"
                             BackgroundColor="{DynamicResource BlockBackgroundColor}"
                             Content="{markup:Translate TwoWeek_Selection}"
                             Value="{markup:Translate TwoWeek_Selection}"/>
                <BoxView Style="{StaticResource SeperatorLineStyle}"/>
                <RadioButton Padding="5"
                             BackgroundColor="{DynamicResource BlockBackgroundColor}"
                             Content="{markup:Translate Month_Selection}"
                             Value="{markup:Translate Month_Selection}"/>
                <BoxView Style="{StaticResource SeperatorLineStyle}"/>
</StackLayout>

更新:似乎它与切换视图有关。每当我转到我的设置页面来更改单选按钮的选择时,OnPropertyChanged();只被解雇一次。但是每当我关闭视图并返回它时,它就会将其关闭两次。随后随着每次切换,它会增加 OnPropertyChanged(); 的次数。叫做。值仍然正常工作,只是视觉状态没有更新。

更新 2:我很确定它与生成的包含单选按钮的弹出窗口有关。这是初始化带有单选按钮的弹出窗口的代码;

        public void ShowAmountOfWeeksPopup()
        {
            _selectWeeksToViewPopupControl = new SelectWeeksToViewPopupControl(this);
            
            _selectWeeksToViewPopupControl.Show();
        }

        public void DismissAmountOfWeeksPopup()
        { 
            _selectWeeksToViewPopupControl.Dismiss();
        }

【问题讨论】:

    标签: c# xaml xamarin mvvm viewmodel


    【解决方案1】:

    Xamarin -- 单选按钮的视觉状态在设置后不更新 视图模型中的值。

    我看不到您应用的其他代码。于是我根据示例代码GroupedRadioButtonsViewModelPage.xaml做了一个测试,但是无法重现这个问题。

    可以参考以下代码:

    GroupedRadioButtonsViewModelPage.xaml

    <?xml version="1.0" encoding="UTF-8"?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 x:Class="RadioButtonDemos.GroupedRadioButtonsViewModelPage"
                 Title="Grouped RadioButtons ViewModel demo">
        <StackLayout Margin="10"
                     RadioButtonGroup.GroupName="{Binding GroupName}"
                     RadioButtonGroup.SelectedValue="{Binding Selection}">
            <Label Text="What's your favorite animal?" />
            <RadioButton Content="Cat"
                         Value="Cat" />
            <RadioButton Content="Dog"
                         Value="Dog" />
            <RadioButton Content="Elephant"
                         Value="Elephant" />
            <RadioButton Content="Monkey"
                         Value="Monkey"/>
            <Label x:Name="animalLabel">
                <Label.FormattedText>
                    <FormattedString>
                        <Span Text="You have chosen:" />
                        <Span Text="{Binding Selection}" />
                    </FormattedString>
                </Label.FormattedText>
            </Label>
    
    
            <Button  Text="test"  Clicked="Button_Clicked"/>
        </StackLayout>
    </ContentPage>
    

    GroupedRadioButtonsViewModelPage.xaml.cs

    public partial class GroupedRadioButtonsViewModelPage : ContentPage
    {
    
        AnimalViewModel model;
        public GroupedRadioButtonsViewModelPage()
        {
            InitializeComponent();
    
            model = new AnimalViewModel();
            BindingContext = model;
    
            //BindingContext = new AnimalViewModel
            //{
            //    GroupName = "animals",
            //    Selection = "Monkey"
            //};
        }
    
        private void Button_Clicked(object sender, System.EventArgs e)
        {
            model.Selection = "Dog";
        }
    }
    

    AnimalViewModel.cs

    public class AnimalViewModel : INotifyPropertyChanged
    {
        string groupName;
        object selection;
    
    
        public AnimalViewModel() {
    
            GroupName = "animals";
            Selection = "Elephant";
        }
    
        public string GroupName
        {
            get => groupName;
            set
            {
                groupName = value;
                OnPropertyChanged(nameof(GroupName));
            }
        }
    
        public object Selection
        {
            get => selection;
            set
            {
                selection = value;
                OnPropertyChanged(nameof(Selection));
            }
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    

    注意:

    我添加了一个按钮,当点击按钮时,我们可以改变ViewModel(AnimalViewModel)的Selection的值,然后UI会自动刷新。

    【讨论】:

    • 嗨,杰西,感谢您的回复!似乎这与每次我创建一个新页面时,GC 都会发生一些事情而不是处理旧页面有关。导致 OnPropertyChanged() 多次触发。在我看来,我的代码有问题。不过感谢您的回复!
    • 您的问题解决了吗?
    • 很遗憾,没有。这似乎与我们正在使用的插件有关。每次按下按钮以显示设置弹出窗口时,SyncFusion 的 SfPopupLayout 在创建新弹出窗口时似乎无法正确处理弹出窗口,从而导致视觉错误。所以这可能是一个非常小众的问题。我已对问题添加了更新,并将问题更新为更具体。
    • 如果方便的话,可以发个基本的demo到github或者onedriver,方便我们测试一下吗?
    • 我花了一点时间才让插件正常工作,但还是这样。我能够重现完全相同的问题。只需单击显示弹出按钮,选择三个单选按钮之一。关闭弹出窗口并重新打开它。在 OnPropertyChanged() 上设置断点;你会注意到它会触发多次。视觉状态也不会改变。 github.com/TwisterTies/sfpopupradiobuttons
    猜你喜欢
    • 1970-01-01
    • 2018-05-12
    • 1970-01-01
    • 1970-01-01
    • 2016-03-31
    • 1970-01-01
    • 1970-01-01
    • 2022-10-22
    • 1970-01-01
    相关资源
    最近更新 更多