2017-06-06 16 views
0

我的目標是使用鏈接打開新窗口的字符串。問題在於字符串必須是可本地化的。僅使xaml中的鏈接成爲資源字符串的一部分

爲了讓我們的本地化工具能夠識別的字符串,它是這樣定義的:

<sys:String x:Uid="testString" x:Key="testString">click here for a good time</sys:String> 

的字符串將然後像這樣被引用:

<TextBlock Text="{StaticResource testString}"/> 

我需要「這裏」這個詞是打開另一個窗口的鏈接。其他單詞在點擊時不應該做任何事情。

這甚至可能嗎?

回答

0

是否有某些原因,這些字符串不能拆分?你已經在你的問題中假設'這裏'轉化爲他們應該點擊的地方。

<sys:String x:Uid="prefix" x:Key="testString">click</sys:String> 
<sys:String x:Uid="caption" x:Key="testString">here</sys:String> 
<sys:String x:Uid="suffix" x:Key="testString">for a good time</sys:String> 

<StackPanel> 
    <TextBlock Text="{StaticResource prefix}" /> 
    <Button Command={StaticResource someCommand}> 
     <TextBlock Text="{StaticResource caption}" /> 
    </Button> 
    <TextBlock Text="{StaticResource suffix}" /> 
</StackPanel> 
0

這可能嗎?

不是沒有分裂的string到莫名其妙的話,寫一些代碼。你需要在某個地方定義實際的鏈接。

例如,您可以處理TextBlockLoaded事件並填充其Inline屬性。

下面是一個應該給你的想法的例子。

<TextBlock Text="{StaticResource testString}" Loaded="TextBlock_Loaded"/> 

private void TextBlock_Loaded(object sender, RoutedEventArgs e) 
{ 
    const string linkText = "here"; 
    TextBlock txt = sender as TextBlock; 
    string[] words = txt.Text.Split(' '); 
    if (words.Contains(linkText)) 
    { 
     txt.Text = string.Empty; 
     foreach (string word in words) 
     { 
      if (word == linkText) 
      { 
       var link = new Hyperlink(new Run(linkText + " ")); 
       link.Click += (ss, ee) => 
       { 
        //do something when the link is clicked on 
       }; 
       txt.Inlines.Add(link); 
      } 
      else 
      { 
       txt.Inlines.Add(word + " "); 
      } 
     } 
     txt.Text.TrimEnd(); 
    } 
} 
+0

這正是我所擔心的。 字符串不能拆分,因爲「點擊這裏很好」可以作爲整個句子不同地翻譯,而不是將已翻譯的單詞粘在一起。 – Ash

+0

因此,在你定義單詞「here」是可點擊的地方......? – mm8