Windows 8 C#調用C++編寫的Windows運行時組件

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.     }  

運行的效果

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章