Workaround for x:Shared in Universal Applications

别来无恙 提交于 2019-12-11 10:18:52

问题


I have a Style setter that sets the value to a resource.

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?


回答1:


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>



来源:https://stackoverflow.com/questions/33388126/workaround-for-xshared-in-universal-applications

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!