如何清除WinRT中的TimeZoneInfo缓存? [英] How to clear TimeZoneInfo cache in WinRT?

查看:106
本文介绍了如何清除WinRT中的TimeZoneInfo缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在.NET中, TimeZoneInfo 使用的某些值被缓存,例如 TimeZoneInfo.Local DateTime.ToLocalTime 等。在传统的.NET Framework应用程序中,可以通过调用 TimeZoneInfo.ClearCachedData(),如在文档中解释

In .NET some values used by TimeZoneInfo are cached, such as the current time zone in use by TimeZoneInfo.Local, DateTime.ToLocalTime, etc. In traditional .NET framework apps, this cache can be cleared by calling TimeZoneInfo.ClearCachedData(), as explained in the docs.

但是,在WinRT应用程序(例如Windows Store应用程序和Windows Universal Apps)中, ClearCachedData API不可用。

However, in WinRT applications, such as Windows Store apps, and Windows Universal Apps, the ClearCachedData API is not available.

如何清除WinRT环境中的时区缓存?

How do you clear the time zone cache in a WinRT environment?

推荐答案

有一个未记录的方法可以实现此目标,在.NET中可以看到参考源

There's an undocumented way to achieve this, as seen here in the .NET reference source.

TimeZoneInfo.ConvertTime(new DateTime(0), TimeZoneInfo.Utc);

这将清除时区缓存,并且可以在WinRT和传统.NET Framework中使用( 4.6 +)、. NET Core和PCL。

This will clear the time zone cache, and it works in WinRT as well as traditional .NET Framework (4.6+), .NET Core, and PCL.

根据Microsoft .NET团队的同事的说法,这是解决已知问题的非正式解决方法。

According to my colleagues at Microsoft on the .NET team, this is an unofficial workaround to a known problem. It is not expected to be removed, but it is an implementation detail and not part of the public API, so use at your own risk.

请注意,<$ c $如果可用,应使用c> TimeZoneInfo.ClearCachedData 代替此方法。它已添加到.NET Standard 2.0和UWP 10.1中(在此处查看所有环境)。

Note that TimeZoneInfo.ClearCachedData should be used instead of this approach, when it is available. It was added to .NET Standard 2.0, and UWP 10.1 (see all environments here).

这篇关于如何清除WinRT中的TimeZoneInfo缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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