在Xamarin Forms中,属性值不会更新UI中的Label值

时间:2017-06-14 03:38:31

标签: c# xamarin xamarin.ios xamarin.forms

我有一个按钮,想要在ViewModel上点击按钮时更新UI Label值。我实现了INotifyPropertyChanged,但它无法正常工作。属性值不会在Xamarin Forms

中更新UI中的Label值

MyViewModel

public class MyViewModel : INotifyPropertyChanged
    { 
        public ICommand selectDurationCommand;
        public ICommand SelectDurationCommand
        {
            get { return selectDurationCommand; }
            set
            {
                selectDurationCommand = value;
                OnPropertyChanged();
            }
        }
        public string _fare{ get; set; } 
        public string Fare
        {
            get { return _fare; }
            set
            {
                _fare = value;
                OnPropertyChanged();
            }
        }      
        public event PropertyChangedEventHandler PropertyChanged;
        public MyViewModel()
        {
           selectDurationCommand = new Command((object s) => get_fare(s));
            _fare = "$00.00";
        }
       public void get_fare(object btn)
        {
            var b = (Button)btn;
            _fare="$03.00";
        }
 protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }
}

FareDetails.xaml

<cl:BasePage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="familyinfo.FareDetails" xmlns:cl="clr-namespace:familyinfo;assembly=familyinfo" x:Name="PDuration">
<Label FontSize="22" TextColor="Black" VerticalOptions="FillAndExpand" Font="Roboto-Medium" Text="{Binding Fare}" VerticalTextAlignment="Center" HorizontalTextAlignment="Center" />
<Button Command="{Binding Source={x:Reference PDuration}, Path=BindingContext.SelectDurationCommand}" CommandParameter="{x:Reference min15Button}" x:Name="min15Button" HeightRequest="30" HorizontalOptions="FillAndExpand" BorderRadius="8" Text="15 MIN" TextColor="#ffffff" BackgroundColor="#f2415c" />
</cl:BasePage>

FareDetails.xaml.cs

public partial class FareDetails : BasePage
    {
        MyViewModel _MyViewModel { get; set; }
        public FareDetails()
        {
            InitializeComponent();
            _MyViewModel = (MyViewModel)this.BindingContext;
        }
}

2 个答案:

答案 0 :(得分:2)

要引发PropertyChanged通知,您需要将属性值设置为私有字段。

将Command方法更改为:

public void get_fare(object btn)
{
     var b = (Button)btn;
     Fare="$03.00";
}

注意:您的_fare可以安全地成为私人字段。

private string _fare;
public string Fare
{
    get { return _fare; }
    set
    {
        _fare = value;
        OnPropertyChanged();
    }
}  

答案 1 :(得分:0)

你能确保(MyViewModel)this.BindingContext的值不为空吗?

替换以下代码并检查标签值是否显示

MyViewModel _MyViewModel { get; set; }
 public FareDetails()
 {
    _MyViewModel = new MyViewModel();
    BindingContext = _MyViewModel;
    InitializeComponent();

  }

还设置公共属性(Fare =“$ 0.00”)而不是私有属性(_fare =“$ 0.00”)