我正在开发WP 8.1中的音乐播放器应用程序。我正在尝试实现跳转列表功能。 为了实现跳转列表,我正在关注给定here的示例。
我正在对可用的代码进行一些必要的更改。这是我如何实现所需功能,因为我在C#,MVVM& WP 8.1很小。
但我在ViewModel的第
行遇到错误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; }
}
}
演示代码可以从here下载。
答案 0 :(得分:2)
我同意评论者Clint Good的建议。您的CreateSampleData()
方法似乎负责初始化ContactModel
个实例的集合。据推测,您是从Data
属性调用此属性,因为这些是您要用于初始化data
字段的项目。
也就是说,你有一个严重的问题:C#属性不支持async
/ await
模式。解决此问题的一个习惯用法是异步初始化支持字段,然后在初始化完成时引发属性更改事件。不幸的是,您的ContactsViewModel
类没有实现INotifyPropertyChanged
或继承DependencyObject
(基于XAML的程序处理属性更改通知的两种方式)。
将所有这些结合起来并对代码进行必要的更改,您最终会得到类似的结果:
<强> ContactsViewModel:强>
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;
}
}
}
<强> ContactModel:强>
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; }
}
请注意,我还完全删除了您的try / catch。简单地吞下异常是非常糟糕的。如果您认为需要处理任何异常,请在调用者处理它们(即ContactsViewModel.InitData()
),您可以在其中执行有用的操作并在必要时通知用户(但只处理特定的异常,您知道如何响应)