StorageFile Async usage in a NON Metro application
Asked Answered
L

4

8

I am trying to create an instance of StorageFile in my class library...

var localFolder = ApplicationData.Current.LocalFolder;
StorageFile destinationFile = await localFolder.CreateFileAsync(destination, CreationCollisionOption.GenerateUniqueName);

VS11 is not building say: 'await' requires that the type 'Windows.Foundation.IAsyncOperation' have a suitable GetAwaiter method. Are you missing a using directive for 'System'?

obviously I am using .net 4.5 as target and I am referencing Windows Assemblies... not sure why this code work in MetroStyle but does not build in a class library... How can I create an instance of Storagefile in a class library??? at this stage it is not important if the file is created in an async way...

pls let me know your thoughts... Stelio

Legislature answered 3/5, 2012 at 9:18 Comment(1)
In the Metro style, are there any extra using statements at the top of the code file?Draftee
O
10

What worked for me is to "manually" add the TargetPlatformVersion

<PropertyGroup>
  <TargetPlatformVersion>8.0</TargetPlatformVersion>
</PropertyGroup>

and in Item group add the following

<ItemGroup>
  <Reference Include="System.Runtime.WindowsRuntime" />
  <Reference Include="System.Runtime" />
  <Reference Include="Windows" />
</ItemGroup>

Then the project should compile normaly.

Oyster answered 1/11, 2013 at 17:17 Comment(0)
C
8

I had the same issue.The problem was system namespace is missing in the namespace header.I just included system in the namespace and it worked. hope it helps.

Currish answered 25/4, 2014 at 10:35 Comment(0)
D
5

Looks like you are trying to use a type from the WinRT libraries because the StorageFile class documentation states it applies to Metro only and it is found in Windows.Storage.

This blog post goes through how to build it, but it appears to be a manual process. It also details the cause of the error:

Using the await keyword causes the compiler to look for a GetAwaiter method on this interface. Since IAsyncOperation does not define a GetAwaiter method, the compiler wants to look for an extension method.

Basically, it looks like you need to add a reference to: System.Runtime.WindowsRuntime.dll


Please take the time to read his blog post, but I will put the important part here for clarity.


Blog Content Below Unceremoniously Plagiarised

First, in Notepad, I created the following C# source code in EnumDevices.cs:

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);
        }
    }
}

Second, I created a Build.bat file which I run from the Developer Command Prompt to build this code (This should be 1 line but I wrap it here for read ability):

csc EnumDevices.cs  
/r:c:\Windows\System32\WinMetadata\Windows.Devices.Enumeration.winmd  
/r:c:\Windows\System32\WinMetadata\Windows.Foundation.winmd 
/r:System.Runtime.WindowsRuntime.dll  
/r:System.Threading.Tasks.dll

Then, at the command prompt, I just run the EnumDevices.exe to see the output.

Draftee answered 3/5, 2012 at 9:30 Comment(5)
I think it should be pointed out what Jeffrey Richter metiones in his post: the missing assembly is System.Runtime.WindowsRuntime.dll.Thermograph
Guys, thanks for the prompt response... I was trying to use the background downloader from a WPF application on Windows 8 (in a NON-Metro context) but I guess it is not a good idea and it is better to use BITS.... but I think that using the interop with the windowsRuntime is not a good idea...Legislature
I get the following error when trying to run the example from the blog: numDevices.cs(14,35): error CS4028: 'await' requires that the type 'Windows.Foundation.IAsyncOperation<Windows.Devices.Enumeration.DeviceIn formationCollection>' have a suitable GetAwaiter method. Are you missing a using directive for 'System'?Tansy
@Tansy Do you have a using statement at the top of the file? using System;. Also, are you running a late enough version of VS on the relevant C# version to get the await keyword support?Draftee
Yes, I copied the example exactly. I assume it has await keyword support since it is looking for a GetAwaiter method and not complaining about unknown keyword or something. But I do not run VS, I'm using the csc compiler that comes with the .NET 4.5 runtime.Tansy
H
1

It must be a long time since the post when I After adding references:
System.Runtime.WindowsRuntime.dll
System.Threading.Tasks.dll

and targeting windows 8 in project file:

  <PropertyGroup>
    <TargetPlatformVersion>8.0</TargetPlatformVersion>
  </PropertyGroup>

Mentioned above example can be compiled in VS.

Highjack answered 8/8, 2013 at 14:14 Comment(1)
please post a separate question if you want to ask something.Wring

© 2022 - 2024 — McMap. All rights reserved.