Silverlight ValidatesOnException默认消息本地化 [英] Silverlight ValidatesOnException default message localization

查看:165
本文介绍了Silverlight ValidatesOnException默认消息本地化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的silverlight 4 MVVM应用程序中,我可以在运行时切换语言:

  public void SetLanguage $ b {
var culture = new CultureInfo(language);
Thread.CurrentThread.CurrentUICulture = culture;
Thread.CurrentThread.CurrentCulture = culture;
// ...
}

ValidatesOnException = true在转换问题的情况下,它做的工作。但是默认的异常消息是在我的操作系统的文化,而不是手动选择的。



异常消息本地化的想法是改变CurrentCulture和CurrentUICulture,我做了。



感谢:)

我可以做些什么?

编辑:我试图在convertback方法中使用自定义转换器与自定义异常,以验证用户的输入。问题,在convertback方法中的异常没有被validatesOnException捕获,它打破了应用程序。



编辑2:澄清 - >如果我有一个十进制属性绑定一个文本框,我在这个文本框中输入blabla,我想看到有一个问题,我希望消息在运行时语言环境,而不是操作系统语言环境。
我不能在我的属性设置中引发异常,因为我从来没有到达,默认转换器在此之前引发自己的异常。



我希望它清楚中。如果我可以帮助你帮助我,请不要犹豫:)

解决方案

=http://msdn.microsoft.com/en-us/library/system.windows.controls.validationrule.aspx =nofollow> ValidationRule ,并将其添加到 Binding.ValidationRules 集合。你必须清除之前的集合(我不知道如何做到XAML),并添加这条规则(如何做到在MSDN页之一描述)。



此类具有验证方法,您可以在其中执行验证并返回所需的错误消息。


In my silverlight 4 MVVM application, i can switch languages during runtime :

public void SetLanguage(string language)
{
    var culture = new CultureInfo(language);
    Thread.CurrentThread.CurrentUICulture = culture;
    Thread.CurrentThread.CurrentCulture = culture;
    // ...
}

For the inputs, i just added "ValidatesOnException=true" in case of conversion problems and it does the job. But the default exception message is in the culture of my OS and not in the manually chosen one.

In this thread on exception message localization the idea is to change CurrentCulture and CurrentUICulture, which i did. So i'm kind of stuck.

What can i do ?

Thanks :)

Edit : i tried to use a custom converter with a custom exception in the convertback method in order to validate the user's input. Problem, an exception within a convertback method is NOT caught by the validatesOnException, it breaks the application.

Edit 2 : to clarify -> if i have a decimal property bound to a textbox, and i enter "blabla" in this textbox, i want to see that there is a problem, and i want the message to be in the runtime locale and not the OS locale. I can't raise an exception in my property setter because i never get there, the default converter raises its own exception before that.

I hope it's clear. If i can help you to help me, please don't hesitate :)

解决方案

You can use custom implementation of ValidationRule and add to the Binding.ValidationRules collection. You'll have to clear the collection before (I am not sure how to do it XAML) and add this rule (how to do it is described in one of the MSDN page).

This class has Validate method, where you can perform your validation and return the error message you want.

这篇关于Silverlight ValidatesOnException默认消息本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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