【问题标题】:Property value doesn't update value to Label in UI in Xamarin Forms属性值不会在 Xamarin 表单的 UI 中将值更新为标签
【发布时间】:2017-06-14 03:38:30
【问题描述】:

我有一个按钮,想在 ViewModel 中单击按钮时更新 UI 标签值。我实现了 INotifyPropertyChanged 但它不起作用。属性值不会在 Xamarin 表单的 UI 中将值更新为标签

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;
        }
}

【问题讨论】:

  • 我看不到您在哪里设置页面的 BindingContext..
  • 我正在使用来自另一个页面的新绑定上下文导航到此页面
  • 当你点击按钮时你的命令正在触发?
  • @apineda 是的...

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


【解决方案1】:

要引发 PropertyChanged 通知,您需要将值设置为 Property 而不是私有字段。

将您的命令方法更改为:

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();
    }
}  

【讨论】:

    【解决方案2】:

    你能保证(MyViewModel)this.BindingContext的值不为null吗?

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

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

    同时设置公共属性 (Fare = "$0.00") 而不是私有属性 (_fare = "$0.00")

    【讨论】:

      猜你喜欢
      • 2015-04-23
      • 2021-06-09
      • 2017-06-05
      • 1970-01-01
      • 1970-01-01
      • 2015-06-02
      • 1970-01-01
      • 2016-11-03
      • 1970-01-01
      相关资源
      最近更新 更多