none
any sample of using WinRT API in Desktop App?

Answers

  • The Visual Studio 11 Express which installs with the Windows Developer Preview supports only Metro style apps, but the Visual Studio 11 Developer Preview supports desktop apps.

    You should be able to find System.Runtime.WindowsRuntime.dll in the GAL.

    --Rob

    • Marked as answer by Kavin Huang Friday, December 16, 2011 1:00 AM
    Thursday, December 15, 2011 5:35 PM
    Owner

All replies

  • There aren't any samples on this, but there isn't a lot of trickiness to it.  You will need to reference the winmd namespaces that you want to use from %windows%\system32\winmetadata and System.Runtime.WindowsRuntime.dll and then call the WinRT classes as you would from a Metro style app.

    There currently isn't any documentation about which WinRT classes can be called successfully from a desktop app, but anything that doesn't depend on the Metro environment should work.

    --Rob

    Thursday, December 15, 2011 6:44 AM
    Owner
  • Hi Rob

    Thank you for your answer.  I use VS2008 since VS 11 supports Metro only. When I try to "Add Reference" of  a .winmd file, I get a error message "Please make sure the file is accessible, and that it is a valid assembly or COM component". And the System.Runtime.WindowsRuntime.dll can't be viewed in the object browser.

     

    Regards

    Thursday, December 15, 2011 9:20 AM
  • The Visual Studio 11 Express which installs with the Windows Developer Preview supports only Metro style apps, but the Visual Studio 11 Developer Preview supports desktop apps.

    You should be able to find System.Runtime.WindowsRuntime.dll in the GAL.

    --Rob

    • Marked as answer by Kavin Huang Friday, December 16, 2011 1:00 AM
    Thursday, December 15, 2011 5:35 PM
    Owner
  • OK thanks a lot
    Friday, December 16, 2011 1:12 AM
  • Here is a sample of using WinRT API in a Desktop App.  I'll try to post in in a blog article later with more details, but this should be enough to get you going.

    Prerequisites: I'm using the "Windows 8 Developer Preview with developer tools 64-bit" which gave me the VS11 Express with Metro Class library.  Then I installed VS11 Developer Preview full, which gave me desktop templates. Note: my first Dev Preview image I built with the 32 bit image which did not come with the developer tools.  I then downloaded the VS11 full and had issues getting references to WinMD libraries to bind properly.  So I rebuilt my image starting with the "with developer tools" ISO and all is good now.

    Step 1: I started with a Desktop App.  I used a C# Console app, but you could presumably use any of them (WinForms, WPF, etc...). 

    Step 2: I added a C# Windows Metro style class library (which gave me access to the Windows.* API's, AKA WinRT)

    Step 3: I added a reference from the Console app to the Metro style class library

    Step 4: I had to add  a reference to System.Runtime.dll (not System.Runtime.WindowsRuntime) by finding it here:

    C:\Windows\Microsoft.NET\Framework64\v4.0.30319

    or here:

    C:\Windows\Microsoft.NET\Framework\v4.0.30319 (this also works)

    Step 5: I wrote a few methods in the class library referencing WinRT.  I tried the light sensor, which returned null, and storage which threw an exception because it was missing the correct context of a WinRT app.  I finally found a WinRT method that worked:

    Windows.Networking.Connectivity.NetworkInformation.GetHostNames()

    Step 6: I iterated through the hostname objects and passed the results back to the console app as a string.  The console app does not have access to the WinRT API.

    Note: I got a warning "The project 'ClassLibrary1' cannot be referenced." but it worked anyway.

    Here is a snippet from the Desktop assembly:

    using ClassLibrary1;

    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Class1 c1 = new Class1();
                float lightSensorReading = c1.GetSensorReading();  //returns -1 (light sensor is null)
                string localData = c1.GetWinRtLocalHostNames();  // returns a list of local hostnames
                Console.WriteLine(localData);
            }
        }
    }

    Here is a snippet from the Metro style class library:

            public string GetWinRtLocalHostNames()
            {
                StringBuilder builder = new StringBuilder();
                var list = Windows.Networking.Connectivity.NetworkInformation.GetHostNames();
                builder.AppendLine("hostnames: ");
                int hostNameCount = 0;
                foreach (Windows.Networking.HostName hostName in list)
                {
                    builder.AppendFormat("{0}:\t", ++hostNameCount);
                    builder.AppendLine(hostName.DisplayName);
                }
                return builder.ToString();
            }

    Saturday, December 17, 2011 4:42 PM
  • For what it's worth -- this does not work anymore. Visual Studio 2012 RC won't let you add the project-to-project reference from your ConsoleApplication1 to the metro style class library.
    Friday, June 08, 2012 12:52 AM