So, I am making an app for wp7.
To keep it simple, these are my files:
- LoginPage.xaml (the starup page)
- MainPage.xaml
- MainViewModel.cs
- ItemViewModel.cs
In MainViewModel.cs I included the folowing function:
private void DownloadItems()
{
string key = this.User.Key;
WebClient wc = new WebClient();
wc.DownloadStringCompleted += callback;
wc.DownloadStringAsync(new Uri("http://localhost/items?key=" + key)); //JSON
}
and the callback function:
private void callback(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
List<ItemViewModel> col = Deserialize_ItemViewModel(e.Result); // deserialize JSON to List<ItemViewModel>
this.Items = new ObservableCollection<ItemViewModel>(col);
ItemDB.Sponsors.InsertAllOnSubmit(col);
ItemDB.SubmitChanges();
this.IsDataLoaded = true;
// ???
}
}
When the user logs in the login will be processed and when everything is ok DownloadItems will be called which is using the freshly set User.Key.
What I need is to show a ProgressIndicator while the download is occuring and when the download is completed and processed I want to navigate to MainPage.xaml, which will be ready by that time.
I hope anyone can help me, thanks in advance!
I think I would try to solve it differently. Let your LoginPage only handle the login, then you re-direct to your main page.
In your view model, for the main page, you create a bool property called something like
Loading, which you can set to true during your async call. Bind this to the visible property of the progress bar so it shows whenLoadingis true, use a converter to handle bool -> visible. When the data is loaded you just set theLoadingto false which will result in the progress bar disappearing. At the same time you bind the visible property of the control/view toLoadingas well, but this will use a different converter that is the inverted value of the converter for the progress bar.Hope that will help.
UPDATE: I missed that you already have a
IsDataLoaded, is that on your view model? The converter should look something like:Then use it like:
Example code is taken from: http://dotnetbyexample.blogspot.com/2010/11/converter-for-showinghiding-silverlight.html