我有一个异步休息服务,当通过OnAppearing事件加载页面时会创建List 。protected async override void OnAppearing(){ base.OnAppearing(); RestService restService = new RestService(); List<Example> exampleList = await restService.GetExample();}使用MVVM模式准备好异步操作后,用exampleList填充XAML ListView的最佳实践是什么? 最佳答案 我亲自提供了一个基本的ContentPage实现,并在其中指向事件在 View 模型中实现的接口(interface)。例如。public interface IPageAppearingEvent{ void OnAppearing();}public class BasePage : ContentPage{ protected override void OnBindingContextChanged () { base.OnBindingContextChanged (); var onAppearingLifeCycleEvents = BindingContext as IPageAppearingEvent; if (onAppearingLifeCycleEvents != null) { var lifecycleHandler = onAppearingLifeCycleEvents; base.Appearing += (object sender, EventArgs e) => lifecycleHandler.OnAppearing (); } }}public class ViewModel : IPageAppearingEvent{ public void OnAppearing() { //Do whatever you like in here }}只要确保您的 View 是BasePage的子类,您就可以轻松进行。关于listview - Xamarin.Forms MVVM实现可通过OnApperaing事件填充XAML ListView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38879321/
10-11 03:43