gpt4 book ai didi

c# - 无法在WP 8.1中将void分配给隐式类型的局部变量

转载 作者:行者123 更新时间:2023-12-03 10:27:58 24 4
gpt4 key购买 nike

我正在开发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下载。

最佳答案

我同意评论员克林特·古德的建议。您的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;
}
}
}

联系人模型:
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())中处理它们,在该处您可以做一些有用的事情,并在必要时通知用户(但仅处理特定的异常,您知道如何响应)。

关于c# - 无法在WP 8.1中将void分配给隐式类型的局部变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28060335/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com