Wp7,C# 在 viewModel 中下载完成时导航
所以,我正在为 wp7 制作一个应用程序。 为了简单起见,这些是我的文件:
- LoginPage.xaml(启动页面)
- MainPage.xaml
- MainViewModel.cs
- ItemViewModel.cs
在 MainViewModel.cs 中,我包含以下函数:
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
}
和回调函数:
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;
// ???
}
}
当用户登录时,登录将进行处理,当一切正常时,将调用使用新设置的 User.Key 的 DownloadItems。
我需要的是在下载发生时显示 ProgressIndicator,当下载完成并处理时,我想导航到 MainPage.xaml,届时它将准备就绪。
我希望任何人都可以帮助我,提前致谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想我会尝试以不同的方式解决它。让您的登录页面仅处理登录,然后您重定向到主页。
在您的视图模型中,为主页创建一个名为
Loading
的 bool 属性,您可以在异步调用期间将其设置为 true。将其绑定到进度条的可见属性,以便在Loading
为 true 时显示,使用转换器来处理 bool ->可见的。加载数据时,您只需将Loading
设置为 false,这将导致进度条消失。同时,您还将控件/视图的可见属性绑定到 Loading,但这将使用不同的转换器,即进度条转换器的反转值。希望这会有所帮助。
更新:我错过了您已经有一个
IsDataLoaded
,它在您的视图模型上吗?转换器应类似于:然后使用它:
示例代码取自: http://dotnetbyexample.blogspot.com/2010/11/converter-for-showinghiding-silverlight.html
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 whenLoading
is true, use a converter to handle bool -> visible. When the data is loaded you just set theLoading
to false which will result in the progress bar disappearing. At the same time you bind the visible property of the control/view toLoading
as 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
当您以任何异步方式更新 UI 时,您可以按以下方式使用 Dispatcher,
我认为这对您有帮助。
when you update UI in any asynchronous, then you use Dispatcher in the following way
I think it hepls to you.