在使用資料系結時,我遇到了一個問題,即單選按鈕不會在視覺上更新,但值是正確的。我有一個 RadiobuttonGroup.GroupName 和一個 RadioButtonGroup.SelectedValue。SelectedValue 是資料系結到我的帶有 {Binding Selection} 的 ViewModel。選擇也在我的 ViewModel 中宣告。
每當我將 RadioButton 的選擇更改為未選中的按鈕時, OnPropertyChanged(); 熄滅三次。(我想這是因為視圖中有三個按鈕,這里可能是錯誤的。)導致值被選擇并被移交給我的資料系結選擇。但是按鈕的視覺狀態不會改變。單選按鈕位于 SfPopupLayout 彈出視窗中。它總是在第一次初始化彈出視窗并在視圖中提供時按預期作業。但是在隨后的每一次服務中,它都會在視覺上出現問題。導致必須多次單擊單選按鈕才能更改視覺狀態。
真的沒有太多事情發生,只是 Selection 存盤在我的 ViewModel 中。我已經檢查了 GitHub 上關于 RadioButtons 和資料系結的 Xamarin-Examples-Demos,但我無法重現我在演示中遇到的相同問題。
XAML 代碼片段;
<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();
}
最終更新:
根據 SyncFusion,這是一個錯誤,最終在他們的支持論壇上創建了一個論壇帖子,他們最終為我提供了修復程式。如果有人遇到同樣的問題,請參考以下鏈接;https://www.syncfusion.com/forums/171545/radiobuttons-not-working-properly-when-closure-and-re-opening-a-popup
uj5u.com熱心網友回復:
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));
}
}
筆記:
我添加了一個按鈕,當點擊按鈕時,我們可以改變SelectionViewModel( AnimalViewModel)的值,然后UI會自動重繪 。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/402159.html
