c++/winrt deviceinformation不存在错误

发布于 2025-01-21 03:33:23 字数 2664 浏览 1 评论 0原文

您好,我正在尝试从 ac# 程序移植以下代码。



 public class DeviceListEntry
 {
     private DeviceInformation device;
     private String deviceSelector;

     public String InstanceId
     {
         get
         {
             return device.Properties[DeviceProperties.DeviceInstanceId] as String;
         }
     }

     public DeviceInformation DeviceInformation
     {
         get
         {
             return device;
         }
     }

     public String DeviceSelector
     {
         get
         {
             return deviceSelector;
         }
     }

     /// <summary>
     /// The class is mainly used as a DeviceInformation wrapper so that the UI can bind to a list of these.
     /// </summary>
     /// <param name="deviceInformation"></param>
     /// <param name="deviceSelector">The AQS used to find this device</param>
     public DeviceListEntry(Windows.Devices.Enumeration.DeviceInformation deviceInformation, String deviceSelector)
     {
         device = deviceInformation;
         this.deviceSelector = deviceSelector;
     }

 }
}

本质上,如果我尝试将 DeviceListEntry 构造函数与 deviceInformation 对象进行移植,则会出现错误,提示 DeviceInformation 类没有默认构造函数。

但是,如果我删除设备对象的相应代码,DevuceListEntry 构造函数不会返回任何错误。

这就是我所拥有的:

#include "pch.h"
#include "DeviceListEntry.h"
#include "Constants.h"
using namespace winrt::Windows::Devices::Enumeration;
using namespace winrt::Windows::Foundation::Collections;

namespace SerialArduino
{

    const winrt::hstring DeviceProperties::DeviceInstanceID = {L"System.Devices.DeviceInstanceId"};


    // having a DeviceInformation object as parameter as well as object causes no default constructor error
    DeviceListEntry::DeviceListEntry(winrt::Windows::Devices::Enumeration::DeviceInformation deviceInformation, winrt::hstring deviceSelector)
    {
        device = deviceInformation;
        this->deviceSelector = deviceSelector;
    }

    winrt::hstring DeviceListEntry::InstanceId()
    {

        return winrt::unbox_value<winrt::hstring>(device.Properties().Lookup(DeviceProperties::DeviceInstanceID));
    
    }

    winrt::Windows::Devices::Enumeration::DeviceInformation DeviceListEntry::DeviceInformation()
    {
        return device;
    }

    winrt::hstring DeviceListEntry::DeviceSelector()
    {
        return deviceSelector;
    }

}

错误:

Error (active)  E0291   no default constructor exists for class 
Error   C2512   'winrt::Windows::Devices::Enumeration::DeviceInformation': no appropriate default constructor available SerialArduino

任何帮助将不胜感激,因为我完全迷失了我做错的事情。 提前致谢

Hello i'm trying to port the following code from a c# program.



 public class DeviceListEntry
 {
     private DeviceInformation device;
     private String deviceSelector;

     public String InstanceId
     {
         get
         {
             return device.Properties[DeviceProperties.DeviceInstanceId] as String;
         }
     }

     public DeviceInformation DeviceInformation
     {
         get
         {
             return device;
         }
     }

     public String DeviceSelector
     {
         get
         {
             return deviceSelector;
         }
     }

     /// <summary>
     /// The class is mainly used as a DeviceInformation wrapper so that the UI can bind to a list of these.
     /// </summary>
     /// <param name="deviceInformation"></param>
     /// <param name="deviceSelector">The AQS used to find this device</param>
     public DeviceListEntry(Windows.Devices.Enumeration.DeviceInformation deviceInformation, String deviceSelector)
     {
         device = deviceInformation;
         this.deviceSelector = deviceSelector;
     }

 }
}

Essentially if I attempt to port the DeviceListEntry Constructor with the deviceInformation object it errors me saying there is no default Constructor for the DeviceInformation Class.

however if I remove the corresponding code for the device object the DevuceListEntry constructor doesn't return any errors.

Here's what I have:

#include "pch.h"
#include "DeviceListEntry.h"
#include "Constants.h"
using namespace winrt::Windows::Devices::Enumeration;
using namespace winrt::Windows::Foundation::Collections;

namespace SerialArduino
{

    const winrt::hstring DeviceProperties::DeviceInstanceID = {L"System.Devices.DeviceInstanceId"};


    // having a DeviceInformation object as parameter as well as object causes no default constructor error
    DeviceListEntry::DeviceListEntry(winrt::Windows::Devices::Enumeration::DeviceInformation deviceInformation, winrt::hstring deviceSelector)
    {
        device = deviceInformation;
        this->deviceSelector = deviceSelector;
    }

    winrt::hstring DeviceListEntry::InstanceId()
    {

        return winrt::unbox_value<winrt::hstring>(device.Properties().Lookup(DeviceProperties::DeviceInstanceID));
    
    }

    winrt::Windows::Devices::Enumeration::DeviceInformation DeviceListEntry::DeviceInformation()
    {
        return device;
    }

    winrt::hstring DeviceListEntry::DeviceSelector()
    {
        return deviceSelector;
    }

}

Error:

Error (active)  E0291   no default constructor exists for class 
Error   C2512   'winrt::Windows::Devices::Enumeration::DeviceInformation': no appropriate default constructor available SerialArduino

Any help would be greatly appreciated as I'm completely lost on what I'm doing wrong.
Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寒尘 2025-01-28 03:33:23

假设您的 DeviceListEntry 声明如下所示:

namespace SerialArduino {

class DeviceListEntry {
public:
    //...

private:
    winrt::Windows::Devices::Enumeration::DeviceInformation device;
    winrt::hstring deviceSelector;
};

}

尝试像这样实现您的构造函数:

// having a DeviceInformation object as parameter as well as object causes no default constructor error
DeviceListEntry::DeviceListEntry(winrt::Windows::Devices::Enumeration::DeviceInformation deviceInformation, winrt::hstring deviceSelector_)
    : device(deviceInformation), deviceSelector(deviceSelector_)
{ }

这样,device 将从 deviceInformation 进行复制初始化。

否则,编译器将尝试默认构造 device,然后将 deviceInformation 复制分配给它。

Assuming your DeviceListEntry declaration looks like:

namespace SerialArduino {

class DeviceListEntry {
public:
    //...

private:
    winrt::Windows::Devices::Enumeration::DeviceInformation device;
    winrt::hstring deviceSelector;
};

}

Try implementing your constructor like this:

// having a DeviceInformation object as parameter as well as object causes no default constructor error
DeviceListEntry::DeviceListEntry(winrt::Windows::Devices::Enumeration::DeviceInformation deviceInformation, winrt::hstring deviceSelector_)
    : device(deviceInformation), deviceSelector(deviceSelector_)
{ }

This way, device will be copy-initialized from deviceInformation.

Otherwise, the compiler will try to default-construct device and then copy-assign deviceInformation to it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文