单选按钮未由对象中的数据填充

Radio buttons are not populated by data in Object

这里完全是新手一问

我正在使用一些数据创建应用程序。在 FirstPage 上有 Picker 和一些用户可以选择的数据(对象)。然后将选定的数据(对象)传递给 SecondPage,并且此页面会填充来自此对象的数据。虽然 entry/text 已正确填充,但单选按钮未正确填充。玩了几个小时(创建简单的测试项目并创建各种场景)后,我注意到以下内容:

问题是当我从 SQL 数据库填充数据时。如果我创建 ObservableCollection 并使用代码添加数据,则此方法有效,但如果我从 SQL 填充数据,则无效。请注意,我没有更改 SecondPage 上的任何内容(显示数据的页面),只是如何将对象添加到 FirstPage 上的 ObservableCollectin

绑定也有效,因为如果我更改选择并保存,数据将保存到对象。如果我再次加载对象,单选按钮不会显示已保存的选项。

有人对此有任何解决方案吗?或者至少知道为什么在从 SQL?

添加对象时没有填充单选按钮

我的代码(我使用 FreshMVVM 和 rg.plugins.popup)

第一页

页面模型

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using FreshMvvm;
using RadioButtonTest.Model;
using RadioButtonTest.Services;
using Xamarin.Forms;
using FreshMvvm.Popups;

namespace RadioButtonTest
{
    class FirstPageModel : FreshBasePageModel
    {
        public async override void Init(object initData)
        {
            DatabaseConnection database = await DatabaseConnection.Instance;
            var c = await database.GetAllCategories();

            Categories = new ObservableCollection<Category>(c); //If objects are
            added like this radio buttons are not populated

            // If objects are added like below radio buttons are populated
            Categories.Add(new Category() { CategoryID = 5, CategoryName = "Name
            one", FlowType = "1" });
            Categories.Add(new Category() { CategoryID = 1, CategoryName = "Name
            two", FlowType = "2" });
            Categories.Add(new Category() { CategoryID = 2, CategoryName = "Name
            three", FlowType = "1" });
            Categories.Add(new Category() { CategoryID = 3, CategoryName = "Name
            four", FlowType = "0" });
            Categories.Add(new Category() { CategoryID = 4, CategoryName = "Name
            five", FlowType = "2" });
        }

        public Command GoToTestPage
        {
            get => new Command(async () =>
            await CoreMethods.PushPopupPageModel<SecondPageModel>(ChosenObject));
        }

        private ObservableCollection<Category> categories;

        public ObservableCollection<Category> Categories
        {
            get => categories;
            set
            {
                categories = value;
                RaisePropertyChanged();
            }
        }

        private Category chosenObject;

        public Category ChosenObject
        {
            get => chosenObject;
            set
            {
                chosenObject = value;
                RaisePropertyChanged();
            }
        }
    }
}

<?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="RadioButtonTest.FirstPage">
    <ContentPage.Content>
        <StackLayout>
            <Picker ItemsSource="{Binding Categories}"
                SelectedItem="{Binding ChosenObject}"
                ItemDisplayBinding="{Binding CategoryName}"/>
            <Button Text="Go to test Page"
                Command="{Binding GoToTestPage}"/>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

第二页

页面模型

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using FreshMvvm;
using Xamarin.Forms;
using FreshMvvm.Popups;
using RadioButtonTest.Model;

namespace RadioButtonTest
{
    class SecondPageModel : FreshBasePageModel
    {
        public async override void Init(object initData)
        {
            Category mT = (Category)initData;
            ChosenObject = mT;
        }

        private Category chosenObject;

        public Category ChosenObject
        {
            get => chosenObject;
            set
            {
                chosenObject = value;
                RaisePropertyChanged();
            }
        }

        public Command CancelCategory => new Command(async () =>
        {
            await CoreMethods.PopPopupPageModel();
        });
    }
}

<?xml version="1.0" encoding="utf-8" ?>
<pages:PopupPage xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="RadioButtonTest.SecondPage"
    xmlns:pages="clr-namespace:Rg.Plugins.Popup.Pages;assembly=Rg.Plugins.Popup"
    xmlns:inputLayout="clr-namespace:Syncfusion.XForms.TextInputLayout;assembly=Syncfusion.Core.XForms"
    CloseWhenBackgroundIsClicked="False"
    Padding="20">
    <StackLayout BackgroundColor="White"
        HorizontalOptions="Center"
        VerticalOptions="Center"
        Padding="20">
        <Label TextColor="Black"
            HorizontalOptions="Center"
            VerticalTextAlignment="Center"
            Text="{Binding CategoryPopupText}" />
        <inputLayout:SfTextInputLayout Hint="Category" >
            <Entry Text="{Binding CategorySelected.CategoryName}"/>
        </inputLayout:SfTextInputLayout>
        <StackLayout Orientation="Horizontal"
            RadioButtonGroup.GroupName="TEST"
            RadioButtonGroup.SelectedValue="{Binding ChosenObject.FlowType,
            Mode=TwoWay}">
            <RadioButton Content="Op 1"
                Value="0" />
            <RadioButton Content="Op 2"
                Value="1" />
            <RadioButton Content="Op 3"
                Value="2"/>
        </StackLayout>
        <StackLayout Orientation="Horizontal">
            <Button Text="Save" Command="{Binding SaveCategory}"/>
            <Button Text="Cancel" Command="{Binding CancelCategory}"/>
            <Button Text="Delete"
                Command="{Binding DeleteCategorytWarning}"
                HorizontalOptions="EndAndExpand"
                TextColor="Red"
                IsVisible="{Binding IsDeleteCategoryVisible}"/>
        </StackLayout>
    </StackLayout>
</pages:PopupPage>
Radio buttons are not populated by data in Object

如果要在SecondPageFrom中正确填充三个RadioButton,可以绑定三个字段(RB_firstRB_secondRB_third ) 在 class SecondPageModel 到三个 RadioButtons.

请参考以下代码:

    <StackLayout Orientation="Horizontal"
                             RadioButtonGroup.GroupName="TEST"
                             RadioButtonGroup.SelectedValue="{Binding ChosenObject.FlowType, Mode=TwoWay}"
                           >
        <RadioButton Content="Op 1" IsChecked="{Binding RB_first}"
                                     Value="0" />
        <RadioButton Content="Op 2" IsChecked="{Binding RB_second}"
                                     Value="1"
                                     />
        <RadioButton Content="Op 3" IsChecked="{Binding RB_third}"
                                     Value="2"/>
    </StackLayout>

另外,请不要在SecondPageModel

中注释掉方法 Workaround();
       void Workaround()
    {
        switch (ChosenObject.FlowType)
        {
            case "0":
                RB_first = true;
                break;
            case "1":
                RB_second = true;
                break;
            case "2":
                RB_third = true;
                break;
            default:
                break;
        }
    }