将IsChecked属性与xamarin MVVM中的单选按钮组绑定



我正在寻找一种通过单选按钮控制标签文本的方法。我已经阅读过类似的问题在这个网站以及其他没有能够找到一个工作的解决方案。我尝试过使用转换器以及将单选按钮放入列表中。当我通过调试器运行代码时,视图模型中单选按钮的代码永远不会到达。任何帮助都将不胜感激。下面是my View的代码:

<?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="sampleRadioButtons.Views.ButtonPage"
xmlns:vm="clr-namespace:sampleRadioButtons.ViewModels">


<ContentPage.BindingContext>
<vm:MainPageViewModel/>
</ContentPage.BindingContext>

<ContentPage.Content>
<StackLayout VerticalOptions="Center">
<StackLayout Orientation="Horizontal" RadioButtonGroup.GroupName="time">
<RadioButton Content="One Year" IsChecked="{Binding YearChecked, Mode=TwoWay}"/>
<RadioButton Content="One Month" IsChecked="{Binding MonthChecked, Mode=TwoWay}"/>
<RadioButton Content="One Week" IsChecked="{Binding WeekChecked, Mode=TwoWay}"/>
</StackLayout>
<Label Text="Total is"/>
<Label Text="{Binding Total}"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>

这是我相应的视图模型:

using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace sampleRadioButtons.ViewModels
{
public class MainPageViewModel : INotifyPropertyChanged
{
public MainPageViewModel()
{
total = GetTotal();
}
public void OnPropertyChange([CallerMemberName] string name = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}

#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
var changed = PropertyChanged;
if (changed == null)
return;

changed.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
private string total;
public string Total
{
get { return total; }
set
{
total = value;
OnPropertyChanged();
}
}

private bool yearChecked;
public bool YearChecked
{
get { return yearChecked; }
set
{
yearChecked = value;
OnPropertyChange();
}
}
private bool monthChecked;
public bool MonthChecked
{
get { return monthChecked; }
set
{
monthChecked = value;
OnPropertyChange();
}
}
private bool weekChecked;
public bool WeekChecked
{
get { return weekChecked; }
set
{
weekChecked = value;
OnPropertyChange();
}
}
public string GetTotal()
{
if(YearChecked == true) { return "$10"; }
if(monthChecked == true) { return "$20"; }
if(weekChecked == true) { return "30"; }
else { return "$50"; }
}
}
}

在您的构造函数中,您正在设置backbackfield而不是属性,将total替换为Total

public MainPageViewModel()
{
Total = GetTotal();
}

你也应该调用GetTotal()不仅在构造函数中,而且当三个属性中的一个发生变化时:

private bool yearChecked;
public bool YearChecked
{
get { return yearChecked; }
set
{
yearChecked = value;
OnPropertyChange();
Total = GetTotal();
}
}
private bool monthChecked;
public bool MonthChecked
{
get { return monthChecked; }
set
{
monthChecked = value;
OnPropertyChange();
Total = GetTotal();
}
}
private bool weekChecked;
public bool WeekChecked
{
get { return weekChecked; }
set
{
weekChecked = value;
OnPropertyChange();
Total = GetTotal();
}
}
  • 同样在你的GetTotal()中,你应该在你的if条件下使用属性,而不是他们的后台字段:MonthChecked而不是monthCheckedWeekChecked而不是weekChecked

https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/auto-implemented-properties

最新更新