Skip to end of metadata
Go to start of metadata

Introduction

This article shows how to integrate Windows Runtime components that's built in C#, with Titanium module. This content is valid as of Titanium SDK 6.1.0.

Create simple Windows Runtime component in C#

This section we are going to create a basic Windows Runtime Component in C# that exposes few methods. For basic information about Windows Runtime Components in C#, refer to official document from Microsoft here: Creating Windows Runtime Components in C# and Visual Basic.

  1. In Visual Studio, choose File, New, Project. In the Installed Templates section of the New Project dialog box, choose Visual C#, Windows Universal. Then choose the Windows Runtime Component (Universal Windows) and enter MyComponent for the project name.
  2. In Solution Explorer, rename the Class1.cs to MyComponent.cs that is the class basically what we want to export.
  3. Then, changes the name of the namespace to MyComponent, and change the name of the class to Speaker.

MyComponent.cs

Then, add one static method, one instance method and property for instance. That would be look like below.

MyComponent.cs


After updating your Windows Runtime component, select solution in the Solution Explorer, right-click and choose Build Solution. When build is succeeded, you get MyComponent.winmd that is built under the directory in your project. You may see the location of the built binary in the Visual Studio's Output view.

Call Windows Runtime component from Titanium module

After successfully building your Windows Runtime component describe above, you should have MyComponent.winmd under the Debug folder. Then we want to write a Titanium Module that uses your component.

Create new Titanium module

Copy MyComponent.winmd into windows/lib folder of your module project

Copy your Windows Runtime component files into windows/lib folder of your Titanium module project. As of Titanium 6.1.0, Titanium CLI will look into the folder and generates Visual Studio project that has links to the components automatically.

Modify Titanium module to use MyComponent

ComExampleTest.hpp

Then your Titanium modules cpp will look like:

ComExampleTest.cpp

For more examples of using the module API, see working Titanium modules under github/appcelerator/titanium_mobile_windows/Source

C++ built-in types and Windows Runtime types

You might wonder why we can't pass JSValue as string directly to MyComponent->Name. That is because only Windows Runtime types can be passed across ABI boundary. For basic information about type system in Windows Runtime refer to Type System (C++/CX). To pass string across ABI boundary you need to convert JSValue to std::string and then convert to Platform::String^ (C++). There are some utility functions to convert it in TitaniumWindows::Utility namespace. Let say we have argument variable that is passed as JSValue like below, we can cast JSValue to std::string, and then convert it to Platform::String^ using TitaniumWindows::Utility::ConvertUTF8String. Note that we need to treat strings in JSValue as UTF8 because it consists of UTF8 strings.

Build module with --run-cmake option

In order to re-create Visual Studio project files so it can use your component, we need to specify --run-cmake option.


Then Titanium CLI will generate module zip that contains your Windows Runtime Component.

Next steps

  • No labels