Windows runtime components are common public libraries in Windows 8, which can be written in C ++, C #, or VB, however, you can use any language to compile Windows 8 Metro to call the Windows runtime components seamlessly.
The following uses a Windows 8 project written in C # To call a Windows runtime component written in C ++.
Create a Windows runtime component:
Write the followingCode:
# Include"Pch.h"# Include"Winrtcomponent. h"Using NamespaceCppwinrtcomponentdll2;IntCalculatorsample: add (IntX,IntY ){ReturnX +Y ;}
Header file
# PragmaOnceUsing NamespaceWindows: Foundation;NamespaceCppwinrtcomponentdll2 {Public Ref ClassCalculatorsampleSealed{Public:IntAdd (IntX,IntY );};}
Call the C ++ method of the Windows runtime component in a project written in C #
Add windows runtime Components
UI
< Page X: Class = "Testwinrtcsdemo. mainpage" Istabstop = "False" Xmlns = "Http://schemas.microsoft.com/winfx/2006/xaml/presentation" Xmlns: x = "Http://schemas.microsoft.com/winfx/2006/xaml" Xmlns: Local = "Using: testwinrtcsdemo" Xmlns: d = "Http://schemas.microsoft.com/expression/blend/2008" Xmlns: MC = "Http://schemas.openxmlformats.org/markup-compatibility/2006" MC: ignorable = "D" > < Grid Background =" {Staticresource applicationpagebackgroundthemebrush} " > < Stackpanel > < Textbox X: Name = "Txtx" Horizontalalignment = "Center" Height = "45" Width = "258" > </ Textbox > < Textblock Text = "+" Horizontalalignment = "Center" Height = "45" Width = "258" Fontsize = "14" Fontweight = "Bold" /> < Textbox X: Name = "Txty" Horizontalalignment = "Center" Height = "45" Width = "258" > </ Textbox > < Button Content = "Call the winrt method to add" Horizontalalignment = "Center" Click = "Button_click_1" > </ Button > < Textbox X: Name = "Txtaddresult" Horizontalalignment = "Center" Height = "45" Width = "258" /> </ Stackpanel > </ Grid > </ Page >
C # code
Using System; Using System. Collections. Generic; Using System. IO; Using System. LINQ; Using Windows. Foundation; Using Windows. Foundation. collections; Using Windows. UI. XAML; Using Windows. UI. XAML. controls; Using Windows. UI. XAML. Controls. primitives; Using Windows. UI. XAML. Data; Using Windows. UI. XAML. input; Using Windows. UI. XAML. Media; Using Windows. UI. XAML. Navigation; Using Cppwinrtcomponentdll2; // introduce the Windows runtime Space Namespace Testwinrtcsdemo { Public Sealed Partial Class Mainpage: Page { Public Mainpage (){ This . Initializecomponent ();} Protected Override Void Onnavigatedto (navigationeventargs e ){} Private Void Button_click_1 ( Object Sender, routedeventargs e ){ If (Txtx. Text! = "" & Txty. Text! = "" ) {Calculatorsample calcobj = New Calculatorsample (); // directly create objects in Windows runtime, which is convenient Int X = Convert. toint32 (txtx. Text. tostring ()); Int Y = Convert. toint32 (txty. Text. tostring (); txtaddresult. Text = Calcobj. Add (x, y). tostring (); // call the method in Windows runtime }}}}
Running Effect