まず、Windows Phone 8 の開発に関する私の経験はそれほど大きくはありませんが、ASP.NET Framework のように見えるものもあります。
バックグラウンドで Web リクエストを実行しているときに表示され、リクエストが処理されると非表示になる、不確定な進行状況バーが必要です。
私のソリューションは機能しますが、満足していません
(機能をテストするために、progressBar/Text が 1 つのピボット要素内にありました)
。
Pivot 要素を含む "MainPage" という XAML ページ。
<phone:PhoneApplicationPage>
...
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!-- HERE I want the progress bar and loading-text to show up if possible -->
<TextBlock Name="ProgressText" Text="Loading..." Visibility="Collapsed"/>
<ProgressBar Name="ProgressBar" Visibility="Collapsed" IsIndeterminate="True"/>
<!-- HERE I want the progress bar and loading-text to show up if possible -->
<phone:Pivot Title="MyTitle">
<!-- Here are my PivotItems -->
</phone:Pivot>
</Grid>
<phone:PhoneApplicationPage>
私の分離コードは次のようになります。
protected override void OnNavigatedTo(
App.ViewModel.LoadSomething();
}
関数 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;
});
}
だから今私の質問:
ナビゲートしたピボット要素にプログレスバーとローディングテキストを表示することはできますか?
ご覧のとおり、参照 "(MainPage)App.RootFrame.Content" によって、Progress-Bar/Text オブジェクトに到達し、属性を設定するだけです。しかし、私はこの方法が好きではありません。
Progress-Bar/Text に {Binding ...} 値を設定して、コードをよりクリーンにする方法が必要だと思いました。したがって、ProgressBar と ProgressText の属性「Visibility」をバインドして、LoadSomething() の開始時に「可視」になり、処理が終了したときに「折りたたみ」になるようにするにはどうすればよいでしょうか?
前もって感謝します!
敬具