单选按钮不是由 Object 中的数据填充的
Posted
技术标签:
【中文标题】单选按钮不是由 Object 中的数据填充的【英文标题】:Radio buttons are not populated by data in Object 【发布时间】:2021-12-29 02:10:51 【问题描述】:在这里完成一个问题。
我正在使用一些数据创建应用程序。在FirstPage
上有Picker
,其中包含一些用户可以选择的数据(对象)。然后将选定的数据(对象)传递给SecondPage
,该页面将填充来自该对象的数据。虽然条目/文本已正确填充,但单选按钮却没有。在玩了几个小时(创建简单的测试项目并创建各种场景)后,我注意到以下几点:
问题是当我从 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>
【问题讨论】:
请正确格式化您的代码,使其可读 谢谢。下次注意。Issue is when I am populating data from SQL database.
在导航到第二页之前,请重新检查您是否可以正确地从您的 sql 数据库中获取数据。
@Jessie Zhang -MSFT 我不知道如何检查我从我的 SQL 数据库中正确获取数据。我查看并看到,在导航到第二页之前,所有数据都在对象中 - 正如您从 image 中看到的那样
如果方便的话,可以发个基本的demo到github或者onedriver,方便我们测试一下吗?
【参考方案1】:
Radio buttons are not populated by data in Object
如果要在SecondPageFrom
中正确填充三个RadioButton
,可以将SecondPageModel
类中的三个字段(RB_first
、RB_second
和RB_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>
另外,请不要在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;
【讨论】:
这行得通。我认为这不是必需的,RadioButtons 将从 Object 填充。看起来他们需要单独的方法来设置它们。以上是关于单选按钮不是由 Object 中的数据填充的的主要内容,如果未能解决你的问题,请参考以下文章