Wp7,C# 在 vi​​ewModel 中下载完成时导航

发布于 2025-01-05 16:21:06 字数 1179 浏览 2 评论 0原文

所以,我正在为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦幻的心爱 2025-01-12 16:21:06

我想我会尝试以不同的方式解决它。让您的登录页面仅处理登录,然后您重定向到主页。

在您的视图模型中,为主页创建一个名为 Loading 的 bool 属性,您可以在异步调用期间将其设置为 true。将其绑定到进度条的可见属性,以便在 Loading 为 true 时显示,使用转换器来处理 bool ->可见的。加载数据时,您只需将 Loading 设置为 false,这将导致进度条消失。同时,您还将控件/视图的可见属性绑定到 Loading,但这将使用不同的转换器,即进度条转换器的反转值。

希望这会有所帮助。

更新:我错过了您已经有一个IsDataLoaded,它在您的视图模型上吗?转换器应类似于:

public class VisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, 
               CultureInfo culture)
    {
        if (value != null && value is bool && parameter != null)
        {
            var bValue = (bool) value;
            var visibility = (Visibility)Enum.Parse(
            typeof (Visibility), parameter.ToString(),true);
            if (bValue) return visibility;
            return visibility == 
            Visibility.Visible ? Visibility.Collapsed : Visibility.Visible;
        }

        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter,
                   CultureInfo culture)
    {
         throw new NotImplementedException();
    }
}

然后使用它:

Visibility="{Binding IsDownloading, Converter={StaticResource VisibilityConverter}, ConverterParameter=Visible}"

示例代码取自: 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 when Loading is true, use a converter to handle bool -> visible. When the data is loaded you just set the Loading to false which will result in the progress bar disappearing. At the same time you bind the visible property of the control/view to Loading 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:

public class VisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, 
               CultureInfo culture)
    {
        if (value != null && value is bool && parameter != null)
        {
            var bValue = (bool) value;
            var visibility = (Visibility)Enum.Parse(
            typeof (Visibility), parameter.ToString(),true);
            if (bValue) return visibility;
            return visibility == 
            Visibility.Visible ? Visibility.Collapsed : Visibility.Visible;
        }

        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter,
                   CultureInfo culture)
    {
         throw new NotImplementedException();
    }
}

Then use it like:

Visibility="{Binding IsDownloading, Converter={StaticResource VisibilityConverter}, ConverterParameter=Visible}"

Example code is taken from: http://dotnetbyexample.blogspot.com/2010/11/converter-for-showinghiding-silverlight.html

友谊不毕业 2025-01-12 16:21:06

当您以任何异步方式更新 UI 时,您可以按以下方式使用 Dispatcher,

Dispatcher.BeginInvoke(delegate

{

 NavigationService.Navigate(new Uri("/Folder/pagename.xaml", UriKind.Relative));

});

我认为这对您有帮助。

when you update UI in any asynchronous, then you use Dispatcher in the following way

Dispatcher.BeginInvoke(delegate

{

 NavigationService.Navigate(new Uri("/Folder/pagename.xaml", UriKind.Relative));

});

I think it hepls to you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文