WinRT Holy COw
-
Upload
eugene-zharkov -
Category
Technology
-
view
1.485 -
download
2
description
Transcript of WinRT Holy COw
www.buildwindows.com
WinRT Holy Cow
Eugene ZharkovSilverlight MVP
www.buildwindows.com
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
[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
The C# code you have to write today…
www.buildwindows.com
Traditional Windows API
Your Managed Code
Manually Generated
Interop Code
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;}
www.buildwindows.com
Windows Runtime Architecture
Windows Metadata & Namespace
Language Projection
Windows Core
Windows Runtime Core
XAML Storage …Network
UI Pickers MediaControls
Metro style app
Runtime Broker
Language Support (CLR, WinJS, CRT)
Web Host (HTML, CSS, JavaScript))
www.buildwindows.com
Your Managed Code
Windows Runtime
Windows 8 API
www.buildwindows.com
You already have the skills to build Metro style apps
with C# and VB
www.buildwindows.com
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 influencedthe Windows Runtime
www.buildwindows.com
Windows Runtime is designed to be used from object-oriented
languages like C# and Visual Basic
www.buildwindows.com
Windows Runtime metadata files use an updated version of .NET’s
metadata format
www.buildwindows.com
Viewing Windows Metadata Files
demo
www.buildwindows.com
Windows Runtime includes a XAML based framework for Metro style
apps
www.buildwindows.com
Many of the Windows Runtime design guidelines
came from the .NET design guidelines
www.buildwindows.com
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
www.buildwindows.com
Using the Windows Runtime from C#
demo
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;}
www.buildwindows.com
Primitives(strings, numbers, etc)
Interfaces
Enums
Structs
Static Members
Properties
EventsDelegates
Classes
Constructors
Methods
Almost everything maps directly between
the Windows Runtime and .NET
www.buildwindows.com
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
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
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>
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>
Windows.Foundation.Collections.IVectorView <Windows.Storage.StorageFile>
www.buildwindows.com
IIterable<T> ↔ IEnumerable<T>
IVector<T> ↔ IList<T>
IVectorView<T> ↔ IReadOnlyList<T>
IMap<K,V> ↔ IDictionary<K,V>
IMapView<K,V> ↔ IReadOnlyDictionary<K,V>
.NET automatically maps collection interfaces to their Windows
Runtime equivalent
www.buildwindows.com
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();
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
www.buildwindows.com
You should build a Windows Runtime component when you want
your code to be used fromJS, 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!"; }}
Windows Runtime in managed code Sample
public sealed class MyClassLibrary{ public string HelloWorld() { return "Hello, World!"; }}
www.buildwindows.com
By adhering to a few simple rules, you can build
a managed Windows Runtime component
that projects into C++ or JavaScript
www.buildwindows.com
Only the public types and members
in your managed Windows Runtime component project need to follow
the simple rules
www.buildwindows.com
API signatures must only use Windows Runtime types
Inheritance can only be used for XAML controls,
all other types must be sealedOnly supports system provided generic types
Structs can only have public data fields
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; }}
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; }}
Authoring Collection Code
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;}
Authoring Managed Async Method
Authoring Windows Runtime Async Method
public IAsyncOperation<string> GetTweetAsync(){ return AsyncInfoFactory.Create( () => this.GetTweetTaskAsync());}
www.buildwindows.com
Visual Studio has built-in support for managed
Windows Runtime component projects
www.buildwindows.com
Building Windows Runtime Components in C#
demo
www.buildwindows.com
You already have the skills to build Metro style apps
with C# and VB
Influenced by C# and VB
Feels natural and familiar from C# and Visual Basic
Build your own managed Windows Runtime components