Windows运行时组件是Windows 8里面通用的公共库,它可以使用C++,C#或者VB来编写,不过你的Windows 8 metro是用什么语言编写都可以调用无缝地调用Windows运行时组件。

下面通过一个C#编写的Windows 8项目来调用一个用C++编写的Windows运行时组件。

创建一个Windows运行时组件:

 

 

 

编写如下的代码:

 

 
  
  1. #include "pch.h"  
  2. #include "WinRTComponent.h"  
  3.  
  4. using namespace CppWinRTComponentDll2;  
  5.  
  6. int CalculatorSample::Add(int x, int y)  
  7. {  
  8.     return x+y;  

头文件

 

 
  
  1. #pragma once  
  2.  
  3. using namespace Windows::Foundation;  
  4.  
  5. namespace CppWinRTComponentDll2  
  6. {  
  7.  
  8.     public ref class CalculatorSample sealed  
  9.     {  
  10.     public:  
  11.         int Add(int x, int y);  
  12.  
  13.     };  

在C#编写的项目中调用Windows运行时组件的C++方法

添加Windows运行时组件

 

 

 

UI部分

 

 
  
  1. <Page 
  2.     x:Class="TestWinRTCSDemo.MainPage" 
  3.     IsTabStop="false" 
  4.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  5.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  6.     xmlns:local="using:TestWinRTCSDemo" 
  7.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  8.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  9.     mc:Ignorable="d"> 
  10.  
  11.     <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> 
  12.         <StackPanel> 
  13.             <TextBox x:Name="txtX" HorizontalAlignment="Center" Height="45" Width="258"></TextBox> 
  14.             <TextBlock   Text="+" HorizontalAlignment="Center"  Height="45" Width="258" FontSize="14" FontWeight="Bold"/> 
  15.             <TextBox x:Name="txtY" HorizontalAlignment="Center" Height="45" Width="258"></TextBox> 
  16.             <Button Content="调用WinRT方法来相加" HorizontalAlignment="Center" Click="Button_Click_1" ></Button> 
  17.             <TextBox x:Name="txtAddResult" HorizontalAlignment="Center" Height="45" Width="258"/> 
  18.         </StackPanel> 
  19.     </Grid> 
  20. </Page> 

C#代码部分

 

 
  
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.IO;  
  4. using System.Linq;  
  5. using Windows.Foundation;  
  6. using Windows.Foundation.Collections;  
  7. using Windows.UI.Xaml;  
  8. using Windows.UI.Xaml.Controls;  
  9. using Windows.UI.Xaml.Controls.Primitives;  
  10. using Windows.UI.Xaml.Data;  
  11. using Windows.UI.Xaml.Input;  
  12. using Windows.UI.Xaml.Media;  
  13. using Windows.UI.Xaml.Navigation;  
  14. using CppWinRTComponentDll2;//引入Windows运行时的空间  
  15.  
  16. namespace TestWinRTCSDemo  
  17. {  
  18.  
  19.     public sealed partial class MainPage : Page  
  20.     {  
  21.         public MainPage()  
  22.         {  
  23.             this.InitializeComponent();  
  24.         }  
  25.  
  26.         protected override void OnNavigatedTo(NavigationEventArgs e)  
  27.         {  
  28.         }  
  29.  
  30.         private void Button_Click_1(object sender, RoutedEventArgs e)  
  31.         {  
  32.             if (txtX.Text != "" && txtY.Text != "")  
  33.             {  
  34.                  CalculatorSample calcobj =  new  CalculatorSample();//直接创建Windows运行时里面的对象,很方便  
  35.                  int x = Convert.ToInt32(txtX.Text.ToString());  
  36.                  int y = Convert.ToInt32(txtY.Text.ToString());  
  37.                  txtAddResult.Text = calcobj.Add(x,y).ToString();//调用Windows运行时里面的方法  
  38.             }  
  39.         }  
  40.     }  

运行的效果