C# 非高速应用程序中的StorageFile异步使用
我正在尝试在类库中创建StorageFile的实例C# 非高速应用程序中的StorageFile异步使用,c#,windows-8,windows-runtime,isolatedstorage,async-await,C#,Windows 8,Windows Runtime,Isolatedstorage,Async Await,我正在尝试在类库中创建StorageFile的实例 var localFolder = ApplicationData.Current.LocalFolder; StorageFile destinationFile = await localFolder.CreateFileAsync(destination, CreationCollisionOption.GenerateUniqueName); < VS11不是构建说:“Acess”要求类型“Windows .Fase.IasycCur
var localFolder = ApplicationData.Current.LocalFolder;
StorageFile destinationFile = await localFolder.CreateFileAsync(destination, CreationCollisionOption.GenerateUniqueName);
< VS11不是构建说:“Acess”要求类型“Windows .Fase.IasycCurress”有一个合适的GETAuthItter方法。您是否缺少“系统”的using指令
显然,我正在使用.NET4.5作为目标,并且我正在引用Windows程序集。。。
不确定为什么此代码在MetroStyle中工作,但不在类库中生成。。。如何在类库中创建Storagefile的实例???在这个阶段,文件是否以异步方式创建并不重要
请让我知道你的想法。。。
Stelio看起来您正在尝试使用WinRT库中的类型,因为
StorageFile
声明它仅适用于Metro,并且它位于Windows.Storage
中
介绍了如何构建它,但它似乎是一个手动过程。它还详细说明了错误的原因:
使用await关键字会导致编译器查找GetAwaiter
方法。因为IAsyncOperation没有
定义一个GetAwaiter方法,编译器希望查找
扩展方法
基本上,您需要添加对以下内容的引用:System.Runtime.WindowsRuntime.dll
请花点时间阅读他的博客文章,但为了清楚起见,我将把重要部分放在这里
下面的博客内容被剽窃 首先,在记事本中,我在EnumDevices.cs中创建了以下C#源代码:
using System;
using System.Threading.Tasks;
using Windows.Devices.Enumeration;
using Windows.Foundation;
class App {
static void Main() {
EnumDevices().Wait();
}
private static async Task EnumDevices() {
// To call DeviceInformation.FindAllAsync:
// Reference Windows.Devices.Enumeration.winmd when building
// Add the "using Windows.Devices.Enumeration;" directive (as shown above)
foreach (DeviceInformation di in await DeviceInformation.FindAllAsync()) {
Console.WriteLine(di.Name);
}
}
}
其次,我创建了一个Build.bat文件,我从开发人员命令提示符运行该文件来构建此代码(这应该是1行,但为了便于阅读,我将其包装在这里):
然后,在命令提示下,我只需运行EnumDevices.exe来查看输出。自从我发出post命令以来,肯定已经有很长时间了 添加引用后:
System.Runtime.WindowsRuntime.dll
System.Threading.Tasks.dll 并在项目文件中以windows 8为目标:
<PropertyGroup>
<TargetPlatformVersion>8.0</TargetPlatformVersion>
</PropertyGroup>
8
上面提到的例子可以在VS中编译。对我来说,有效的方法是“手动”添加TargetPlatformVersion
<PropertyGroup>
<TargetPlatformVersion>8.0</TargetPlatformVersion>
</PropertyGroup>
8
并在项目组中添加以下内容
<ItemGroup>
<Reference Include="System.Runtime.WindowsRuntime" />
<Reference Include="System.Runtime" />
<Reference Include="Windows" />
</ItemGroup>
然后项目应该正常编译。我也遇到了同样的问题。问题是名称空间标题中缺少系统名称空间。我只是在名称空间中包含了系统,它工作了。希望有帮助。在Metro样式中,代码文件顶部是否有额外的using语句?我想应该指出Jeffrey Richter metiones在他的帖子中所说的:缺少的程序集是System.Runtime.WindowsRuntime.dll.Guys,感谢您的及时回复。。。我试图使用Windows 8上WPF应用程序的后台下载程序(在非Metro上下文中),但我想这不是一个好主意,最好使用BITS。。。。但是我认为使用WINDOWSRunTIME使用互操作不是一个好主意……当我试图从博客中运行这个例子时,我会遇到以下错误:<代码> NoMutoS.cs(14,35):错误CS4028:“Acess”要求类型“Windows .Fase.IasycCurress”有一个合适的GeaAuthItter方法。“System”是否缺少using指令?@Hjulle文件顶部是否有using语句<代码>使用系统代码>。另外,您是否在相关C#版本上运行了足够晚的VS版本,以获得
wait
关键字支持?是的,我完全复制了示例。我假设它有await
关键字支持,因为它正在寻找GetAwaiter
方法,而不是抱怨未知关键字或其他东西。但是我不运行VS,我使用的是.NET 4.5运行时附带的csc
编译器。如果您想问一些问题,请另外提问。
<ItemGroup>
<Reference Include="System.Runtime.WindowsRuntime" />
<Reference Include="System.Runtime" />
<Reference Include="Windows" />
</ItemGroup>