使用xaml在windws 8应用程序中从页面导航到另一个时添加进度条
我正在开发一个Windows 8应用程序,我想在从一个导航到另一个页面时向我的页面添加一个进度条,因为用户必须等待才能从服务器加载数据。 任何人都可以向我提供我需要在XAML和C#中添加的代码吗? 应该使用进度条的哪些属性或事件?
// XAML PAGE
<!-- --> //C# Page using App1.Data; using App1.DataService; using System; using System.Collections.Generic; using System.IO; using System.Linq; using Windows.Foundation; using Windows.Foundation.Collections; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls.Primitives; using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Input; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Navigation; // The Grouped Items Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234231 namespace App1 { /// /// A page that displays a grouped collection of items. /// public sealed partial class GroupedItemsPage1 : App1.Common.LayoutAwarePage { public GroupedItemsPage1() { this.InitializeComponent(); } /// /// Populates the page with content passed during navigation. Any saved state is also /// provided when recreating a page from a prior session. /// /// The parameter value passed to /// when this page was initially requested. /// /// A dictionary of state preserved by this page during an earlier /// session. This will be null the first time a page is visited. protected override void LoadState(Object navigationParameter, Dictionary pageState) { // TODO: Create an appropriate data model for your problem domain to replace the sample data var sampleDataGroups = DataSource.GetGroups((String)navigationParameter); this.DefaultViewModel["Groups"] = sampleDataGroups; var tracks = groupedItemsViewSource.View.CollectionGroups; (Zoom.ZoomedOutView as GridView).ItemsSource = tracks; // GetSectors(); } /// /// Invoked when an item within a group is clicked. /// /// The GridView (or ListView when the application is snapped) /// displaying the item clicked. /// Event data that describes the item clicked. /*protected async override void LoadState(Object navigationParameter, Dictionary pageState) { var client = new DataServiceSoapClient(); var sectors = await client.GetSectorsAsync(1); this.DefaultViewModel["sectors"] = sectors; }*/ void ItemView_ItemClick(object sender, ItemClickEventArgs e) { // Navigate to the appropriate destination page, configuring the new page // by passing required information as a navigation parameter var itemId = ((DataItem)e.ClickedItem).UniqueId; this.Frame.Navigate(typeof(GroupDetailPage1), itemId); } private void Content_Hub_Click(object sender, RoutedEventArgs e) { Frame.Navigate(typeof(App1.GroupedItemsPage1)); } private void MARKET_RESEARCH_Click(object sender, RoutedEventArgs e) { Frame.Navigate(typeof(App1.SamplePage)); } } }
在LoadState中加载数据异步。 在获取数据显示内容网格并隐藏进度条后,将ProgressBar(或ProgressRing)可见性设置为Visible并隐藏主内容网格。
protected async override void LoadState(Object navigationParameter, Dictionary pageState) { ProggressBarVisible(true); try { MainContentGrid.Visibility = Visibility.Collapsed; this.DataContext = await MyDataSource.GetData(navigationParameter); } finally { ProggressBarVisible(false); MainContentGrid.Visibility = Visibility.Visible; } } private void ProggressBarVisible(bool visible) { ProgressRingLoad.Visibility = visible ? Visibility.Visible : Visibility.Collapsed; ProgressRingLoad.IsActive = visible; }
空页的XAML看起来应该是这样的:
顺便说一句。 要获取LoadState,您应该使用LayoutAwarePage
如果要加载数据async
,请将其添加到您的函数中
private async void LoadDataAsync() { //progressbar start progressBar.IsIndeterminate = true; //Some async calls //other stuff //stop progressbar progressBar.IsIndeterminate = false; }
您的问题是您在页面构造函数,OnNavigatedTo事件或Loaded事件中获取数据。 显示页面的所有块。 所以你必须找到另一种解决方案。 我在那里看到两个:
1.稍后加载您的数据。 推迟加载,您可以显示具有进度或类似的页面
并在您的OnNavigatedTo-handler中
PrgRing.Visibility = Visibility.Visible;
然后加载您的数据,当您完成后,显示并设置
PrgRing.Visibility = Visibility.Collapsed;
2.在中间使用一个页面,它会加载您的数据并显示进度。
XAML:
C#
//load the data and then this.rootFrame.Navigate(typeof(TargetPage));