C++ Coder

HCP高性能计算架构,实现,编译器指令优化,算法优化, LLVM CLANG OpenCL CUDA OpenACC C++AMP OpenMP MPI

C++博客 首页 新随笔 联系 聚合 管理
  98 Posts :: 0 Stories :: 0 Comments :: 0 Trackbacks
http://www.cnblogs.com/linzheng/archive/2012/07/07/2580848.html

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

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

创建一个Windows运行时组件:

编写如下的代码:

#include "pch.h"
#include 
"WinRTComponent.h"

using namespace CppWinRTComponentDll2;

int CalculatorSample::Add(int x, int y)
{
    
return x+y;
}

头文件

#pragma once

using namespace Windows::Foundation;

namespace CppWinRTComponentDll2
{

    
public ref class CalculatorSample sealed
    
{
    
public:
        
int Add(int x, int y);

    }
;
}

 

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

添加Windows运行时组件

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="调用WinRT方法来相加" HorizontalAlignment="Center" Click="Button_Click_1" ></Button>
            
<TextBox x:Name="txtAddResult" HorizontalAlignment="Center" Height="45" Width="258"/>
        
</StackPanel>
    
</Grid>
</Page>

C#代码部分

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;//引入Windows运行时的空间

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();//直接创建Windows运行时里面的对象,很方便
                 int x = Convert.ToInt32(txtX.Text.ToString());
                 
int y = Convert.ToInt32(txtY.Text.ToString());
                 txtAddResult.Text 
= calcobj.Add(x,y).ToString();//调用Windows运行时里面的方法
            }

        }

    }

}

 

运行的效果


 


posted on 2012-10-29 21:48 jackdong 阅读(462) 评论(0)  编辑 收藏 引用 所属分类: Windows RT

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理