gpt4 book ai didi

c# - Wp7,C#在viewModel中完成下载时导航

转载 作者:行者123 更新时间:2023-12-03 10:49:45 25 4
gpt4 key购买 nike

所以,我正在为 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,届时它将准备就绪。

    我希望任何人都可以帮助我,在此先感谢!

    最佳答案

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

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

    希望这会有所帮助。

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

    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

    关于c# - Wp7,C#在viewModel中完成下载时导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9278130/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com