2016-05-12 36 views
0

我有我的viewmodel中的集合屬性綁定到一個GridView的一些問題。我使用的是MVVM light,我相信我已經正確地安裝了ViewModelLocator並在頁面的xaml中設置了DataContext。問題綁定GridView項目源viewmodel屬性

模型

public class Base 
{ 
    public ObservableCollection<Downloads> results { get; set; } 
} 
public class Downloads 
{ 
    public int id { get; set; } 
    public string name { get; set; } 
    public int trackNumber { get; set; } 
    public string mixName { get; set; } 
    public string title { get; set; } 
    public string slug { get; set; } 
    public string releaseDate { get; set; } 
    public string publishDate { get; set; } 
    public List<Artist> artists { get; set; } 
    public string artistNames 
    { 
     get 
     { 
      return (artists == null) 
       ? string.Empty 
       : string.Join(", ", artists.Select(a => a.name)); 
     } 
    } 
    public string artistNamesSlug 
    { 
     get 
     { 
      return (artists == null) 
       ? string.Empty 
       : string.Join("_", artists.Select(a => a.name)); 
     } 
    } 

    public Release release { get; set; } 
    public Label label { get; set; } 
    public Image images { get; set; } 
    public int downloadId { get; set; } 
    public string audioFormat { get; set; } 
    public string downloadUrl { get; set; } 
} 
public class Release 
{ 
    public int id { get; set; } 
    public string name { get; set; } 
    public string type { get; set; } 
    public string slug { get; set; } 
} 
public class Label 
{ 
    public int id { get; set; } 
    public string name { get; set; } 
    public string type { get; set; } 
    public string slug { get; set; } 
    public bool status { get; set; } 
} 
public class Image 
{ 
    public LargeImage large { get; set; } 
} 
public class LargeImage 
{ 
    public int id { get; set; } 
    public int width { get; set; } 
    public int height { get; set; } 
    public string url { get; set; } 
    public string secureUrl { get; set; } 
} 

視圖模型

public class AvailableViewModel : ViewModelBase 
{ 

    public AvailableViewModel() 
    { 

    } 

    private Base availableDownloads; 
    public Base AvailableDownloads 
    { 
     get 
     { 
      if(availableDownloads == null) 
      { 
       GetData(); 
      } 
      return availableDownloads; 
     } 
     set 
     { 
      availableDownloads = value; 
      RaisePropertyChanged(() => AvailableDownloads); 
     } 
    } 

    private async void GetData() 
    { 
     OAuth oauth = new OAuth(); 

     string httpMethod = "GET"; 
     string parameters = "status=available"; 
     string response = await oauth.GetData(OAuth.availDownloadsUrl, httpMethod, parameters); 

     Base availableDownloads = JsonConvert.DeserializeObject<Base>(response); 
    } 
} 

XAML

DataContext="{Binding Available, Source={StaticResource Locator}}"> 

<Page.Resources> 
    <DataTemplate x:Key="AvailableGridView"> 
     <Grid Margin="0,10,0,0"> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="*"/> 
       <ColumnDefinition Width="2*"/> 
      </Grid.ColumnDefinitions> 
      <Image Source="{Binding AvailableDownloads.images.large.url}" Grid.Column="0" /> 
      <StackPanel Grid.Column="1" VerticalAlignment="Top" Margin="20,0,0,0"> 
       <TextBlock Text="{Binding AvailableDownloads.title}" Style="{StaticResource BaseTextBlockStyle}" TextWrapping="Wrap"/> 
       <TextBlock Text="{Binding AvailableDownloads.release.name}" Style="{StaticResource BaseTextBlockStyle}"/> 
       <TextBlock Text="{Binding AvailableDownloads.artistNames}" Style="{StaticResource SubtitleTextBlockStyle}"/> 
      </StackPanel> 
     </Grid> 
    </DataTemplate> 
</Page.Resources> 


