Windows Runtime GridView在设置ItemsSource属性时引发ArgumentException [英] Windows Runtime GridView throws ArgumentException when setting ItemsSource property

查看:99
本文介绍了Windows Runtime GridView在设置ItemsSource属性时引发ArgumentException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public class ComicDataWNGroup 
{
public ComicDataWNGroup(string title)
{
this.Title = title;
this.Items = new ObservableCollection< ComicDataItem>();
}

public String标题{get;组; }

public ObservableCollection< ComicDataItem>物品{get;私人集合}
}

public class ComicDataWNSource
{
private RedditAPI.RedditManager currentManager;

public ComicDataWNSource(RedditAPI.RedditManager currentManager)
{
this.currentManager = currentManager;
Groups = new ObservableCollection< ComicDataWNGroup>();
}

public async任务< int> ObtainRecentContent()
{
SubredditComicDataSource comicList = new SubredditComicDataSource();
await comicList.LoadItems();
var comicSources = comicList.GetAllItems();

for(int i = 0; i< comicSources.Count; i ++)
{
var links = await this.currentManager.GetSubredditLinks(comicSources [i] .UrlTitle, 6,0);
ComicDataWNGroup group = new ComicDataWNGroup(From+ comicSources [i] .Title);
for(int j = 0; j< links.Data.Count; j ++)
{
var linkItem =(RedditAPI.Objects.Link)links.Data [j];
BitmapImage img = await ThumbnailRetriever.GetThumbnailFromRedditUrl(linkItem.ThumbnailUrl);
ComicDataItem item = new ComicDataItem(linkItem.Title,false,true,0,img,0,null,null,Visibility.Visible,Visibility.Collapsed);
group.Items.Add(item);
}
Groups.Add(group);
}

return 0;
}

public ObservableCollection< ComicDataWNGroup>组
{
{get;私人集合}
}
}

注意:ComicDataItem是一个主要包含get设定属性。如果需要,我可以稍后再发布。



使用这两个类,我试图将ComicDataWNSource对象的Groups属性绑定到GridView。



以下代码是我用于绑定的代码。

  DataModel.ComicDataWNSource source = new DataModel.ComicDataWNSource(App.Manager); 
await source.ObtainRecentContent();

CollectionViewSource viewSource = new CollectionViewSource();
viewSource.Source = source.Groups;
ViewSource.IsSourceGrouped = true;
viewSource.ItemsPath = new PropertyPath(Items);

this.itemGridView.ItemsSource = viewSource;

GridView是Visual Studio在创建新的分组项目页面时生成的默认值。我已经更改了适当的绑定以匹配ComicDataWNGroup类和ComicDataItem类中的属性(如果需要,我可以发布GridView的XAML)。此外,source.ObtainRecentContent()正在填充具有正确数据的Groups数组。 组和内部数组的数组都包含对象,并且它们都不为空。



问题出现在将视图源设置为网格视图。每当我尝试它,它会抛出以下异常:



参数异常:值不在预期范围内。



我不知道我可能做错了这个事情。我已经看过这个 msdn页面,其中显示你如何使用分组的网格视图项目和查看源代码。任何想法我在做错什么?

解决方案

好的,我设法通过执行以下操作来完成这项工作:



在我定义了GridView的xaml中,我将CollectionViewSource声明为相同的xaml文件,而不是文件后面的代码。

 < Page.Resources> 
< CollectionViewSource x:Name =itemsViewSource>< / CollectionViewSource>
< /Page.Resources>

我将GridView的ItemsSource属性绑定到以下属性:

 < GridView 
x:Name =itemGridView
ItemsSource ={Binding Source = {StaticResource itemsViewSource}}
< / GridView>

在代码背后,我将代码更改为以下内容:

  DataModel.ComicDataWNSource source = new DataModel.ComicDataWNSource(App.Manager); 
await source.ObtainRecentContent();

itemsViewSource.Source = source.Groups
itemsViewSource.IsSourceGrouped = true;
this.itemsViewSource.ItemsPath = new PropertyPath(Items);

通过进行这些更改,代码可以工作。我现在确定我做错了的事情是ItemsSource期望一个指向CollectionViewSource对象的Binding对象,而不是实际的CollectionViewSource对象。


