我在Models文件夹中有一个Booking类。导航到该类时,我已经通过了Booking实例,现在如何从视图中访问Booking的属性?
我的ViewModel:
public class BookingEditViewModel : ViewModelBase
{
public Booking Booking { get;set; }
public override void OnNavigatedTo(INavigationParameters parameters)
{
Booking = parameters.GetValue<Booking>("booking");
}
}
我的观点:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
xmlns:booking="clr-namespace:HelloWorldPrism.Models"
prism:ViewModelLocator.AutowireViewModel="True"
x:Class="HelloWorldPrism.Views.BookingEdit">
<StackLayout>
<Label Text="{Binding Booking.Id}"/>
<Label Text="{Binding Booking.CustomerId}"/>
</StackLayout>
</ContentPage>
我的模特:
public class Booking
{
public int Id { get; set; }
public int CustomerId { get; set; }
}
我已将Models名称空间添加为xmlns的一部分,接下来该怎么做才能访问Booking的属性?
编辑: 如果我只向类的构造函数中的Booking属性添加任何值,则Label将打印出这些值。
public BookingEditViewModel(BookingService bookingService, INavigationService navigationService) : base(navigationService)
{
this.bookingService = bookingService;
Booking = new Booking { Id = 999, CustomerId = 2222, };
}
所以问题似乎在于,当在OnNavigatedTo中更新Booking时,更新不会传播回UI。我已经将BindingMode更改为TwoWay,仍然无法正常工作。我该如何解决?
答案 0 :(得分:0)
好的,找到答案了。基本上,我的问题与以下问题相同:Prism Xamarin Forms View is not updated after changing the model in OnNavigatedTo
我只需要将属性更改为此:
public Booking Booking
{
get => booking;
set => SetProperty(ref booking, value);
}