Application_Launching中的WP7 webclient

时间:2012-04-26 16:47:58

标签: c# windows-phone-7 xaml

我使用两个不同的网络客户端来下载数据以显示在我的应用的主页面中。我想将此代码移至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));
        }
    }

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

2 个答案:

答案 0 :(得分:0)

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

您所要做的就是使用此属性加载页面的datacontext。

修改

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

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

答案 1 :(得分:0)

查看默认的“Windows Phone数据绑定应用程序”Windows Phone项目。此项目具有在App中创建的ViewModel,并且MainPage使用此ViewModel。然后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");
    }
}

发布部分也是如此。