簡體   English   中英

屬性值不會更新Xamarin Forms中UI中Label的值

[英]Property value doesn't update value to Label in UI in Xamarin Forms

我有一個按鈕,並希望在從ViewModel單擊按鈕時更新UI標簽值。 我實現了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;
        }
}

為了引發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();
    }
}  

您可以確保(MyViewModel)this.BindingContext的值不為null嗎?

替換下面的代碼,並檢查是否顯示標簽值

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

  }

還設置公共屬性(票價=“ $ 0.00”),而不是私有財產(_fare =“ $ 0.00”)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM