Windows 8 C # Call windows runtime components written in C ++

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.