XAML如何在两个地方使用一个资源数据值?

How to use a resource data value in two places in XAML?

我希望显示与 Button 的工具提示和 TextBlock 的文本相同的文本。这是我的简化 xaml:

<Button x:Uid="SweetButton">
  <TextBlock x:Uid="SweetTextBlock"/>
</Button>

现在我的 .resw 文件中有两个条目:

<data name="SweetButton.ToolTipService.ToolTip" xml:space="preserve">
  <value>The same text</value>
</data>
<data name="SweetTextBlock.Text" xml:space="preserve">
  <value>The same text</value>
</data>

有什么方法可以让我的 .resw 文件中只有一个条目吗?

我试过为数据元素提供两个名称属性,但这是不允许的。我想以某种方式引用一个数据元素的值作为第二个数据元素的值,但我不确定如何。

遗憾的是,您无法在尝试填写 2 个完全不同的属性(ToolTipText 时仍然使用 x:Uid 语法的同时重新使用该资源条目). x:Uid 的通用资源系统非常强大,可以在你的资源文件中附加任何 属性,但缺点是资源值的可重复性。

UWP XAML has somewhat different rules for x:Uid uniqueness than previous XAML-utilizing technologies used. For UWP XAML it is legal for the same x:Uid ID value to exist as a directive on multiple XAML elements. However, each such element must then share the same resolution logic when resolving the resources in a resource file. Also, all XAML files in a project share a single resource scope for purposes of x:Uid resolution, there is no concept of x:Uid scopes being aligned to individual XAML files.

来源:https://msdn.microsoft.com/en-us/library/windows/apps/mt204791.aspx

如果您有很多重复的资源值,您可以考虑使用 'old' 方法之一来处理资源并自行绑定资源。