`

[win8 开发学习笔记 ]关于编程模式和C++/CX语言

 
阅读更多

根据微软官方文档总结的,原文都是英文,理解有误望指正!

windows8 两种编程方式

1.使用C++/CX

2.使用WRL + C++

public ref class 可以用来在wrt组件中传递,而标准c++则不能

The Visual C++ component extensions (C++/CX) supports user-defined reference classes and structs, and user-defined value classes and structs. Public forms of these types can be passed between Windows Runtime components and apps, whereas standard C++ classes and structs cannot.

ref class 可以与 std C++ 混编

You can freely mix standard C++ types and functions in a ref class. However, between Windows Runtime components, you can pass only public ref classes, ref structs, value structs, and value classes.

ref class 通过引用计数实现垃圾回收

Reference types are implemented as reference-counted pointers to Windows Runtime objects. C++/CX automatically modifies the reference count (ref count) of an object when it is instantiated, copied, set to null, or goes out of scope. When the reference count becomes zero, the object’s destructor is immediately invoked just as in any C++ program. There is no separate garbage collection mechanism in C++/CX

public ref class 的定义形式,一般是为了达到能够将这个对象在其他5种语言中使用的目的。vc++提供隐含转型功能,所以开发的典型模式是ref class内部使用std c++代码编写,然后通过转型到公共接口上。

You typically use these extensions in the public interfaces where your code is passing Windows Runtime types back and forth across the ABI to JavaScript, C#, or Visual Basic (or even another C++ module). Visual C++ provides a variety of implicit conversions between Windows Runtime types and standard C++ types. The typical pattern is to use standard C++ types and libraries internally as usual, and convert to Windows Runtime types in the public interfaces.

Only Windows Runtime types can be passed across the ABI boundary.

Only Windows Runtime types can be passed across the ABI boundary. The compiler will raise an error if the component has a type such as std::wstring as a return type or parameter in a public method. If you use the C++ built-in types such as int, double, and so on, the compiler automatically converts them to the appropriate Windows Runtime type—int32, float64, and so on—in parameters and return types of public methods. No such conversion occurs unless you are passing the type across the ABI.For more information, see Visual C++ language reference.

Activatable class 也称作 ref class.一个wrt组件里必须要有一个activatable class.

An activatable class (also known as a ref class) is one that can be instantiated from another language such as JavaScript. To be consumable from another language such as JavaScript, a component must contain at least one activatable class. An activatable class must inherit directly from Platform::Object. For more information, see Type System (C++/CX).

An activatable class must be declared as * public ref class sealed *.

An activatable class must be declared as public ref class sealed. The ref class keywords tell the compiler to create the class as a Windows Runtime compatible type, and the sealed keyword specifies that the class cannot be inherited. A class must be sealed to be consumed by JavaScript.

基础数据类型可以通过隐含转换(implicit conversion)到Windows Runtime Type

    C++
    // Implicit conversion of return value to equivalent Windows Runtime type.
    double LogCalc(double input)
    {
        // Use C++ standard library as usual.
        return std::log(input); 
    }
    JavaScript
    //Call a method
    var num = nativeObject.logCalc(21.5);
    document.getElementById('callmethod').innerHTML = num;

用户自定义的数据结构,可以通过封装成 value struct 或者 继承IPropertySet来传递

    C++
    // Custom struct
    public value struct Batter
    {
        String^ Name;
        int Number;
        double BattingAverage;
    };

    Public ref class LangSample
    {
    private:
        MyData batter_;
    public:
        property Batter 
        {
            Batter get(){ return batter_; }
        }
    }
    JavaScript
    var myData = nativeObject.batter;
        document.getElementById('myDataResult').innerHTML = myData.name + " , " + myData.number + " , " + myData.battingAverage.toPrecision(3);

Delegates and events 更像是function objects

A delegate is a Windows Runtime type that represents a function object. You can use delegates in connection with events, callbacks, and asynchronous method calls to specify an action to be performed later. Like a function object, the delegate provides type-safety by enabling the compiler to verify the return type and parameter types of the function. The declaration of a delegate resembles a function signature, the implementation resembles a class definition, and the invocation resembles a function invocation. A delegate instance can also be created "inline" by using a lambda expression.

    C++
    namespace mycomp
    {
        public delegate void PropertyChangedHandler(Object^ source, int i);

        public ref class sealed LangSample
        {
            event PropertyChangedHandler^ propertyChangedEvent;        
            // Property that has custom setter/getter
            property int PropertyA
            {
                int get() { return _propertyAValue; }
                void set(int propertyAValue) 
                {
                    if (propertyAValue != _propertyAValue)
                    {
                        _propertyAValue = propertyAValue;
                        propertyChangedEvent(this, propertyAValue);
                    }
                }
            }    
        };
    }
    JavaScript
    // Define an event handler method
    var singlecasthandler = function (ev) {
        document.getElementById('singlecastresult').innerHTML = ev;
        };

    // Subscribe to the event
    nativeObject.onpropertychangedevent = singlecasthandler;

    // Set the value of the property and fire the event
    nativeObject.propertyA = 2 * propValue;
分享到:
评论

相关推荐

    C语言和C++Builder学习笔记.rar_C++笔记_c++学习笔记_c/C++_学习笔记_编程语言

    C语言和C++是两种广泛使用的编程语言,它们在软件开发领域占据着重要地位。C语言以其简洁、高效和底层操作能力著称,而C++则在C的基础上引入了面向对象编程,极大地扩展了其应用范围。 C语言作为基础,它的知识点...

    C/C++/Linux语言学习基础笔记

    在"C/C++/Linux语言学习基础笔记"这个资源中,我们可以深入探讨三个核心主题:C++编程语言、C编程语言以及Linux操作系统。这些笔记对于初学者或者希望在嵌入式系统领域深化理解的人来说是非常宝贵的。 首先,让我们...

    c++学习笔记精华版

    通过以上内容的学习,我们可以看到 C++ 作为一种强大的编程语言,不仅具备高效的性能,而且拥有丰富的特性和工具来支持复杂的应用程序开发。对于初学者来说,理解 C++ 的基本概念和技术是非常重要的第一步。

    【C++学习笔记】一份详细的学习笔记,让你轻松掌握C++编程!

    【C++学习笔记】这份详尽的资源是为那些希望深入了解C++编程语言的人们精心准备的。C++是一种强大的、通用的编程语言,它的设计理念是面向对象,同时支持过程化编程,使得它在系统软件、应用软件、游戏开发、设备...

    win32编程学习笔记

    这个“win32编程学习笔记”可能包含了关于如何构建、理解和调试Win32应用程序的基础知识和高级概念。 在Win32编程中,开发者需要了解以下关键知识点: 1. **Windows API**:这是Win32编程的核心,提供了大量的函数...

    嵌入式开发学习笔记( java - c/c++ :从入门到入门 )

    标题中提到了“嵌入式开发学习笔记”,这意味着笔记内容涉及了嵌入式系统的开发过程,主要使用了Java和C/C++语言。这种学习笔记对于初学者来说是非常有价值的,因为它能够帮助他们理解如何从零基础开始学习嵌入式...

    C++ 学习笔记 整理

    C++是一种强大的、通用的编程语言,被广泛应用于系统软件、应用软件、游戏开发、设备驱动、嵌入式系统等领域。这份"C++学习笔记"涵盖了C++的基础到高级概念,旨在帮助初学者和有一定经验的程序员深入理解并掌握C++。...

    C++学习笔记本

    C++学习笔记C++学习笔记C++学习笔记C++学习笔记C++学习笔记

    C++服务器开发精髓笔记

    C++服务器开发精髓笔记 本笔记涵盖了C++服务器开发的精髓知识点,从RAII到Pimpl、C++11新特性、统一的...本笔记涵盖了C++服务器开发的多方面知识点,旨在帮助开发者更好地理解和应用C++语言,提高开发效率和代码质量。

    《C++编程思想》学习笔记

    《C++编程思想》的学习笔记主要涵盖了对象的创建与使用,同时也涉及了语言的翻译过程,包括解释器和编译器的工作原理。本章节详细介绍了C++编程的基础知识,包括编译过程、静态类型检查、分段编译工具的使用,以及...

    C++学习笔记.doc

    C++是一种强大的、面向对象的编程语言,广泛应用于系统软件、游戏开发、嵌入式系统以及许多高性能应用。学习C++不仅仅是掌握语法,更重要的是理解编程的核心概念,以便更好地应用这种语言解决问题。 **一、何谓编程...

    Visual C++/MFC学习笔记.doc

    《Visual C++/MFC学习笔记》是一份深入探讨C++和Microsoft Foundation Class (MFC)库的文档,旨在帮助读者掌握使用Visual C++开发Windows应用程序的技能。这份笔记主要分为六个章节,覆盖了从入门到高级的多个主题。...

    C++编程学习笔记学习

    C++编程学习笔记学习

    C/C++开发实战笔记

    《C/C++开发实战笔记》是一本专注于C/C++编程实践和学习心得的资料,旨在帮助初学者和有经验的开发者巩固基础知识,提升实战技能。作者通过自身的学习历程和编程经验,提炼出了一系列的关键知识点,涵盖了C/C++语言...

    c++学习笔记.pdf

    C++是一种静态类型、编译式、通用的编程语言,它支持过程化编程、面向对象编程和泛型编程。C++广泛应用于软件开发领域,包括操作系统、游戏开发、实时物理模拟器等。以下是从提供的文件中提取的知识点: 1. 关于C和...

    《精通Oracle 10g Pro*C/C++编程》源代码与学习笔记

    总之,《精通Oracle 10g Pro*C/C++编程》的源代码和学习笔记为读者提供了一个全面学习和实践Oracle数据库C/C++编程的宝贵资源。通过深入研究这些内容,开发者可以更好地利用Oracle的强大功能,提升数据库应用的性能...

    Visual C++程序设计学习笔记.rar

    Visual C++是一款由微软开发的集成开发环境(IDE),它专为使用C++语言进行Windows应用程序开发而设计。本学习笔记主要围绕Visual C++的各个方面展开,包括基础语法、MFC(Microsoft Foundation Classes)框架、GUI...

    软件工程师学习笔记大全(C++,JAVA)4.rar

    这份名为“软件工程师学习笔记大全(C++,JAVA)4.rar”的压缩文件包含了软件工程师在学习C++和Java编程语言时的重要参考资料。这些文档详细涵盖了从基础到高级的各种主题,对于提升技能和准备面试非常有帮助。以下...

Global site tag (gtag.js) - Google Analytics