我使用两个不同的网络客户端来下拉数据以显示在我的应用程序的主页上。我想将此代码移动到 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,所以我不确定我应该在这里放什么。该应用程序运行没有任何问题,但没有显示任何数据。