Silverlight ValidatesOnException 默认消息本地化

发布于 2024-12-04 10:43:04 字数 832 浏览 2 评论 0原文

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

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

对于输入,我刚刚添加了“ValidatesOnException=true”以防出现转换问题,它就完成了这项工作。但默认的异常消息是我的操作系统的文化,而不是手动选择的。

在关于异常消息本地化的线程中,想法是更改 CurrentCulture 和 CurrentUICulture,我就是这样做的。所以我有点卡住了。

我能做些什么 ?

谢谢:)

编辑:我尝试在convertback方法中使用带有自定义异常的自定义转换器,以验证用户的输入。问题是,convertback 方法中的异常不会被 validatesOnException 捕获,它会破坏应用程序。

编辑2:澄清->如果我有一个绑定到文本框的小数属性,并且我在此文本框中输入“blabla”,我想看到是否存在问题,并且我希望消息位于运行时区域设置中,而不是操作系统区域设置中。 我无法在属性设置器中引发异常,因为我从未到达那里,默认转换器在此之前引发了自己的异常。

我希望一切都清楚。如果我能帮助你,请不要犹豫:)

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 :)

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

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

发布评论

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

评论(3

悸初 2024-12-11 10:43:04

也许你一开始就没有改变文化。

我建议您尝试此链接中第一个答案中给出的方法:

更改 Silverlight 应用程序的文化< /a>

Perhaps you aren't changing the culture at the outset.

I suggest that you try the approach given in the first answer in this link:

Change culture of Silverlight application

挽袖吟 2024-12-11 10:43:04

一种可能的方法是将属性的类型更改为字符串,即使您在其后面存储了十进制值。 getter 将在存储的 decimal 值上调用 ToString,而 setter 将执行从 stringdecimal 的转换> 使用 Decimal.Parse或类似的。这种方法确实意味着您必须自己进行类型转换,但它至少给您更多的控制权。

您的 setter 可以抛出异常来指示验证错误。或者,您可以使用以下接口之一 IDataErrorInfoINotifyDataErrorInfo 显示验证错误。 此页面< /a> 有一个使用 IDataErrorInfo 的示例,并且 这个有一个例子使用 INotifyDataErrorInfo。

One possible approach is to change the type of the property to string, even though you're storing a decimal value behind it. The getter would call ToString on the decimal value stored, and the setter would do the conversion back from string to decimal using Decimal.Parse or similar. This approach does mean you have to do the type conversion yourself, but it does at least give you a bit more control.

Your setter can throw exceptions to indicate validation errors. Alternatively, you can use one of the interfaces IDataErrorInfo and INotifyDataErrorInfo to show the validation error. This page has an example of using IDataErrorInfo, and this one has an example using INotifyDataErrorInfo.

汹涌人海 2024-12-11 10:43:04

您可以使用 ValidationRule 的自定义实现并添加到 Binding.ValidationRules 集合。您必须先清除该集合(我不确定如何执行 XAML)并添加此规则(MSDN 页面之一中描述了如何执行此操作)。

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

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.

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