从c ++ / cx移植到c ++ / winrt时,实现Windows运行时接口以供本地使用时出错

问题描述

更新

基于Ryan Shepherd的回复,我将代码更改为:

class DerivedClass : winrt::implements<DerivedClass,BaseClass> {
public:
DerivedClass(winrt::FrameworkElement& control) : 
5===>implements_type(control) {}

static winrt::com_ptr<DerivedClass> from(winrt::FrameworkElement control) { 
    control ? control.Tag().try_as<SvgCanvasController>() : nullptr;
}
}

现在我得到一个错误

没有构造函数实例“ winrt :: implements :: implements [with D = DerivedClass,I = BaseClass]”与参数列表匹配的参数类型是:(winrt :: Windows :: UI :: Xaml :: FrameworkElement)。

我确实定义了BaseClass构造函数,该构造函数以&FrameworkElement作为参数。因此,我不确定会收到什么错误

原始

我在C ++ / CX中有这个功能

ref class DerivedClass sealed : public BaseClass {
public:
explicit DerivedClass(Windows::UI::Xaml::FrameworkElement^ &control)
    : BaseClass(control) {}

static DerivedClass from(Windows::UI::Xaml::FrameworkElement^ control) {
    return control ? dynamic_cast<DerivedClass>(control->Tag) : nullptr;
}   
}

当我尝试将其转换为c ++ / winrt

class DerivedClass : winrt::implements<DerivedClass,BaseClass> {
public:
1===>DerivedClass(winrt::FrameworkElement& control) : BaseClass(control) 
2===>{
}

static DerivedClass from(winrt::FrameworkElement control) { 
    if (control)
 3===>       return control.Tag().try_as<SvgCanvasController>();
    else
 4===>       return nullptr;
}

我想在这里讨论和理解一些问题。如上所述,我收到以下错误

1

BaseClass不是DerivedClass类的非静态数据成员或基类

2

winrt :: implements 认构造函数无法引用-它是一个删除函数

3

没有合适的用户定义的从winrt :: impl :: com_ref 到DerivedClass的转换

4

没有合适的构造函数来将std :: nullptr_t转换为DerivedClass

希望对这些错误有一些想法。

我了解我创建了一个DerivedClass对象

auto obj = winrt::make<DerivedClass>();

问题:

  1. 如何正确编写构造函数
  2. 如何像在C ++ / CX中一样将参数传递给基类构造函数
  3. 如何处理from()函数定义中的错误

解决方法

关于第一个错误,DerivedClass不继承自BaseClass,它继承自winrt::implements<DerivedClass,BaseClass>,而BaseClass则继承自winrt::implements。孙子类不能直接初始化其祖父母-大多数经历其直接基类型。幸运的是,C ++ / WinRT在#include <winrt/Windows.Foundation.h> #include <winrt/Windows.UI.Xaml.h> using namespace winrt; using namespace winrt::Windows::Foundation; using namespace winrt::Windows::UI::Xaml; struct BaseType : implements<BaseType,IInspectable> { explicit BaseType(FrameworkElement const&) {} }; struct DerivedType : implements<DerivedType,BaseType> { explicit DerivedType(FrameworkElement const& arg) : implements_type(arg) {} }; int main() { make<DerivedType>(FrameworkElement{ nullptr }); } 中提供了一个帮助程序类型,因此您不必重复所有这些模板参数。因此,您的构造函数应如下所示:

winrt::make

此代码为我成功编译。

其他三个错误是由于您试图直接在堆栈上返回实现类型而引起的。这些对象是引用计数的,并且必须在堆上分配,由于调用DerivedClass,您似乎理解了这些对象。但是等式的其余部分是,您需要返回winrt::com_ptr<DerivedClass>而不是返回static winrt::com_ptr<Derived> from(winrt::FrameworkElement const& arg) { return arg ? arg.Tag().try_as<DerivedClass>() : nullptr; } ,结果如下:

server {
    listen 80;
    server_name martinschildrenacademy.com www.martinschildrenacademy.com;
    return 301 https://$host$request_uri;
    location = /favicon.ico {
            log_not_found off;
            access_log off;
    }

    location = /robots.txt {
            allow all;
            log_not_found off;
            access_log off;
    }

    location / {
            # This is cool because no php is touched for static content.
            # include the "?$args" part so non-default permalinks doesn't break when using query string
            try_files $uri $uri/ /index.php?$args;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
            expires max;
            log_not_found off;
         }
    }

    # Default server configuration
    #
    server {
    #       listen 80 default_server;
    #       listen [::]:80 default_server;
    listen 443 ssl;
    server_name martinschildrenacademy.com www.martinschildrenacademy.com;

    ssl_certificate /etc/letsencrypt/live/martinschildrenacademy.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/martinschildrenacademy.com/privkey.pem;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';

    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
# Note: You should disable gzip for SSL traffic.
    # See: https://bugs.debian.org/773332
    #
    # Read up on ssl_ciphers to ensure a secure configuration.
    # See: https://bugs.debian.org/765782
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;

    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.php  index.html index.htm index.nginx-debian.html;

    server_name martinschildrenacademy.com;

    # pass PHP scripts to FastCGI server
    #
    location ~ \.php$ {
            include snippets/fastcgi-php.conf;#
    #       # With php-fpm (or other unix sockets):
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    #       # With php-cgi (or other tcp sockets):
    #       fastcgi_pass 127.0.0.1:9000;
    }

    # deny access to .htaccess files,if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
            deny all;
    }
  }