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

如何解决从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;
    }
  }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-