在我的SL应用程序中,我有一个连接到Web服务的页面,用于检索某些文化信息,例如日期格式。由于在Silverlight应用程序的实例化时不知道此信息,我无法在公共App()构造函数中设置此信息。以下行似乎不适用于页面类中的服务调用的异步完成方法:
var dateFormatString = e.Result.DateFormatString;
Thread.CurrentThread.CurrentCulture = (CultureInfo)Thread.CurrentThread.CurrentCulture.Clone();
Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern = dateFormatString;
有没有办法从页面类设置silverlight应用程序的文化?
答案 0 :(得分:0)
在主线程中设置它(你的asyncCompleted可能在不同的线程上运行)。 如果你不知道怎么做,请告诉我。 没有必要克隆文化。 (代码中的第2行)。 只需直接设置:
var dateFormatString = e.Result.DateFormatString;
Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern = dateFormatString;
** EDIT添加了从UI Thread调用的示例代码。 您始终可以将状态传递给异步wcf调用。在这种情况下,我传递一个Dispatcher。 调用回调后,使用Dispatcher更改UI线程中的UI Culture。 我把它写在了我的头顶,但应该足以让你知道你需要做什么。
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,你可以使用它的Disptacher属性。 否则使用静态:
Deployment.Current.Dispatcher