作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
根据 WinRT 规范,私有(private)类应该覆盖 IInspectable::GetRuntimeClassName()并返回一个不同的名称(或 NULL)。但是由于 IInspectable
不是一个 ref 类(它是一个结构),我不能从它继承,也不能重写这些方法。
从 MSDN 定义:
HRESULT GetRuntimeClassName(
[out] HSTRING *className
);
所以问题是,如何为 WinRT (ref) 类覆盖 IInspectable::GetRuntimeClassName()
?
示例尝试:
#include "Hstring.h"
#include "Inspectable.h"
#include <sal.h>
namespace PhoneVoIPApp
{
namespace BackEnd
{
namespace CallController
{
public interface class IHelloWorld
{
Platform::String^ SayHello();
};
private ref class HelloWorldImpl sealed :IHelloWorld
{
public:
virtual Platform::String^ SayHello();
virtual HRESULT GetRuntimeClassName(_Out_ HSTRING *className) override;
};
Platform::String^ HelloWorldImpl::SayHello()
{
return L"Hello World!";
}
HRESULT HelloWorldImpl::GetRuntimeClassName(_Out_ HSTRING *className)
{
*className = nullptr;
return E_NOTIMPL;
}
}
}
}
最佳答案
我与 CLR 团队进行了交谈,他们为我指出了正确答案。
当使用私有(private)类时,可以将一个 secret 的未记录属性 Platform::Metadata::RuntimeClassName
添加到代码中。
所以问题的解决方法是:
#include "Hstring.h"
#include "Inspectable.h"
#include <sal.h>
namespace PhoneVoIPApp
{
namespace BackEnd
{
namespace CallController
{
public interface class IHelloWorld
{
Platform::String^ SayHello();
};
private ref class HelloWorldImpl sealed : [Platform::Metadata::RuntimeClassName]IHelloWorld
{
public:
virtual Platform::String^ SayHello();
};
Platform::String^ HelloWorldImpl::SayHello()
{
return L"Hello World!";
}
}
}
}
关于c++ - 如何为 WinRT (ref) 类覆盖 IInspectable::GetRuntimeClassName(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21309802/
根据 WinRT 规范,私有(private)类应该覆盖 IInspectable::GetRuntimeClassName()并返回一个不同的名称(或 NULL)。但是由于 IInspectable
我是一名优秀的程序员,十分优秀!