Download presentation
Presentation is loading. Please wait.
Published byArnold Miles Modified over 6 years ago
1
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.
2
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
3
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.
4
Manually Generated Interop Code Traditional Windows API
Your Managed Code Manually Generated Interop Code Traditional Windows API
5
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.
6
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
7
Your Managed Code Windows Runtime Windows 8 API
8
You already have the skills to build Metro style apps with C# and VB
9
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
10
C# and Visual Basic influenced the Windows Runtime
11
Windows Runtime is designed to be used from object-oriented languages like C# and Visual Basic
12
Windows Runtime metadata files use an updated version of
Windows Runtime metadata files use an updated version of .NET’s metadata format
13
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.
14
Windows Runtime includes a XAML based framework for Metro style apps
15
Many of the Windows Runtime design guidelines came from the
Many of the Windows Runtime design guidelines came from the .NET design guidelines
16
Windows Runtime was expressly designed to work well with C# and Visual Basic
17
Using the Windows Runtime feels natural and familiar from C# and Visual Basic
18
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.
19
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.
20
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.
21
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
22
Most differences between Windows Runtime and
Most differences between Windows Runtime and .NET are hidden from the managed developer
23
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.
24
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.
25
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.
26
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.
27
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>
28
A few places use extension methods to bridge between Windows Runtime and managed code
29
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.
30
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(); }
31
You can write your own Windows Runtime components in C# or Visual Basic
32
You should build a Windows Runtime component when you want your code to be used from JS, C++, C# and VB
33
Traditional COM in Managed Code
[ComVisible(true), Guid("06D7901C 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.
34
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.
35
By adhering to a few simple rules, you can build a managed Windows Runtime component that projects into C++ or JavaScript
36
Only the public types and members in your managed Windows Runtime component project need to follow the simple rules
37
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
38
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.
39
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.
40
Authoring Managed Async Method
private async Task<string> GetTweetTaskAsync() { string url = " "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.
41
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.
42
Visual Studio has built-in support for managed Windows Runtime component projects
43
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.
44
You already have the skills to build Metro style apps with C# and VB
45
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
46
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 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
47
thank you Feedback and questions http://forums.dev.windows.com
Session feedback
48
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.