“System.DateTime"不是有效的 Windows 运行时参数类型 [英] 'System.DateTime' is not a valid Windows Runtime parameter type

查看:23
本文介绍了“System.DateTime"不是有效的 Windows 运行时参数类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 C# 类,它在我的 Windows 应用商店应用 (C#) 中运行良好.但是当我尝试在 Windows 运行时组件中使用它时,我收到以下错误:

I'm using a C# class and it works perfectly fine in my Windows Store App (C#). But when I try to use it in a Windows Runtime Compenent I get the following error:

Calculator.Calculate(System.DateTime)' 具有类型为 'System.DateTime' 的参数 'dateTime'.System.DateTime"不是有效的 Windows 运行时参数类型.

Calculator.Calculate(System.DateTime)' has parameter 'dateTime' of type 'System.DateTime'. 'System.DateTime' is not a valid Windows Runtime parameter type.

类中的示例对象:

public DateTime Calculate(DateTime dateTime)
{
   int dayNumberOfDateTime = ExtractDayNumber(dateTime);
   int sunRiseInMinutes = CalculateSunRiseInternal(tanSunPosition, differenceSunAndLocalTime);
   return CreateDateTime(dateTime, sunRiseInMinutes);
}

我该如何解决这个问题?还有什么问题?

How can I fix this? And what is the problem?

推荐答案

当您创建 Windows 运行时组件时,您的组件可以由不受管理的语言使用,如 Javascript 或 C++.显然,这些语言不知道如何生成正确的 System.DateTime,它是一种特定的 .NET 类型.

When you create a Windows Runtime Component then your component can be used by languages that are not managed, like Javascript or C++. Clearly those languages have no idea how to generate a proper System.DateTime, it is a specific .NET type.

因此,此类组件必须仅使用本机 WinRT 类型,否则请遵守 WinRT 中存在的限制.您从一开始就会遇到的一个这样的限制是 WinRT 不支持实现继承.这要求您声明您的类密封.

Such components must therefore only use native WinRT types and otherwise observe the restrictions present in WinRT. One such restriction you'll run into from the get-go is that WinRT does not support implementation inheritance. Which requires you to declare your class sealed.

本机 WinRT 类型与 .NET 类型非常不同.可以存储日期的真正运行时类型是 Windows.Foundation.DateTime.字符串实际上是一个 HSTRING 句柄.List 实际上是一个 IVector.等等.

The native WinRT types are very unlike the .NET types. The real runtime type that can store a date is Windows.Foundation.DateTime. A string is actually an HSTRING handle. A List is actually an IVector. Etcetera.

不用说,如果您实际上必须使用这些本机类型,那么您的程序将不再类似于 .NET 程序.而您没有,CLR 的 .NET 4.5 版本内置了语言投影.代码可自动将 WinRT 类型转换为等效的 .NET 类型.该翻译有一些粗糙的边缘,某些类型无法轻易替代.但他们中的绝大多数都没有问题.

Needless to say, if you would actually have to use those native types then your program wouldn't resemble a .NET program anymore. And you don't, the .NET 4.5 version of the CLR has a language projection built in. Code that automatically translates WinRT types to their equivalent .NET types. That translation has a few rough edges, some types cannot easily be substituted. But the vast majority of them map without trouble.

System.DateTime 就是这样一种粗糙的边缘.Windows.Foundation.DateTime 的语言投影是 System.DateTimeOffset.因此,只需通过如下声明您的方法来解决您的问题:

System.DateTime is one such rough edge. The language projection of Windows.Foundation.DateTime is System.DateTimeOffset. So simply solve your problem by declaring your method like this:

public DateTimeOffset Calculate(DateTimeOffset dateTime) {
    // etc..
}

唯一值得注意的一点是,这仅适用于其他代码可能使用的成员.公共成员.

The only other point worth noting is that this is only required for members that other code might use. Public members.

这篇关于“System.DateTime"不是有效的 Windows 运行时参数类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