我使用兩個不同的Web客戶端來下拉數據以顯示在我的應用程序的主頁面中。我想將這些代碼移動到app.xaml.cs中,以在用戶進入主頁面之前下載數據。我不確定如何在主頁面上爲我的列表框設置itemssource。這是迄今爲止我所擁有的。內Application_LaunchingApplication_Launch中的WP7 webclient
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>
感謝您的幫助。
UPDATE
見上面更新的代碼和下面
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,所以我不確定我應該放在這裏。該應用程序運行沒有任何問題,但沒有數據顯示。
請看我的更新,我已經嘗試過。我從來沒有使用視圖模型,所以我不知道它應該如何設置。 – nos9 2012-04-26 18:56:47
您必須觸發PropertyChanged事件。看到我更新的帖子。如果您是MVVM的新手,請做一些調查。這裏是一個很好的小文章開始 http://www.windowsphonegeek.com/articles/Windows-Phone-Mango-Getting-Started-with-MVVM-in-10-Minutes – 2012-04-26 19:24:38
我明白你在說什麼,但是這段代碼給了我很多錯誤,包括「_genres是一個字段,但是像一個類型一樣使用」。我會閱讀MVVM,看看我能不能找出一些東西。 – nos9 2012-04-26 20:16:14