2012-04-26 5 views
2

私は、アプリケーションのメインページに表示するデータをプルダウンするために2つの異なるWebクライアントを使用します。このコードをapp.xaml.csに移動して、ユーザーがメインページに入る前にデータをダウンロードしたいと思います。メインページのリストボックスのitemssourceを設定する方法がわかりません。ここに私がこれまで持っていたものがあります。 Application_LaunchingWPLのWebClientを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> 

ありがとうございました。

UPDATE

更新上記のコードと

私はメインページの背後にあるコードでのDataContextを設定した
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を参照してください。これはViewModelを初めて使用したので、私はここに何を入れなければならないのか正確にはわかりません。アプリは問題なく実行されますが、データは表示されません。

答えて

0

デフォルトの「Windows Phone Databoundアプリケーション」Windows Phoneプロジェクトを見てください。このプロジェクトには、アプリケーション内で作成したViewModelがあり、このViewModelはMainPageで使用されます。 MainPageはBindingを使用して必要なデータにアクセスします。あなたはであなたの財産、そして、あなたのXAMLを使用して、ジャンルを設定したときに更新されるUIのために

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

ようになり

ViewModel.Genres = genresData.results; 

を行うことによって、アプリケーションからのViewModelにデータを設定することができますViewModelはPropertyChangedイベントを発生させる必要があります

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

リリースパートと同様です。

+0

私が試みたことについては私のアップデートをご覧ください。以前はビューモデルを使ったことがなかったので、どのようにセットアップするべきか分かりません。 – nos9

+0

PropertyChangedイベントを発生させる必要があります。私の更新された記事を見てください。 MVVMを初めてお使いの場合は、いくつかの調査をしてください。ここに素敵な小さな記事があります。 http://www.windowsphonegeek.com/articles/Windows-Phone-Mango-Getting-Started-with-MVVM-in-10-Minutes –

+0

私はあなたの言うことを理解しています。そのコードは "_genresはフィールドですが型のように使われます"を含む多くのエラーを表示します。私はMVVMを読んで、何かを理解できるかどうかを見ます。 – nos9

0

Appクラスにプロパティを追加し、ダウンロードしてデシリアライズしたデータを保持するように設定できます。

あなたがしなければならないことは、このプロパティを使ってページのデータコンテキストをロードすることだけです。

EDIT

オーバーライドしアプリクラスのOnStartupとApp.xamlからStartURIを削除:

base.OnStartUp(E); Window1 w =新しいWindow1(); w.DataContext = ViewModel; this.MainWindow = w; w.Show();

+0

お返事ありがとうございます。これがどのように行われたかの例を教えてください。まだ初心者プログラマー。 – nos9

+0

私の知る限り、欠落しているのはApp.ViewModelへのWindowのDataContextの設定です。ウィンドウ/ビューを開き、そのDataContextをApp.ViewModelに割り当てる場所を探します。見つからない場合は、App.xaml内のStartupURIを削除し、AppクラスのOnStartupメソッドをオーバーライドする必要があります。私の答えはこちらを参照してください:http://stackoverflow.com/questions/6306549/override-onstartup-in-wpf –