<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 
    <GridView ItemsSource="{Binding AvailableDownloads.results}" SelectionMode="Multiple" ItemTemplate="{StaticResource AvailableGridView}"/> 
</Grid> 

這可能是問題的一部分,但是當我在XAML設置在DataContext佈局顯示對象參考錯誤。我不知道爲什麼會發生,但應用程序將編譯並運行。我是MVVM的新手,我似乎無法弄清楚爲什麼我的綁定在這裏不起作用。

回答

0

這可能是問題的一部分,但是當我在xaml中設置DataContext時,佈局顯示對象引用錯誤。

這是「對象引用錯誤」NullReferenceException錯誤嗎?由於你的代碼不全面,我可以在幾個地方重現這個問題,但是我不知道究竟是哪個原因導致了你的問題。

首先,我改變了你的XAML代碼測試在這裏,我用X:綁定在DataTemplateGridView的:

... 
    DataContext="{Binding Available, Source={StaticResource Locator}}" 
    xmlns:model="using:[Namespace of your app].Model"> 
<Page.Resources> 
    <DataTemplate x:Key="AvailableGridView" x:DataType="model:Downloads"> 
     <Grid Margin="0,10,0,0"> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="*" /> 
       <ColumnDefinition Width="2*" /> 
      </Grid.ColumnDefinitions> 
      <!--<Image Source="{x:Bind downloadUrl}" Grid.Column="0" />--> 
      <StackPanel Grid.Column="1" VerticalAlignment="Top" Margin="20,0,0,0"> 
       <TextBlock Text="{x:Bind title}" Style="{StaticResource BaseTextBlockStyle}" TextWrapping="Wrap" /> 
       <TextBlock Text="{x:Bind release.name}" Style="{StaticResource BaseTextBlockStyle}" /> 
       <TextBlock Text="{x:Bind artistNames}" Style="{StaticResource SubtitleTextBlockStyle}" /> 
      </StackPanel> 
     </Grid> 
    </DataTemplate> 
</Page.Resources> 

<Grid> 
    <GridView ItemsSource="{Binding AvailableDownloads.results}" SelectionMode="Multiple" ItemTemplate="{StaticResource AvailableGridView}" /> 
</Grid> 

我改變了你Base類是這樣的:

public class Base 
{ 
    public ObservableCollection<Downloads> results { get; set; } 

    public Base() 
    { 
     results = new ObservableCollection<Downloads>(); 
    } 
} 

這如果您在添加數據時未創建ObservableCollection<Downloads>的新實例,則可能是一個可能的原因。

我改變了你的AvailableViewModel()這樣,加入到result的數據是假的,只是爲了測試:

public AvailableViewModel() 
{ 
    availableDownloads = new Base(); 
    availableDownloads.results.Add(new Downloads { title = "11111", artistNames = "222", release = new Release(0, "333", "", "") }); 
    availableDownloads.results.Add(new Downloads { title = "11111" }); 
    availableDownloads.results.Add(new Downloads { title = "11111" }); 
    availableDownloads.results.Add(new Downloads { title = "11111" }); 
    availableDownloads.results.Add(new Downloads { title = "11111" }); 
} 

正如你所看到的,我創建Base類的新實例這裏。

我注意到,在您的XAML代碼,您使用的release.name,對於這一點,我想你需要修改Release類是這樣的:

public class Release 
{ 
    public Release() 
    { 
    } 

    public Release(int Id, string Name, string Type, string Slug) 
    { 
     this.id = Id; 
     this.name = Name; 
     this.type = Type; 
     this.slug = Slug; 
    } 

    public int id { get; set; } 
    public string name { get; set; } 
    public string type { get; set; } 
    public string slug { get; set; } 
} 

而且你可以創建Release類這樣的實例:

release = new Release(0, "333", "", ""); 

我評論的Image控制在DataTemplate,要做好這項工作,需要修改LargeImageImageRelease類。