簡體   English   中英

Object 中的數據未填充單選按鈕

[英]Radio buttons are not populated by data in Object

在這里完成一個問題的新手。

我正在使用一些數據創建應用程序。 FirstPage上有Picker ,其中包含用戶可以選擇的一些數據(對象)。 然后將選定的數據(對象)傳遞給SecondPage ,此頁面將填充來自此 object 的數據。 雖然條目/文本已正確填充,但單選按鈕卻沒有。 玩了幾個小時(創建簡單的測試項目並創建各種場景)后,我注意到以下幾點:

問題是當我從 SQL 數據庫中填充數據時。 如果我創建ObservableCollection並使用代碼添加數據,那么這是可行的,但如果我從 SQL 填充數據,那么它不會。 請注意,我沒有更改SecondPage上的任何內容(顯示數據的頁面),只是將對象添加到FirstPage上的ObservableCollectin的方式。

綁定也有效,因為如果我更改選擇並保存,數據將保存到 Object。 如果我再次加載 object 單選按鈕不顯示選項已保存。

有沒有人對此有任何解決方案? 或者至少知道為什么從 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 ,可以將 class SecondPageModel中的三個字段( RB_firstRB_secondRB_third )綁定到三個RadioButton

請參考以下代碼:

    <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>

另外,請不要注釋掉方法Workaround(); SecondPageModel

       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;
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM