我有一个 Windows Phone 8.1 应用程序,其中有一个导航到 PageTwo.xaml
的按钮。在那个 PageTwo.xaml.cs
中,在后面的代码中我有这样的东西:
string type = "";
protected override void OnNavigatedTo(NavigationEventArgs e) {
type = e.Parameter.ToString();
}
private void Page_Loaded(object sender, RoutedEventArgs e) {
PageTwoListViewModel pageTwoListViewModel = ViewModelLocator.PageTwoListStatic;
this.DataContext = pageTwoListViewModel;
}
我在 Page_Loaded 事件中设置 DataContext
的原因是因为该项目是一个 ClassLibrary 而我没有 App.xaml
文件,但那不应该影响与我的问题相关的任何事情。
然后在我的 PageTwoViewModel
中有以下内容:
public RelayCommand PageLoadedCommand { get; private set; }
public PageTwoListViewModel() {
this.PageLoadedCommand = new RelayCommand(PageLoaded);
}
private void PageLoaded() {
LoadList();
}
private async void LoadList() {
ObservableCollection<MyListModel> _list = await DatabaseService.GetList();
MyViewList = _list;
}
负责触发 PageLoadedCommand 的代码是:
<Page (...)>
<i:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Loaded">
<core:InvokeCommandAction Command="{Binding PageLoadedCommand}">
</core:InvokeCommandAction>
</core:EventTriggerBehavior>
</i:Interaction.Behaviors>
</Page>
这里的问题是 OnNavigatedTo
和 Page_Loaded
事件在页面可见之前运行,所以如果我有一个大列表要填写,只有在一切都完成之后是否转到 PageTwo.xaml
,卡住应用程序。
我想要做的是导航到 PageTwo.xaml
并在那里启动加载动画并异步填充我的 ListView。我该怎么做?
最佳答案
事实证明,我的问题来自一个相当愚蠢的事情。我的 DatabaseService.GetList();
不是完全异步的,这就是导致问题的原因,所以只要您保证数据库调用是异步的,上述实现就可以正常工作。
关于c# - 在导航到 Page Windows Phone 8.1 后将项目异步加载到 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26549950/