WPF:动态标记扩展 - 重新渲染/更新值
我正在使用标记扩展在 WPF 应用程序中加载国际化字符串,如下所示:
<Button Content="{Translate MyText}"/>
我的标记扩展名为“TranslateExtension”,它从数据库中搜索键“MyText”的值。 它在
ProvideValue(IServiceProvider serviceProvider)
返回正确字符串的方法中执行此操作。 一切正常。
我的问题是,ProvideValue-Method 不会再次被调用,并且当语言更改时无法从数据库中提取新字符串。 我现在需要一种方法来使返回值“动态”,使按钮重新加载它的 xaml 并重新使用标记扩展,无论它是通过更改语言或其他方式时抛出的事件来实现的。 如何让系统再次调用 ProvideValue-Method? 我尝试过类似的 InvalidateVisual() InvalidateArrange() InvalidateMeasure() UpdateLayout()...
我希望我说清楚了。 如果您认为自己能够提供想法或解决方案,请随时询问更多信息。 谢谢
I´m using a markupextension for loading internationalized strings in a WPF application like this:
<Button Content="{Translate MyText}"/>
My markupextension is named "TranslateExtension" and it searches a value for the key "MyText" from a database. It does this in the
ProvideValue(IServiceProvider serviceProvider)
method which returns the right string. Everything is working fine.
My problem is, that the ProvideValue-Method isn´t ever called again and there´s no way to pull a new string from the database when the language is changed. I now need a way to make the returned value "dynamic", to make the Button to reload it´s xaml and re-use the markupextension whether it´ll be through an event thrown when changing languages or whatever. How do i make the system call the ProvideValue-Method again? I tried the likes of
InvalidateVisual() InvalidateArrange() InvalidateMeasure() UpdateLayout()...
I hope I made myself clear. Please feel free to ask for more information of you think you´re able to provide ideas or solutions.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嗯,解决方案是非常简单的想法。
ProvideValue 仅被调用一次,但 serviceProvider 参数还为您提供对象实例和属性 (PropertyInfo)。 这就是您稍后更新属性所需的全部内容。
我会在 App.cs 中创建一个静态事件,比如说 CultureChanged。
在ProvideValue中,从ServiceProvider获取IProvideValueTarget服务,从中可以获取对象实例和属性。 附加到 CultureChanged 事件并在其 EventHandler 中使用反射设置属性值。 请注意内存泄漏。
有 MarkupExtension 使用这种方法并允许动态缓存区域性:
http://www.codeproject.com/KB/WPF/WpfMultiLanguage.aspx
Well, solution is quite simple idea.
ProvideValue is called only once, but serviceProvider parameter provides you also object instance and property (PropertyInfo). This is all you need to update the propety later.
I would make an static event in App.cs, let's say CultureChanged.
In ProvideValue, get IProvideValueTarget service from ServiceProvider, from which you can get the object instance and property. Attach to CultureChanged event and in its EventHandler set the property value using reflection. Do be mindful of memory leaks with.
There is MarkupExtension which uses this approach and allows to cache culture on the fly:
http://www.codeproject.com/KB/WPF/WpfMultiLanguage.aspx
我没有给你答案,只是一个建议......
也许你可以考虑使用 Binding 与 IValueConverter 相结合来进行翻译。 然后,如果用户界面的语言发生变化,您可以简单地递归所有绑定并刷新它们。
就像我说的,只是一个想法。
I don't have an answer for you, but rather a suggestion...
Perhaps you could consider using a Binding combined with an IValueConverter to do the translation. Then, if the language of the user interface changes, you can simply recurse all bindings and refresh them.
Like I said, just an idea.