StorageFile Async usage in a NON Metro application

前端 未结 4 540
庸人自扰
庸人自扰 2020-12-16 23:24

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

var localFolder = ApplicationData.Current.LocalFolder;
StorageFile destinationFile =          


        
相关标签:
4条回答
  • 2020-12-16 23:38

    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.

    0 讨论(0)
  • 2020-12-16 23:49

    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.

    0 讨论(0)
  • 2020-12-16 23:55

    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.

    0 讨论(0)
  • 2020-12-17 00:01

    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.

    0 讨论(0)
提交回复
热议问题