Mohd Anzal
Mohd Anzal

Reputation: 167

UWP-List View Content Empty

My List View items does not appear.Actually I am trying to collect the file names of Documents library into my list view.

Here is the Xaml:

 <ListView x:Name="Mylist" SelectionMode="Single" ShowsScrollingPlaceholders="True"
              Grid.ColumnSpan="2" ItemsSource="{x:Bind data}" >

        <ListView.ItemTemplate>
            <DataTemplate x:DataType="dat:Class1">
                <StackPanel>
                    <TextBlock Text="{Binding Key}" Style="{StaticResource TitleTextBlockStyle}"/>
                </StackPanel>
            </DataTemplate>   
        </ListView.ItemTemplate>

Class1 File:

namespace App1{
public class Class1:List<object>
{
    public Class1()
    {
    }
    public string Items { get; set; }
    public object Key { get; set; }
}}

Mainpagexaml.cs

     public ObservableCollection<Class1> _data;
    public ObservableCollection<Class1> data { get { return _data; } }

    public MainPage()
    {
        this.InitializeComponent();

    }
    protected async override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);
        _data = await Data.ItemsGrouped();
    }

Data.cs

  public async static Task<ObservableCollection<Class1>> generatename()

    {
        ObservableCollection<Class1> newdata = new ObservableCollection<Class1>();
        //var myfiles = await getfiles();
        StorageFolder folder = await KnownFolders.GetFolderForUserAsync(null, KnownFolderId.DocumentsLibrary);
        List<string> filetypefilter = new List<string>();
        filetypefilter.Add(".mp3");

        QueryOptions query = new QueryOptions(CommonFileQuery.OrderBySearchRank, filetypefilter);
        StorageFileQueryResult queryresult = folder.CreateFileQueryWithOptions(query);
        IReadOnlyList<StorageFile> files = await queryresult.GetFilesAsync();
        foreach (var file in files)
        {
            newdata.Add(new Class1() { Items = file.Name });
            var f = file.Name;
        }
        return newdata;

    }
    public async static Task<ObservableCollection<Class1>> ItemsGrouped()
    {

        ObservableCollection<Class1> cgroup = new ObservableCollection<Class1>();
        var x = await generatename();
        var query = from item in x
                    group item by item.Items[0] into g
                    orderby g.Key
                    select new { GroupName = g.Key, Items = g };


        foreach (var item in query)
        {
            Class1 c1 = new Class1();
            c1.Key = item.GroupName;
            foreach (var item2 in item.Items)
            {
                c1.Add(item2);
                c1.Items = item2.Items;
            } 
            cgroup.Add(c1);
        }
        return cgroup;
    }

I found that filenames are read from data.cs file,But no result in the UI.

Upvotes: 2

Views: 497

Answers (1)

Igor Kulman
Igor Kulman

Reputation: 16361

The problem is you are "replacing" your observable collection. Instead of doing the assignment _data = await Data.ItemsGrouped(); create it just once in the constructor (_data = new ObservableCollection<Class1>()) and then never assign it again, just clear it and fill it with data.

_data.Clear();
_data.AddRange(await Data.ItemsGrouped());

Also make the ItemsGrouped method return a list instead of an observable collection.

Upvotes: 2

Related Questions