当前正在使用 Visual Studio 开发普通的 C# Tizen NUI 应用程序。它很有帮助,因为它有现成的模板。没有稍微修改一下。看起来像这样:
//Main.cs
using System;
using Tizen.NUI;
using Tizen.NUI.BaseComponents;
namespace TizenDotNetApp
{
class Program : NUIApplication
{
protected override void OnCreate()
{
base.OnCreate();
Initialize();
}
void Initialize()
{
Window.Instance.KeyEvent += OnKeyEvent;
TextLabel text = new TextLabel("Hello Tizen NUI World");
text.HorizontalAlignment = HorizontalAlignment.Center;
text.VerticalAlignment = VerticalAlignment.Center;
text.TextColor = Color.Blue;
text.PointSize = 12.0f;
text.HeightResizePolicy = ResizePolicyType.FillToParent;
text.WidthResizePolicy = ResizePolicyType.FillToParent;
Window.Instance.GetDefaultLayer().Add(text);
Animation animation = new Animation(2000);
animation.AnimateTo(text, "Orientation", new Rotation(new Radian(new Degree(180.0f)), PositionAxis.X), 0, 500);
animation.AnimateTo(text, "Orientation", new Rotation(new Radian(new Degree(0.0f)), PositionAxis.X), 500, 1000);
animation.Looping = true;
animation.Play();
}
public void OnKeyEvent(object sender, Window.KeyEventArgs e)
{
if (e.Key.State == Key.StateType.Down && (e.Key.KeyPressedName == "XF86Back" || e.Key.KeyPressedName == "Escape"))
{
Exit();
}
}
static void Main(string[] args)
{
var app = new Program();
app.Run(args);
}
}
}
通过电视模拟器运行它,它可以工作。然而,我的目标是让它调用一些 C++ 函数。为此,我有另一个程序也使用 Visual Studio 生成 .dll。
我只需要这个头文件:
//Header.h
#pragma once
#ifdef CPPPROGRAM_EXPORTS
#define CPPPROGRAM_API _declspec(dllexport)
#else
#define CPPPROGRAM_API _declspec(dllimport)
#endif
// Simple greeting function
extern "C" CPPPROGRAM_API void message();
// Adds two numbers
extern "C" CPPPROGRAM_API int add(int a, int b);
// Says hello to passed parameter
extern "C" CPPPROGRAM_API void greet(char* name);
它链接到实现逻辑的实际 .cpp 文件:
#include "pch.h"
#include "Header.h"
#include <stdio.h>
// Testing basic printf function
void message() {
printf("Hello, from C++!\n");
}
// Testing basic parameter passing
int add(int a, int b) {
return a + b;
}
// Testing slightly complicated array passing (rip structs and objects)
void greet(char* name) {
printf("Hello, %s!", name);
}
运行并构建它,会生成一个 .dll 文件:
...\TizenDotNetApp\x64\Debug\CppLibrary.dll
然后我将其添加到我的 Tizen 应用程序的解决方案中,并确保其属性为“始终复制”。然后,我修改了 Tizen .NET 应用程序以实际调用 .dll:
using System;
using Tizen.NUI;
using Tizen.NUI.BaseComponents;
using System.Runtime.InteropServices;
namespace TizenDotNetApp
{
class Program : NUIApplication
{
[DllImport("CppLibrary.dll")]
static extern int add(int a, int b); // <-- called the function
protected override void OnCreate()
{
base.OnCreate();
Initialize();
}
void Initialize()
{
Window.Instance.KeyEvent += OnKeyEvent;
int result = add(5, 8); // <-- simple adding here
TextLabel text = new TextLabel("Hello Tizen NUI World");
text.HorizontalAlignment = HorizontalAlignment.Center;
text.VerticalAlignment = VerticalAlignment.Center;
text.TextColor = Color.Blue;
text.PointSize = 12.0f;
text.HeightResizePolicy = ResizePolicyType.FillToParent;
text.WidthResizePolicy = ResizePolicyType.FillToParent;
Window.Instance.GetDefaultLayer().Add(text);
Animation animation = new Animation(2000);
animation.AnimateTo(text, "Orientation", new Rotation(new Radian(new Degree(180.0f)), PositionAxis.X), 0, 500);
animation.AnimateTo(text, "Orientation", new Rotation(new Radian(new Degree(0.0f)), PositionAxis.X), 500, 1000);
animation.Looping = true;
animation.Play();
}
public void OnKeyEvent(object sender, Window.KeyEventArgs e)
{
if (e.Key.State == Key.StateType.Down && (e.Key.KeyPressedName == "XF86Back" || e.Key.KeyPressedName == "Escape"))
{
Exit();
}
}
static void Main(string[] args)
{
var app = new Program();
app.Run(args);
}
}
}
然而,结果却很不幸:
忽略 .dll 名称。我一直在更改和修改它,看看是否有任何效果,但没有。输出警告始终保持不变:尽管已经被引用,但它以某种方式无法找到 .dll 文件。
但是,当我尝试一个简单的 C# 控制台应用程序时,结果有所不同;它能够识别.dll文件并在C#中完美地调用C++函数。
找到答案(感谢@swift-kim)。编译 .DLL 的电视、模拟器和 PC 的架构不同。
事实上,电视和模拟器使用的是 Linux 形式,因此您实际上需要 .so (Linux) 文件,而不是 .dll (Windows) 文件。
不要使用
g++
或 CMake 正常编译它(这将使输出 .so/.dll 基于您自己机器的体系结构),而是使用 g++-arm-linux-gnueabi
(电视的 .so 编译器)或 g++-arm-linux-gnueabi
生成 .so 的命令应如下所示:
arm-linux-gnueabi-g++ -fPIC -shared calculate.cpp -o libcalculate.so
(是的,由于某种原因该命令末尾有 g++)
您的输出(-o)是libacalculate.so,您要转换的原始文件是calculate.cpp。只要您确保添加现有文件的正常属性正确(“始终复制”属性打开等),这将适用于电视