我可以从页面类设置 silverlight 应用程序的区域性吗?

发布于 2024-10-15 20:42:21 字数 488 浏览 3 评论 0原文

在我的 SL 应用程序中,我有一个连接到 Web 服务的页面来检索一些文化信息,例如日期格式。由于此信息在 silverlight 应用程序实例化时未知,因此我无法在公共 App() 构造函数中设置此信息。以下几行似乎不适用于页面类内服务调用的异步完成方法:

var dateFormatString = e.Result.DateFormatString;
Thread.CurrentThread.CurrentCulture = (CultureInfo)Thread.CurrentThread.CurrentCulture.Clone();
Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern = dateFormatString;

Is there a way to set theculture of the silverlight application from a page class?

In my SL application, I have a page that connects to a web service to retrieve some culture information such as date formats. As this information is not known at the instantiation of the silverlight application, I cannot set this in the public App() constructor. The following lines don't seem to work in the async completed method of the service call within the page class:

var dateFormatString = e.Result.DateFormatString;
Thread.CurrentThread.CurrentCulture = (CultureInfo)Thread.CurrentThread.CurrentCulture.Clone();
Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern = dateFormatString;

Is there a way to set the culture of the silverlight application from a page class?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稚然 2024-10-22 20:42:21

将其设置在主线程中(您的 asyncCompleted 可能在不同的线程上运行)。
如果您不知道该怎么做,请告诉我。
没有必要克隆文化。 (代码中的第 2 行)。
只需直接设置即可:

var dateFormatString = e.Result.DateFormatString;
Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern = dateFormatString;

** 编辑添加了从 UI 线程调用的示例代码。
您始终可以将状态传递给异步 WCF 调用。在这种情况下,我正在传递一个调度程序。
调用回调后,使用 Dispatcher 更改 UI 线程中的 UI 区域性。
这是我随手写下的,但应该足以让您了解需要做什么。

    public void SetCulture()
    {
        YourWcfProxy.GetCultureInfoCompleted += GetCultureInfoCompleted;
        object state = Dispatcher; // variable just for clarity.
        YourWcfProxy.GetCultureInfoAsync(state);
    }

    private void GetCultureInfoCompleted(object sender, GetCultureInfoCompletedCompletedEventArgs e)
    {
        Dispatcher dispatcher = e.UserState as Dispatcher;
        dispatcher.BeginInvoke(() =>
        {
            // set the ui culture here!
        }
        );
    }

** 编辑2(回答问题)

如果您手边有一个UIElement,您可以使用它的Dispatcher 属性。
否则使用静态:

Deployment.Current.Dispatcher

Set it in the main thread (your asyncCompleted is probably running on a different thread).
Let me know if you do not know how to do this.
There is no need to clone the Culture. (line 2 in your code).
Just set it directly:

var dateFormatString = e.Result.DateFormatString;
Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern = dateFormatString;

** EDIT added sample code to call from UI Thread.
You can always pass state to an async wcf call. In this case I am passing a Dispatcher.
Once the callback is called use the Dispatcher to change the UI Culture in the UI thread.
I wrote this off the top of my head, but should be sufficient to give you an idea of what you need to do.

    public void SetCulture()
    {
        YourWcfProxy.GetCultureInfoCompleted += GetCultureInfoCompleted;
        object state = Dispatcher; // variable just for clarity.
        YourWcfProxy.GetCultureInfoAsync(state);
    }

    private void GetCultureInfoCompleted(object sender, GetCultureInfoCompletedCompletedEventArgs e)
    {
        Dispatcher dispatcher = e.UserState as Dispatcher;
        dispatcher.BeginInvoke(() =>
        {
            // set the ui culture here!
        }
        );
    }

** EDIT 2 (answer question)

If you have a UIElement handy, you can use it's Disptacher property.
Otherwise use the static:

Deployment.Current.Dispatcher
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文