6.4-表单类控件难点:RadioButton、ProgressBar
一、RadioButton,单选框分组和选定值
<ContentPage
......
xmlns:vm="clr-namespace:MauiApp15.ViewModels">
<ContentPage.BindingContext>
<vm:MainPageViewModel/>
</ContentPage.BindingContext>
<StackLayout RadioButtonGroup.GroupName="{Binding GroupName}" RadioButtonGroup.SelectedValue="{Binding Selection}">
<Label Text="你最喜欢的动物是?" />
<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="你已选定:" />
<Span Text="{Binding Selection}" />
</FormattedString>
</Label.FormattedText>
</Label>
</StackLayout>
</ContentPage>
public partial class MainPageViewModel: ObservableObject
{
[ObservableProperty]
private string groupName = "animals";
[ObservableProperty]
private string selection = string.Empty;
}
二、ProgressBar,结合动画展示进度条
<ContentPage
......>
<StackLayout>
<ProgressBar x:Name="progressBar" ProgressColor="Orange" />
<Button Clicked="Button_Clicked" Text="点击" />
</StackLayout>
</ContentPage>
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private async void Button_Clicked(object sender, EventArgs e)
{
//将progressBar的进度百分比(Progress),以线性动画的方式,在5000毫秒以内,调整到1.0(100%)
await progressBar.ProgressTo(1.0, 5000, Easing.Linear);
//重新将进度百分比设置为0
progressBar.Progress = 0.0;
}
}