一、WinRT中使用 winrt::init_apartment() 完成windows运行时的初始化,功效等同于调用RoInitialize加上COM初始化
二、头文件,C++/CX中的头文件前面加上winrt/
例如 C++/CX中
#include <Windows.Foundation.h>
#include <Windows.Devices.Bluetooth.h>
#include <Windows.Devices.Bluetooth.Advertisement.h>
变成
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Devices.Bluetooth.h>
#include <winrt/Windows.Devices.Bluetooth.Advertisement.h>
三、命名空间,C++/CX中的命名空间前面加上winrt.
例如
using namespace Windows;
using namespace Windows::Devices;
using namespace Windows::Devices::Bluetooth;
改为
using namespace winrt::Windows;
using namespace winrt::Windows::Devices;
using namespace winrt::Windows::Devices::Bluetooth;
四、对象引用。
C++/CX中一般使用 ^指针来保存对象,一般使用 ref new 来生成对象。而C++/WinRT中使用对象名字的结构体来保存,而且对象直接使用构造函数构造,不需要new
例如
Bluetooth::Advertisement::BluetoothLEAdvertisementWatcher^ scanner_ =
ref new Bluetooth::Advertisement::BluetoothLEAdvertisementWatcher();
改为
Bluetooth::Advertisement::BluetoothLEAdvertisementWatcher scanner_ =
Bluetooth::Advertisement::BluetoothLEAdvertisementWatcher();
五、访问对象的Property。
从->访问,改为.访问。从直接访问property,变成函数指针,如果传参就是设置值,如果不传参就是获取值。
auto id = record->XboxUserId;
auto state = record->UserState;
record->UserState = newValue;
改为
auto id = record.XboxUserId();
auto state = record.UserState();
record.UserState(newValue);
参考
https://learn.microsoft.com/en-us/windows/uwp/cpp-and-winrt-apis/move-to-winrt-from-cx
网友评论