c++/winrt deviceinformation不存在错误
您好,我正在尝试从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您的
DeviceListEntry
声明如下所示:尝试像这样实现您的构造函数:
这样,
device
将从deviceInformation
进行复制初始化。否则,编译器将尝试默认构造
device
,然后将deviceInformation
复制分配给它。Assuming your
DeviceListEntry
declaration looks like:Try implementing your constructor like this:
This way,
device
will be copy-initialized fromdeviceInformation
.Otherwise, the compiler will try to default-construct
device
and then copy-assigndeviceInformation
to it.