表单MVVM实现,通过OnAppraing事件填充XAML ListView
我有一个异步rest服务,当通过OnAppearing事件加载页面时,它会创建一个列表表单MVVM实现,通过OnAppraing事件填充XAML ListView,listview,mvvm,xamarin.forms,Listview,Mvvm,Xamarin.forms,我有一个异步rest服务,当通过OnAppearing事件加载页面时,它会创建一个列表 protected async override void OnAppearing() { base.OnAppearing(); RestService restService = new RestService(); List<Example> exampleList = await restService.GetExample(); } protected
protected async override void OnAppearing()
{
base.OnAppearing();
RestService restService = new RestService();
List<Example> exampleList = await restService.GetExample();
}
protected async override void OnAppearing()
{
base.OnAppearing();
RestService RestService=新的RestService();
List exampleList=等待restService.GetExample();
}
当异步操作准备好使用MVVM模式时,用exampleList填充XAML ListView的最佳实践是什么?我个人提供了一个基本ContentPage实现,并在视图模型中实现了一个接口。例如
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
}
}
只要您确保您的视图是BasePage的子类,那么您就可以开始了。我个人提供了一个基本ContentPage实现,并且在视图模型中实现了一个接口。例如
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
}
}
只要您确保您的视图是BasePage的子类,就可以继续了