【发布时间】: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