I'm new to WP. I try to show ProgressIndicator
when loading data from Server and hide it when loading finished. However, I had a problem: "The ProgressIndicator
only show when my MessageBox
showed. Here's my code:
private void MainPageLoaded(object sender, RoutedEventArgs e)
{
// Create progress loading
SystemTray.ProgressIndicator = new ProgressIndicator();
SystemTray.ProgressIndicator.IsIndeterminate = true;
SystemTray.ProgressIndicator.IsIndeterminate = true;
SystemTray.ProgressIndicator.Text = "Loading...";
SyncDbIfNeed();
}
private void ShowHideProgressIndicator(Boolean isVisible)
{
SystemTray.ProgressIndicator.IsVisible = isVisible;
SystemTray.ProgressIndicator.IsIndeterminate = isVisible;
Debug.WriteLine("ShowHide: " + isVisible);
}
private async void SyncDbIfNeed()
{
if (!MySettings.IsCategorySynced())
{
ShowHideProgressIndicator(true);
try
{
HttpClient httpClient = new HttpClient();
String json = await httpClient.GetStringAsync(MyConstants.UrlGetAllCategory);
MessageBox.Show(json);
}
catch (Exception e)
{
MessageBox.Show("Unexpected error");
}
ShowHideProgressIndicator(false);
}
}
}
Can anyone explain it, and give me an advice? Thanks.
async void
methods should only be used for event handlers. Any other asynchronous method should return aTask
orTask<T>
.You should also split UI loginc from non-UI logic.
Try this instead:
To learn more about
async
-await
, take a look at my curah.