【问题标题】:WP7 webclient in Application_LaunchingApplication_Launching 中的 WP7 网络客户端
【发布时间】:2012-04-27 05:59:45
【问题描述】:

我使用两个不同的网络客户端来下拉数据以显示在我的应用程序的主页上。我想将此代码移动到 app.xaml.cs 以在用户访问主页之前下载数据。我不确定如何在主页上为我的列表框设置 itemssource。这是我目前所拥有的。

Application_Launching 中的代码

     private void Application_Launching( object sender, LaunchingEventArgs e)
    {
        // WebClient jsonGenres
        WebClient jsonGenres = new WebClient();
        Uri apiGenres = new Uri( "http://api.beatport.com/catalog/3/genres" );
        jsonGenres.DownloadStringCompleted += newDownloadStringCompletedEventHandler (jsonGenres_GetDataCompleted);
        jsonGenres.DownloadStringAsync(apiGenres);

        // WebClient jsonHome
        WebClient jsonHome = new WebClient();
        Uri apiHome = new Uri ("http://api.beatport.com/catalog/3/beatport/home" );
        jsonHome.DownloadStringCompleted += newDownloadStringCompletedEventHandler (jsonHome_GetDataCompleted);
        jsonHome.DownloadStringAsync(apiHome);

    }

    // Deserialize genres data
    public void jsonGenres_GetDataCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        GenresHome genresData = JsonConvert.DeserializeObject<GenresHome>(e.Result);

        ViewModel.Genres = genresData.results;
        //this.listGenres.ItemsSource = genresData.results;
    }

    // Deserialize home page data
    public void jsonHome_GetDataCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        ReleasesHome homeData = JsonConvert.DeserializeObject<ReleasesHome>(e.Result);

        const int limit = 6;
        ViewModel.Releases = homeData.results.featuredReleases.Take(limit);
        //this.listRelease.ItemsSource = homeData.results.featuredReleases.Take(limit);
    }

还有我的主页 xaml 代码。

                <ListBox x:Name="listRelease" ItemsSource="{Binding ReleasesHome}" Grid.Row="0" ScrollViewer.VerticalScrollBarVisibility="Disabled" >
                    <ListBox.ItemsPanel>
                        <ItemsPanelTemplate>
                            <toolkit:WrapPanel Orientation="Horizontal" />
                        </ItemsPanelTemplate>
                    </ListBox.ItemsPanel>
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Vertical">
                                <toolkit:HubTile Source="{Binding images.large.url}" MouseLeftButtonUp="releaseSelectedHandler" Margin="10" IsFrozen="True" />
                                <TextBlock Text="{Binding name}" Width="173" />
                                <ListBox ItemsSource="{Binding artists}" Height="28" ScrollViewer.VerticalScrollBarVisibility="Disabled" >
                                    <ListBox.ItemTemplate>
                                        <DataTemplate>
                                            <TextBlock Text="{Binding name}" Margin="10,0,0,0" Width="173" Style="{StaticResource PhoneTextSubtleStyle}" />
                                        </DataTemplate>
                                    </ListBox.ItemTemplate>
                                </ListBox>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </Grid>
        </controls:PanoramaItem>

    <!--Panorama item four-->          
    <controls:PanoramaItem x:Name="genres" Header="genres">
    <!--Single line list-->
            <Grid>
                <ListBox x:Name="listGenres" ItemsSource="{Binding GenresHome}">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Vertical">
                                <TextBlock x:Name="genresTxtBlock" Text="{Binding name}" MouseLeftButtonUp="genreSelectedHandler" Margin="10,5,0,0" Style="{StaticResource PhoneTextExtraLargeStyle}" />                                      
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>       
            </Grid>        
    </controls:PanoramaItem>

感谢您的帮助。

更新

查看上面的更新代码和下面的 ViewModel

public class MainViewModel : INotifyPropertyChanged
{
    public MainViewModel()
    {

    }

    private IEnumerable<ResultGenreHome> _genres; // backing field
    public IEnumerable<ResultGenreHome> GenresHome
    { 
        get { return _genres; }
        set
        {
            _genres = value;
            OnPropertyChanged("GenresHome");
        }
    }

    private IEnumerable<FeaturedReleasesHome> _releases; // backing field
    public IEnumerable<FeaturedReleasesHome> ReleasesHome
    {
        get { return _releases; }
        set
        {
            _releases = value;
            OnPropertyChanged("ReleasesHome");
        }
    }

    private void OnPropertyChanged(string p)
    {
        throw new NotImplementedException();
    }

    public bool IsDataLoaded
    {
        get;
        private set;
    }

    public void LoadData()
    {
        this.IsDataLoaded = true;
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (null != handler)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

我已经在主页后面的代码中设置了数据上下文。这是我第一次使用 ViewModel,所以我不确定我应该在这里放什么。该应用程序运行没有任何问题,但没有显示任何数据。

【问题讨论】:

    标签: c# windows-phone-7 xaml


    【解决方案1】:

    查看默认的“Windows Phone 数据绑定应用程序”Windows Phone 项目。这个项目有一个它在应用程序中创建的 ViewModel,这个 ViewModel 由 MainPage 使用。 MainPage 然后使用 Binding 来获取它需要的数据。您可以通过以下方式在 App 中设置 ViewModel 中的数据

    ViewModel.Genres = genresData.results;
    

    那么你的 xaml 会是这样的

    <ListBox ItemsSource="{Binding Genres}">
        ...
    </ListBox>
    

    为了在设置流派时更新 UI,ViewModel 中的属性必须触发 PropertyChanged 事件

    private IEnumerable<Genre> _genres; // backing field
    public IEnumerable<Genre> Genres
    { 
        get { return _genres; }
        set
        {
            _genres = value;
            OnPropertyChanged("Genres");
        }
    }
    

    Release 部分也是如此。

    【讨论】:

    • 请查看我的更新以了解我的尝试。我以前从未使用过视图模型,所以我不确定它应该如何设置。
    • 您必须触发 PropertyChanged 事件。请参阅我更新的帖子。如果您是 MVVM 新手,请做一些研究。这是一篇不错的入门文章windowsphonegeek.com/articles/…
    • 我明白你在说什么,但该代码给了我很多错误,包括“_genres 是一个字段但用作类型”。我会阅读 MVVM 并看看我是否能弄清楚。
    • 糟糕,检查更新。我忘记了物业上的“流派”名称
    • 好的,我想我很接近了。我唯一的问题是OnPropertyChanged 方法存根上的NotImplementedException。我该如何处理?查看我更新的 ViewModel 代码。
    【解决方案2】:

    您可以向 App 类添加一个属性并将其设置为保存下载和反序列化的数据。

    您所要做的就是使用此属性加载页面的数据上下文。

    编辑

    覆盖 App 类的 OnStartup 并从 App.xaml 中删除 StartURI:

    base.OnStartUp(e); Window1 w = new Window1(); w.DataContext = ViewModel; this.MainWindow = w; w.Show();

    【讨论】:

    • 感谢您的回答。您能否提供一个如何完成此操作的示例。还是个初级程序员。
    • 据我所知,缺少的只是将 Window 的 DataContext 设置为 App.ViewModel。找到您打开窗口/视图的位置并将其 DataContext 分配给 App.ViewModel。如果找不到它,您应该删除 App.xaml 中的 StartupURI 并覆盖 App 类的 OnStartup 方法。在这里查看我的答案:stackoverflow.com/questions/6306549/override-onstartup-in-wpf
    猜你喜欢
    • 1970-01-01
    • 2011-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-10
    • 1970-01-01
    相关资源
    最近更新 更多