building mixed-language apps
DESCRIPTION
Building mixed-language apps. Alan Ludwig Senior Software Development Engineer 4-100. The Windows Runtime makes it possible for you to write great apps using more than one language. The Windows Runtime (WinRT). APIs. Cross-language i nfrastructure. A version of Windows. - PowerPoint PPT PresentationTRANSCRIPT
Building mixed-language appsAlan LudwigSenior Software Development Engineer4-100
The Windows Runtime makes it possible for you to write great apps using more than one language
The Windows Runtime (WinRT)
APIs A versionof Windows
Cross-language
infrastructure
What’s the best language foryour Windows 8 apps?The language you already knowThe language your component is already written inThe language that has access to the features you needThe language that provides the right performanceThe language of your choice
How do I do that?
When you write a Windows Runtime component,you can consume it in JavaScript, C#/VB, or C++
The same cross-language infrastructure that was used to create thousands of Windows Runtime APIs is available for you to write your own Windows Runtime APIs
Demo
Hybrid JavaScript and C++ Sample
Only where neededKeep it simpleAsync everywhere Mind your threads
How do I make it great?
Only where needed
Indirect function callsType conversion
Examples of type conversion
WinRT Type C++ JavaScript C# (.NET)
HSTRING Platform::String var (string) StringGUID GUID var (string) GUIDUInt32 uint32 var (double) uint32DateTime (FILETIME) var (date) DateTimeOffset
Keep it simple
Designing good Windows Runtime interfaces is similarto good OO design
Async everywhere
Use the async pattern for long running operationsThe language projections make this easy
Windows::Foundation::IAsyncOperation<uint32>^ DemoLib::GetCountAsync(){
auto async = concurrency::create_async(// your lambda here[this](){ return _count;});return async;
}
Async operation in C++
public Windows.Foundation.IAsyncOperation<UInt32> GetCountAsync(){return (Windows.Foundation.IAsyncOperation<UInt32>)Task<UInt32>.Run(
() => {return _count;} );
}
Async operation in C#
Mind your threads
Writing great components requires some basic knowledge of threading issues
UI is always thread affine
ThreadingUI
Thr
ead
Wor
ker T
hrea
d
Callasync API
Create task
Other work Update UI Continue
Do workCall
completion
How do I get back to the main thread?
Techniques to get back to the main threadDispatching via the UI elementDispatching via the core windowProxies and stubs
if (output->Dispatcher->HasThreadAccess()){ output->Text += "Count Changed (thread access)! Count is " + newCount.ToString() + "\n";}
Dispatching via the UI element
else{ output->Dispatcher->RunAsync( Windows::UI::Core::CoreDispatcherPriority::Normal, ref new Windows::UI::Core::DispatchedHandler( [this, newCount]() -> void { output->Text += "Count Changed (no thread access)! Count is " + newCount.ToString() + "\n";}));}
Dispatching via the UI element
Windows::ApplicationModel::Core::CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(
Windows::UI::Core::CoreDispatcherPriority::Normal,ref new Windows::UI::Core::DispatchedHandler(
[this,value](){CountChanged(this, value);}
));
Dispatching via the core window
The Windows Runtime will automatically move your calls back to the right thread if proxies and stubs are available
event Windows::Foundation::EventHandler<uint32>^ CountChanged;
event Windows::Foundation::TypedEventHandler<IDemoLib^, uint32>^ CountChanged;
EventHandler vs. TypedEventHandler
Create proxies and stubs for yourWindows Runtime components
Demo
Creating a Windows Runtime in-process component sample (C++/CX)
The same cross-language infrastructure that was used to create thousands of Windows Runtime APIs is available for you to write your own Windows Runtime APIs
Only where neededKeep it simpleAsync everywhere Mind your threads
How do I make it great?
• 10/30/2012 11:45 – B33 Baker – Bringing existing C++ code to Windows Store apps
• 11/1/2012 12:00 – B33 Hood – Gaming reimagined: Gaming case studies
Related Sessions
A list of sessions will be sent one week prior to the event
• Hybrid JavaScriptand C++ sample
• Creating a Windows Runtime in-process component sample (C++/CX)
Resources
Please submit session evals by using the Build Windows 8 appor at http://aka.ms/BuildSessions
• Develop: http://msdn.microsoft.com/en-US/windows/apps/br229512
• Design: http://design.windows.com/
• Samples: http://code.msdn.microsoft.com/windowsapps/Windows-8-Modern-Style-App-Samples
• Videos: http://channel9.msdn.com/Windows
Resources
Please submit session evals by using the Build Windows 8 appor at http://aka.ms/BuildSessions
© 2012 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.