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

运行的效果

 

 

 

 


本文转自linzheng 51CTO博客,原文链接:http://blog.51cto.com/linzheng/1081671

相关文章
|
2月前
|
算法 关系型数据库 测试技术
WHQL微软驱动签名方案,让驱动程序在Windows系统流畅运行
WHQL认证(Windows徽标认证)是微软设立的严格测试标准,旨在确保驱动程序的兼容性、稳定性和互通性。本文介绍了三种WHQL微软驱动签名方案:单系统签名、多系统签名和硬件兼容性测试方案,分别满足不同开发商的需求。通过WHQL认证,不仅能消除Windows安装警告,提升用户体验,还能获得“Designed for Windows”徽标授权,入列全球Windows Catalog及HCL产品表,提升品牌权威性和采购优先权。此外,访问微软OCA可获取错误反馈,助力产品质量改进。选择合适的签名方案,让驱动在Windows系统中流畅运行!
|
4月前
|
监控 搜索推荐 开发工具
2025年1月9日更新Windows操作系统个人使用-禁用掉一下一些不必要的服务-关闭占用资源的进程-禁用服务提升系统运行速度-让电脑不再卡顿-优雅草央千澈-长期更新
2025年1月9日更新Windows操作系统个人使用-禁用掉一下一些不必要的服务-关闭占用资源的进程-禁用服务提升系统运行速度-让电脑不再卡顿-优雅草央千澈-长期更新
294 2
2025年1月9日更新Windows操作系统个人使用-禁用掉一下一些不必要的服务-关闭占用资源的进程-禁用服务提升系统运行速度-让电脑不再卡顿-优雅草央千澈-长期更新
|
4月前
|
人工智能 数据处理 C#
AI Dev Gallery:微软开源 Windows AI 模型本地运行工具包和示例库,助理开发者快速集成 AI 功能
微软推出的AI Dev Gallery,为Windows开发者提供开源AI工具包和示例库,支持本地运行AI模型,提升开发效率。
223 13
|
5月前
|
Linux C# iOS开发
开源GTKSystem.Windows.Forms框架让C# Winform支持跨平台运行
开源GTKSystem.Windows.Forms框架让C# Winform支持跨平台运行
108 12
|
6月前
|
数据库连接 数据库 C#
Windows下C# 通过ADO.NET方式连接南大通用GBase 8s数据库(上)
Windows下C# 通过ADO.NET方式连接南大通用GBase 8s数据库(上)
|
6月前
|
数据库连接 数据库 C#
Windows下C# 通过ADO.NET方式连接南大通用GBase 8s数据库(下)
本文接续前文,深入讲解了在Windows环境下使用C#和ADO.NET操作南大通用GBase 8s数据库的方法。通过Visual Studio 2022创建项目,添加GBase 8s的DLL引用,并提供了详细的C#代码示例,涵盖数据库连接、表的创建与修改、数据的增删查改等操作,旨在帮助开发者提高数据库管理效率。
|
7月前
|
应用服务中间件 Shell PHP
windows系统配置nginx环境运行pbootcms访问首页直接404的问题
windows系统配置nginx环境运行pbootcms访问首页直接404的问题
|
7月前
|
Java Windows
如何在windows上运行jar包/JAR文件 如何在cmd上运行 jar包 保姆级教程 超详细
本文提供了一个详细的教程,解释了如何在Windows操作系统的命令提示符(cmd)中运行JAR文件。
2309 1
|
8月前
|
Windows Python
python获取windows机子上运行的程序名称
python获取windows机子上运行的程序名称
|
8月前
|
小程序 Windows
MASM32编写的程序在Windows 7,10下运行正常,但在Win XP下运行时只闻其声不见其形的故障
MASM32编写的程序在Windows 7,10下运行正常,但在Win XP下运行时只闻其声不见其形的故障

热门文章

最新文章