x:Shared 在通用应用程序中的解决方法 [英] Workaround for x:Shared in Universal Applications
本文介绍了x:Shared 在通用应用程序中的解决方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个样式设置器,可以将值设置为资源.
I have a Style setter that sets the value to a resource.
它适用于单个元素,但是当 Style 应用于 1 个以上的元素时,会抛出异常.这就是我所拥有的:
It works for a single element, but when the Style is applied to more than 1 element, an exception is thrown. This is what I have:
<SymbolIcon x:Key="Star" Symbol="Star" />
<Style TargetType="ContentControl">
<Setter Property="Content" Value="{StaticResource Star}"/>
</Style>
我知道只会创建一个实例.既然我无法创建多实例"资源,我该如何让它工作?
I understand that only one instance will be created. Since I cannot create "multi-instance" resources , how could I make it work?
推荐答案
如果你想制作一对元素,而不仅仅是一个实例,那么你可以使用模板.一个简单的例子:
If you want to make couple elements, not only one instance, then you can use templates. A simple example:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.Resources>
<DataTemplate x:Key="Star">
<SymbolIcon Symbol="Favorite"/>
</DataTemplate>
<Style TargetType="ContentControl">
<Setter Property="ContentTemplate" Value="{StaticResource Star}"/>
</Style>
</Grid.Resources>
<StackPanel Orientation="Horizontal">
<ContentControl/>
<ContentControl/>
<ContentControl/>
<ContentControl/>
</StackPanel>
</Grid>
这篇关于x:Shared 在通用应用程序中的解决方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文