我的列表视图项目没有出现。实际上我正在尝试将文档库的文件名收集到我的列表视图中。
这是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档案:
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;
}
我发现文件名是从data.cs文件中读取的,但没有结果在UI中。
答案 0 :(得分:2)
问题是你正在“替换”你的可观察集合。而不是执行赋值_data = await Data.ItemsGrouped();
只在构造函数(_data = new ObservableCollection<Class1>()
)中创建它一次,然后再也不再分配它,只需清除它并用数据填充它。
_data.Clear();
_data.AddRange(await Data.ItemsGrouped());
同时使ItemsGrouped
方法返回一个列表而不是一个可观察的集合。