我正在WP 8.1中开发一个音乐播放器应用程序,我正在尝试在其中实现跳转列表功能。为了实现跳转列表,我遵循此处给出的示例。
我正在对可用代码进行一些必要的更改,这就是我实现所需功能的方式,因为我在 C#、MVVM 和 wp 8.1 方面的知识很少。
但是我在行的视图模型中遇到错误
var items = ContactModel.CreateSampleData();
错误是:
Cannot assign void to an implicitly-typed local variable
可能的原因是什么????谁能帮助我了解我哪里出错了。
using System.Collections;
using Windows.UI.Xaml.Data;
using JumpListSample.Common.JumpList;
using System.Collections.Generic;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using System;
using Windows.Storage;
using Windows.Storage.Search;
using System.Collections.ObjectModel;
namespace JumpListSample.ViewModels
{
public class ContactsViewModel
{
private IList data;
public IList Data
{
get
{
if (data == null)
{
var items = ContactModel.CreateSampleData();
data = items.ToAlphaGroups(x => x.Name);
}
return data;
}
}
private CollectionViewSource collection;
public CollectionViewSource Collection
{
get
{
if (collection == null)
{
collection = new CollectionViewSource();
collection.Source = Data;
collection.IsSourceGrouped = true;
}
return collection;
}
}
}
public class ContactModel
{
// constructor
public ContactModel()
{
Name = "name";
Albumart = new BitmapImage(new Uri("ms-appx:///Assets/Logo.scale-240.png"));
}
public async static void CreateSampleData()
{
ObservableCollection<ContactModel> data = new ObservableCollection<ContactModel>();
try
{
IReadOnlyList<IStorageItem> MusicLibrary = await KnownFolders.MusicLibrary.GetFoldersAsync(CommonFolderQuery.GroupByAlbum);
foreach (IStorageItem item in MusicLibrary)
{
ContactModel obj = new ContactModel();
IStorageItem musicItem = item;
obj.Name = musicItem.Name;
obj.Albumart = new BitmapImage(new Uri("ms-appx:///Assets/Logo.scale-240.png"));
data.Add(obj);
}
}
catch
{
}
finally
{
}
}
public string Name { get; set; }
public ImageSource Albumart { get; set; }
}
}
演示代码可以从这里下载。
我同意评论者Clint Good的建议。您的CreateSampleData()
方法似乎负责初始化ContactModel
实例的集合。据推测,您是从 Data
属性调用此属性的,因为这些是要用于初始化data
字段的项。
也就是说,您有一个严重的问题:C# 属性不支持 async
/await
模式。解决此问题的一个习惯用法是异步初始化支持字段,然后在初始化完成后引发属性更改事件。遗憾的是,ContactsViewModel
类不实现INotifyPropertyChanged
或继承DependencyObject
(基于 XAML 的程序处理属性更改通知的两种方式)。
将所有这些放在一起并对代码进行必要的更改,您最终会得到这样的东西:
联系人视图模型:
public class ContactsViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
private IList data;
public IList Data
{
get
{
if (data == null)
{
// Ignore the returned Task...we're not going to do anything with it
var _ = InitData();
}
// Caller should handle null. If not, need to return "new Data[0]" here instead
return data;
}
}
private async Task InitData()
{
// Catch _specific_ exceptions here, if necessary
var items = await ContactModel.CreateSampleData();
data = items.ToAlphaGroups(x => x.Name);
OnPropertyChanged("Data");
}
private CollectionViewSource collection;
public CollectionViewSource Collection
{
get
{
if (collection == null)
{
collection = new CollectionViewSource();
collection.Source = Data;
collection.IsSourceGrouped = true;
}
return collection;
}
}
}
联系人型号:
public class ContactModel
{
// constructor
public ContactModel()
{
Name = "name";
Albumart = new BitmapImage(new Uri("ms-appx:///Assets/Logo.scale-240.png"));
}
public async static Task<ObservableCollection<ContactModel>> CreateSampleData()
{
ObservableCollection<ContactModel> data = new ObservableCollection<ContactModel>();
IReadOnlyList<IStorageItem> MusicLibrary = await KnownFolders.MusicLibrary.GetFoldersAsync(CommonFolderQuery.GroupByAlbum);
foreach (IStorageItem item in MusicLibrary)
{
ContactModel obj = new ContactModel();
IStorageItem musicItem = item;
obj.Name = musicItem.Name;
obj.Albumart = new BitmapImage(new Uri("ms-appx:///Assets/Logo.scale-240.png"));
data.Add(obj);
}
return data;
}
public string Name { get; set; }
public ImageSource Albumart { get; set; }
}
请注意,我也完全删除了您的尝试/捕获。简单地吞下例外是非常糟糕的。如果您认为需要处理任何异常,请在调用方中处理它们(即 ContactsViewModel.InitData()
),您可以做一些有用的事情并在必要时通知用户(但只处理特定的异常,您知道如何响应的异常)。