带有超链接的 WPF Resources.resx 字符串?
我想要一个字符串资源,其中包含一个超链接。我想这是不可能的,除非我有 4 个资源字符串:
预超链接文本 超链接href 超链接文本 超链接后文本。
然后通过以下方式在 xaml 中构造它:
<StackPanel Grid.Column="1" Orientation="Horizontal" VerticalAlignment="Top" HorizontalAlignment="Right">
<TextBlock Grid.Column="1" Text="{x:Static p.Resources.PreHText}" />
<Hyperlink Grid.Column="1" NavigateUri="{x:Static p.Resources.HHref}">
<TextBlock Text="{x:Static p.Resources.HText}" /></Hyperlink></TextBlock>
<TextBlock Grid.Column="1" Text="{x:Static p.Resource.PostHText}" />
</StackPanel>
这太糟糕了,原因有很多(样式、不是很动态等)。创建我自己的渲染和字符串格式的栏,例如“请发送电子邮件 {[email protected]< /a>|服务台}以获得进一步帮助”。还有其他方法可以实现这一目标吗? (不必使用 resources.resx 文件)
I want to have a string resource, which contains a hyperlink in it. I guess this isn't possible, unless I had 4 resource strings:
Pre-hyperlink text
hyperlink href
hyperlink text
Post-hyperlink text.
Then construct it in the xaml via:
<StackPanel Grid.Column="1" Orientation="Horizontal" VerticalAlignment="Top" HorizontalAlignment="Right">
<TextBlock Grid.Column="1" Text="{x:Static p.Resources.PreHText}" />
<Hyperlink Grid.Column="1" NavigateUri="{x:Static p.Resources.HHref}">
<TextBlock Text="{x:Static p.Resources.HText}" /></Hyperlink></TextBlock>
<TextBlock Grid.Column="1" Text="{x:Static p.Resource.PostHText}" />
</StackPanel>
Which is just awful, for many many reasons (Styling, not very dynamic etc etc). Bar creating my own render and string format, such as "Please email {[email protected]|the helpdesk} for further assistance". Is there any other way to achieve this? (Doesn't have to use the resources.resx file)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最后,我只是为它制作了自己的文本块控件(想象中命名为 AdvancedTextBlock):
我对我的实现不太满意的唯一两件事是:
A) 我必须隐藏现有的 Text 属性,因为我不知道如何防止该属性覆盖我所做的事情
B) (与 A 相关)我必须调用
每次设置
FormattedText
字段时(应该只承认一次),但这又是一次,因为不知道如何挂钩到实际执行显示内容的方法(期望找到 PreRender、Render 事件或类似事件,但我找不到),所以如果有人知道如何,那就太棒了:)。In the end I just made my own textblock control for it (Imaginitively named AdvancedTextBlock):
The only two things I'm not too happy about with my implementation is that:
A) I had to hack-hide the existing Text property, because I didn't know how to prevent that property from overriding the stuff I do
B) (Related to A) I have to call
AssignInlines
everytime theFormattedText
field is set (which should only be once admittidly), but this is again, down to not knowing how to hook into the method which actually does the displaying stuff (Expecting to find a PreRender, Render event or similar, however I could not), so if anyone knows how to, that'd be awesome :).我的解决方法是在 WPF XAML 顶部声明“x”和“resx”,以便它可以看到资源。 (将 ProductName 替换为产品的命名空间):
然后我通过使其包含 Run 子控件来调整超链接。 (将 DeactivateCommand 更改为数据上下文中的命令,并将 Strings.Deactivate 更改为您的资源名称。资源属性:
我更喜欢这种方式,因为它更短并且没有自定义控件。
如果您需要单词“Contact:”,后跟 emailTo 链接, 你可以:
My workaround is to declare "x" and "resx" at top of the WPF XAML so it can see the resource. (Replace ProductName with your namespace for the product):
and then I adjust the hyperlink by having it contain a Run sub control. (Change DeactivateCommand to your command in your data context and the Strings.Deactivate with your resource name.resource property:
I prefer this way because it is shorter and no custom control.
If you need the word "Contact:" followed by the emailTo link, you can:
这是我的解决方案:
Here is my solution: