silverlight时区转换

tk.*_*tk. 6 silverlight timezone datetime

我正在尝试将WPF应用程序迁移到SilverLight 4.WPF应用程序使用TimeZoneInfo.FindSystemTimeZoneById()和TimeZoneInfo.ConvertTimeFromUtc()将特定时区的DateTime转换为另一个特定时区的DateTime.

但我在SilverLight 4中找不到这些功能.SilverLight似乎只支持Utc和Local之间的时区转换.

有没有办法将DateTime从任何时区转换为SilverLight中的任何其他时区?

Igo*_*nko 2

不幸的是,目前没有标准功能可以做到这一点。

让我们检查(使用反射器)TimeZoneInfo.FindSystemTimeZoneById() 方法的工作原理。它仅采用 s_systemTimeZones 字段中的值之一:

private static Dictionary<string, TimeZoneInfo> s_systemTimeZones
{
    get
    {
        if (s_hiddenSystemTimeZones == null)
        {
            s_hiddenSystemTimeZones = new Dictionary<string, TimeZoneInfo>();
        }
        return s_hiddenSystemTimeZones;
    }
    set
    {
        s_hiddenSystemTimeZones = value;
    }
}
Run Code Online (Sandbox Code Playgroud)

该字段存储所有可用的 TimeZoneInfo-s。当您调用 FindSystemTimeZoneById(id) 时,它只是从预填充的字典中选取一些值。我不知道这个字典何时初始化以及它使用哪些值进行初始化。但是这个线程中的人告诉 TimeZoneInfo 使用注册表中的值: HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Time Zones

最明显的方法是创建自己的 Dictionary 字典并用值填充它。像这样的东西:

Dictionary<string, TimeZoneInfo> dictionary = new Dictionary<string, TimeZoneInfo>();
TimeZoneInfo info = new TimeZoneInfo("ID", new TimeSpan(0, 1, 0, 0), "SomeCultureName", "Some Standard Time", "Some Daylight Time", null, true);
dictionary.Add("Some time", info);
Run Code Online (Sandbox Code Playgroud)

但还有另一个问题:TimeZoneInfo 构造函数是私有的。因此,如果您想使用 FindSystemTimeZoneById() 和 ConvertTimeFromUtc() 功能,那么您应该从头开始实现它。创建一些代表时区的类,创建并用时区信息填充此类的字典等等......
我知道这不是好消息。但我希望它对你有用:)