I have the following two classes for a RageComic reader metro style app I am making.

public class ComicDataWNGroup
{
    public ComicDataWNGroup(string title)
    {
        this.Title = title;
        this.Items = new ObservableCollection<ComicDataItem>();
    }

    public String Title { get; set; }

    public ObservableCollection<ComicDataItem> Items { get; private set; }
}

public class ComicDataWNSource
{
    private RedditAPI.RedditManager currentManager;

    public ComicDataWNSource(RedditAPI.RedditManager currentManager)
    {
        this.currentManager = currentManager;
        Groups = new ObservableCollection<ComicDataWNGroup>();
    }

    public async Task<int> ObtainRecentContent()
    {
        SubredditComicDataSource comicList = new SubredditComicDataSource();
        await comicList.LoadItems();
        var comicSources = comicList.GetAllItems();

        for (int i = 0; i < comicSources.Count; i++ )
        {
            var links = await this.currentManager.GetSubredditLinks(comicSources[i].UrlTitle, 6, 0);
            ComicDataWNGroup group = new ComicDataWNGroup("From " + comicSources[i].Title);
            for(int j = 0; j < links.Data.Count; j++)
            {
                var linkItem = (RedditAPI.Objects.Link)links.Data[j];
                BitmapImage img = await ThumbnailRetriever.GetThumbnailFromRedditUrl(linkItem.ThumbnailUrl);
                ComicDataItem item = new ComicDataItem(linkItem.Title, false, true, 0, img, 0, null, null, Visibility.Visible, Visibility.Collapsed);
                group.Items.Add(item);
            }
            Groups.Add(group);
        }

        return 0;
    }

    public ObservableCollection<ComicDataWNGroup> Groups
    {
        { get; private set; }
    }
}

Note: ComicDataItem is a class that mostly contains get set properties. I can post it later if it is needed.

Using these two classes, I am trying to bind the Groups property of the ComicDataWNSource object to a GridView.

The following code is the code I use for the binding.

DataModel.ComicDataWNSource source = new DataModel.ComicDataWNSource(App.Manager);
await source.ObtainRecentContent();

CollectionViewSource viewSource = new CollectionViewSource();
viewSource.Source = source.Groups;
viewSource.IsSourceGrouped = true;
viewSource.ItemsPath = new PropertyPath("Items");

this.itemGridView.ItemsSource = viewSource;

The GridView is the default one generated by Visual Studio when creating a new Grouped Items Page. I already changed the appropriate bindings to match up with the properties in the ComicDataWNGroup class and ComicDataItem class (I can post the XAML for the GridView as well if needed). Also, source.ObtainRecentContent() is currently filling up the Groups array with the correct data. The array of Groups and inner array of Items both have objects in them and neither of them are null.

The problem arises on the line setting the view source to the grid view. Whenever I try it, it throws the following exception:

Argument Exception: Value does not fall within the expected range.

I don't have a clue what I could be doing wrong for this to happen. I already looked at this msdn page already, which shows you how to use a grouped grid view item and view source. Any ideas what I'm doing wrong?

解决方案

Ok, I managed to make this work by doing the following:

In the xaml where my GridView is defined, I declared the CollectionViewSource in the same xaml file instead of the code behind file.

<Page.Resources>
    <CollectionViewSource x:Name="itemsViewSource" ></CollectionViewSource>
</Page.Resources>

I bound the ItemsSource property of the GridView with the following property:

<GridView
    x:Name="itemGridView"
    ItemsSource="{Binding Source={StaticResource itemsViewSource}}"
</GridView>

In the code behind, I changed the code to the following:

DataModel.ComicDataWNSource source = new DataModel.ComicDataWNSource(App.Manager);
await source.ObtainRecentContent();

itemsViewSource.Source = source.Groups;
itemsViewSource.IsSourceGrouped = true;
this.itemsViewSource.ItemsPath = new PropertyPath("Items");

By making these changes, the code works. I'm now sure the thing I was doing wrong is that ItemsSource expects a Binding object which points to the CollectionViewSource object, not the actual CollectionViewSource object.

这篇关于Windows Runtime GridView在设置ItemsSource属性时引发ArgumentException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