如何结合异步调用编程我的进度条?

首先,我对Windows Phone 8开发的经验不是很大,但有些东西看起来像我更熟悉的ASP.NET Framework。

我想要一个不确定的进度条,它在后台执行Web请求时显示,并在处理请求时隐藏。

我的解决方案有效,但我对它不满意
(并且progressBar / Text位于一个pivot元素内以测试function)
我们有以下内容:

一个名为“MainPage”的XAML页面,带有Pivot元素。

 ...               

我的代码隐藏看起来像这样:

 protected override void OnNavigatedTo( App.ViewModel.LoadSomething(); } 

Function LoadSomething()显示/隐藏进度条和加载文本。
这是我不满意的部分:

 // Method of the ViewModel public void LoadSomething() { //Showing progress bar and loading-text var mainPage = (MainPage)App.RootFrame.Content; mainPage.ProgressBar.Visibility = Visibility.Visible; mainPage.ProgressText.Visibility = Visibility.Visible; // form the URI UriBuilder fullUri = new UriBuilder(string.Format("http://somepage...")); // initialize a new WebRequest HttpWebRequest request = (HttpWebRequest)WebRequest.Create(fullUri.Uri); // set up the state object for the async request UpdateState state = new UpdateState(); state.AsyncRequest = request; // start the asynchronous request request.BeginGetResponse( new AsyncCallback(HandleResponse), state); } private void HandleResponse(IAsyncResult asyncResult) { // Here happens logic and stuff Deployment.Current.Dispatcher.BeginInvoke(() => { // Here happens logic and stuff //Hiding progress bar and loading-text var mainPage = (MainPage)App.RootFrame.Content; mainPage.ProgressBar.Visibility = Visibility.Collapsed; mainPage.ProgressText.Visibility = Visibility.Collapsed; }); } 

所以现在我的问题:

  1. 是否可以在我导航到的任何枢轴元素上显示进度条和加载文本?

  2. 如您所见,通过引用“(MainPage)App.RootFrame.Content”,我可以访问我的Progress-Bar / Text对象并简单地设置属性。 但我不喜欢这样。
    我认为必须有一种方法可以使用{Binding …}值设置Progress-Bar / Text,这样可以使代码更清晰。 因此,如何绑定ProgressBar和ProgressText的属性“Visibility”,以便它们在LoadSomething()开始时变为“可见”,并在处理完成时变为“折叠”?

提前致谢!

亲切的问候

那你几乎已经解决了,至少你描述了第二点的解决方案。

  1. 这个非常简单,只需将TextBlock和ProgressBar放在xaml的Pivot下,这样它们就可以渲染pivot元素的ontop。 然后,您可以使用Horizo​​ntalAlignment,VerticalAlignment和Margin来排列它们。 下面的代码应该将它们放在页面的中间:

      ...               
  2. 几乎一样容易。 我怀疑你的ViewModel实现了INotifyPropertyChanged或派生自某些类吗? 如果没有,那就不行了。 然后将此属性添加到ViewModel:

     private bool _IsLoading = false; public bool IsLoading { get { return _IsLoading; } set { if (_IsLoading != value) { _IsLoading = value; NotifyPropertyChanged("IsLoading"); } } } 

    (NotifyPropertyChanged是您的版本:

     public event PropertyChangedEventHandler PropertyChanged; public void NotifyPropertyChanged(String name) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) handler(this, new PropertyChangedEventArgs(name)); } 

如果您还没有BooleanToVisibilityConverter,请添加此类以将布尔值转换为xaml中的可见性:

 public class BooleanToVisibilityConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return (value is bool && (bool)value) ? Visibility.Visible : Visibility.Collapsed; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return value is Visibility && (Visibility)value == Visibility.Visible; } } 

在MainPage创建中,您可以设置整个页面的DataContext,也可以只将TextBlock和ProgressBar设置为ViewModel(或者通过Resources和xaml进行,但这无关紧要),将BooleanToVisibilityConverter添加为Page资源,如下所示:

    

并将TextBlock和ProgressBar的Visibility属性绑定到ViewModel的IsLoading属性:

   

最后一件事:

在LoadSomething()的开头,你设置IsLoading = true; 在HandleResponse方法结束时IsLoading = false; 那应该这样做。