带有超链接的WPF Resources.resx字符串?

Psy*_*nic 7 c# wpf xaml

我想要一个字符串资源,其中包含一个超链接.我想这是不可能的,除非我有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>
Run Code Online (Sandbox Code Playgroud)

由于许多原因(造型,不是非常动态等等),这很可怕.栏创建我自己的渲染和字符串格式,例如"请发送电子邮件至{me@there.com |帮助台}以获得进一步的帮助".有没有其他方法来实现这一目标?(不必使用resources.resx文件)

Psy*_*nic 1

最后我只是为其制作了自己的文本块控件(想象中命名为 AdvancedTextBlock):

    public class AdvancedTextBlock : TextBlock {
        new private String Text { get; set; } //prevent text from being set as overrides all I do here.
        private String _FormattedText = String.Empty;
        public String FormattedText {
            get { return _FormattedText; }
            set { _FormattedText = value; AssignInlines(); }
        }
        private static Regex TagRegex = new Regex(@"\{(?<href>[^\|]+)\|?(?<text>[^}]+)?}", RegexOptions.Compiled);

        public AdvancedTextBlock() : base() { }
        public AdvancedTextBlock(System.Windows.Documents.Inline inline) : base(inline) { }

        public void AssignInlines(){
            this.Inlines.Clear();
            Collection<Hyperlink> hyperlinks = new Collection<Hyperlink>();
            Collection<String> replacements = new Collection<String>();
            MatchCollection mcHrefs = TagRegex.Matches(FormattedText);
            foreach (Match m in mcHrefs) {
                replacements.Add(m.Value);
                Hyperlink hp = new Hyperlink();
                hp.NavigateUri = new Uri(m.Groups["href"].Value);
                hp.Inlines.Add(m.Groups["text"].Success ? m.Groups["text"].Value : m.Groups["href"].Value);
                hp.RequestNavigate += new RequestNavigateEventHandler(hp_RequestNavigate);
                hyperlinks.Add(hp);
            }
            String[] sections = FormattedText.Split(replacements.ToArray(), StringSplitOptions.None);
            hyperlinks.DefaultIfEmpty(null);
            for (int i = 0, l = sections.Length; i < l; i++) {
                this.Inlines.Add(sections.ElementAt(i));
                if (hyperlinks.ElementAtOrDefault(i) != null) {
                    this.Inlines.Add(hyperlinks[i]);
                }
            }
        }

        void hp_RequestNavigate(object sender, RequestNavigateEventArgs e) {
            RequestNavigate(sender, e);
        }

        //
        // Summary:
        //     Occurs when navigation events are requested.
        public event RequestNavigateEventHandler RequestNavigate;
    }
Run Code Online (Sandbox Code Playgroud)

我对我的实现不太满意的唯一两件事是:

A)我必须隐藏现有的 Text 属性,因为我不知道如何防止该属性覆盖我所做的事情

B)(与AAssignInlines相关)每次设置字段时我都必须调用FormattedText(承认应该只调用一次),但这又是一次,因为不知道如何挂钩实际执行显示内容的方法(期望找到PreRender、Render 事件或类似事件,但我不能),所以如果有人知道如何,那就太棒了:)。