Using the Windows Runtime from C# and Visual Basic 11/7/2018 9:19 AM TOOL-531T Using the Windows Runtime from C# and Visual Basic Harry Pierson Windows Runtime Experience Jesse Kaplan Common Language Runtime .NET Framework © 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
Windows has always provided compelling capabilities for developers to build upon Windows has not always made it straightforward for you to use these capabilities from C# or VB
The C# code you have to write today… [DllImport("avicap32.dll", EntryPoint="capCreateCaptureWindow")] static extern int capCreateCaptureWindow( string lpszWindowName, int dwStyle, int X, int Y, int nWidth, int nHeight, int hwndParent, int nID); [DllImport("avicap32.dll")] static extern bool capGetDriverDescription( int wDriverIndex, [MarshalAs(UnmanagedType.LPTStr)] ref string lpszName, int cbName, [MarshalAs(UnmanagedType.LPTStr)] ref string lpszVer, int cbVer); // more and more of the same © 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
Manually Generated Interop Code Traditional Windows API Your Managed Code Manually Generated Interop Code Traditional Windows API
The C# code you get to write on Windows 8 using Windows.Media.Capture; var ui = new CameraCaptureUI(); ui.PhotoSettings.CroppedAspectRatio = new Size(4, 3); var file = await ui.CaptureFileAsync(CameraCaptureUIMode.Photo); if (file != null) { var bitmap = new BitmapImage() ; bitmap.SetSource(await file.OpenAsync(FileAccessMode.Read)); Photo.Source = bitmap; } © 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
Windows Runtime Architecture Metro style app Language Support (CLR, WinJS, CRT) Language Projection Windows Metadata & Namespace UI Pickers Controls Media Web Host (HTML, CSS, JavaScript)) XAML Storage Network … Windows Runtime Core Runtime Broker Windows Core
Your Managed Code Windows Runtime Windows 8 API
You already have the skills to build Metro style apps with C# and VB
Agenda The relationship between .NET and the Windows Runtime Using Windows Runtime APIs from C# and Visual Basic Building Window Runtime Components in C# and Visual Basic
C# and Visual Basic influenced the Windows Runtime
Windows Runtime is designed to be used from object-oriented languages like C# and Visual Basic
Windows Runtime metadata files use an updated version of Windows Runtime metadata files use an updated version of .NET’s metadata format
Viewing Windows Metadata Files with ILDASM 11/7/2018 9:19 AM demo Viewing Windows Metadata Files with ILDASM © 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
Windows Runtime includes a XAML based framework for Metro style apps
Many of the Windows Runtime design guidelines came from the Many of the Windows Runtime design guidelines came from the .NET design guidelines
Windows Runtime was expressly designed to work well with C# and Visual Basic
Using the Windows Runtime feels natural and familiar from C# and Visual Basic
Using the Windows Runtime from C# 11/7/2018 9:19 AM demo Using the Windows Runtime from C# © 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
Most WinRT types map directly to C#/VB var ui = new CameraCaptureUI(); ui.PhotoSettings.CroppedAspectRatio = new Size(4, 3); var file = await ui.CaptureFileAsync(CameraCaptureUIMode.Photo); if (file != null) { var bitmap = new BitmapImage() ; bitmap.SetSource(await file.OpenAsync(FileAccessMode.Read)); Photo.Source = bitmap; } © 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
Most WinRT types map directly to C#/VB var dtm = DataTransferManager.GetForCurrentView(); dtm.DataRequested += new TypedEventHandler<DataTransferManager, DataRequestedEventArgs>(dtm_DataRequested); void dtm_DataRequested(DataTransferManager sender, DataRequestedEventArgs args) { if (_stream == null) args.Request.FailWithDisplayText("No Image Taken"); else args.Request.Data.SetBitmap(_stream); } © 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
Primitives (strings, numbers, etc) Almost everything maps directly between the Windows Runtime and .NET Primitives (strings, numbers, etc) Classes Constructors Interfaces Static Members Enums Methods Structs Properties Delegates Events
Most differences between Windows Runtime and Most differences between Windows Runtime and .NET are hidden from the managed developer
Exceptions from HRESULTs try { var ui = new CameraCaptureUI(); ui.PhotoSettings.CroppedAspectRatio = new Size(16, 9); } catch (Exception e) //Exception handling code ComException © 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
Calling WinRT Async Operation from C# var picker = new FileOpenPicker(); picker.FileTypeFilter.Add("*"); var files = await picker.PickMultipleFilesAsync(); foreach (var file in files) { lbFiles.Items.Add(file.FileName); } PickMultipleFilesOperation PickMultipleFilesAsync(); public sealed class PickMultipleFilesOperation : IAsyncOperation<IReadOnlyList<StorageFile>>, IAsyncInfo © 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
Accessing a WinRT Collection from C# var picker = new FileOpenPicker(); picker.FileTypeFilter.Add("*"); var files = await picker.PickMultipleFilesAsync(); foreach (StorageFile file in files) { lbFiles.Items.Add(file.FileName); } System.Collections.Generic.IReadOnlyList <Windows.Storage.StorageFile> © 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
Accessing a WinRT Collection from C# var picker = new FileOpenPicker(); picker.FileTypeFilter.Add("*"); var files = await picker.PickMultipleFilesAsync(); foreach (StorageFile file in files) { lbFiles.Items.Add(file.FileName); } Windows.Foundation.Collections.IVectorView <Windows.Storage.StorageFile> System.Collections.Generic.IReadOnlyList <Windows.Storage.StorageFile> © 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
IIterable<T> ↔ IEnumerable<T> .NET automatically maps collection interfaces to their Windows Runtime equivalent IIterable<T> ↔ IEnumerable<T> IVector<T> ↔ IList<T> IVectorView<T> ↔ IReadOnlyList<T> IMap<K,V> ↔ IDictionary<K,V> IMapView<K,V> ↔ IReadOnlyDictionary<K,V>
A few places use extension methods to bridge between Windows Runtime and managed code
Streams Code Sample FileOpenPicker picker = new FileOpenPicker(); picker.FileTypeFilter.Add("*"); StorageFile file = await picker.PickSingleFileAsync(); Windows.Storage.Streams.IInputStream inputStream = await file.OpenForReadAsync(); System.IO.Stream stream = inputStream.AsStream(); System.IO.StreamReader reader = new StreamReader(stream); string contents = reader.ReadToEnd(); © 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
IBuffer Code Sample internal async void ProtectBytes(byte[] data, BinaryWriter output) { DataProtectionProvider dpp = new DataProtectionProvider(); IBuffer result = await dpp.ProtectAsync(data.AsBuffer()); byte[] protectedData; int start = 0; if (result.TryGetUnderlyingData(out protectedData, out start)) output.Write(protectedData); else throw new InvalidOperationException(); }
You can write your own Windows Runtime components in C# or Visual Basic
You should build a Windows Runtime component when you want your code to be used from JS, C++, C# and VB
Traditional COM in Managed Code [ComVisible(true), Guid("06D7901C-9045-4241-B8A0-39A1AC0F8618")] public interface IWindowsApiSample { string HelloWorld(); } [ComVisible(true), [ClassInterface(ClassInterfaceType.None)] [ComDefaultInterface(typeof(IWindowsApiSample))] public class WindowsApiSample : IWindowsApiSample { public string HelloWorld() { return "Hello, World!"; } } © 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
Windows Runtime in managed code Sample public sealed class MyClassLibrary { public string HelloWorld() { return "Hello, World!"; } } © 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
By adhering to a few simple rules, you can build a managed Windows Runtime component that projects into C++ or JavaScript
Only the public types and members in your managed Windows Runtime component project need to follow the simple rules
API signatures must only use Windows Runtime types Structs can only have public data fields Inheritance can only be used for XAML controls, all other types must be sealed Only supports system provided generic types
Authoring Collection Code public sealed class ManagedLibrary { public List<int> GetNumbers() List<int> numbers = new List<int>(); numbers.AddRange(new int[] {0,1,1,2,3}); return numbers; } © 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
Authoring Collection Code public sealed class ManagedLibrary { public IList<int> GetNumbers() List<int> numbers = new List<int>(); numbers.AddRange(new int[] {0,1,1,2,3}); return numbers; } © 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
Authoring Managed Async Method private async Task<string> GetTweetTaskAsync() { string url = "http://api.twitter.com/1/statuses/" + "user_timeline.xml?screen_name=bldwin"; var http = new HttpClient(); var resp = await http.GetAsync(url); var xml = XDocument.Load(resp.Content.ContentReadStream); var str = xml.Descendants("text") .Select(x => x.Value).First(); return str; } © 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
Authoring Windows Runtime Async Method public IAsyncOperation<string> GetTweetAsync() { return AsyncInfoFactory.Create( () => this.GetTweetTaskAsync()); } © 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
Visual Studio has built-in support for managed Windows Runtime component projects
Building Windows Runtime Components in C# 11/7/2018 9:19 AM demo Building Windows Runtime Components in C# © 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
You already have the skills to build Metro style apps with C# and VB
Build your own managed Windows Runtime components Influenced by C# and VB Feels natural and familiar from C# and Visual Basic Build your own managed Windows Runtime components
For more information Related sessions Documentation & articles Contact PLAT-874T: Lap around the Windows Runtime TOOL-930C: A .NET developer's view of Windows 8 app development TOOL-810T: Async made simple in Windows 8, with C# and Visual Basic APP-737T: Metro style apps using XAML: what you need to know TOOL-532T: Using the Windows Runtime from C++ TOOL-533T: Using the Windows Runtime from JavaScript PLAT-875T: Windows Runtime internals: understanding "Hello World" PLAT-876T: Lessons learned designing the Windows Runtime Tour of the IDE for C#, C++ and Visual Basic Developers Building your first Windows Metro style app with C++, C# or Visual Basic Roadmap for creating Metro style apps in C#, C++ and Visual Basic Contact Please visit the forums on the Windows Dev Center at http://forums.dev.windows.com We’ll be active on the Building Metro style Apps with C# or VB forum For best response, please include the Session ID in the title of your post This talk’s Session ID is TOOL-531T
thank you Feedback and questions http://forums.dev.windows.com Session feedback http://bldw.in/SessionFeedback
11/7/2018 9:19 AM © 2011 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION. © 2011 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.