none
关于WPF资源字典的问题 RRS feed

  • 问题

  • 这个问题不好用语言直接描述,我写了一个小demo。

    希望大家帮助我解答下。

    我在App.xaml中定义了一个资源:

    <Application.Resources>
            <Button Content="it is a button"
                    x:Key="button" />
        </Application.Resources>

    然后在MainWindow中这样定义界面:

    <Window x:Class="ContentControlDemo.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <StackPanel Orientation="Vertical">
                <ContentControl Content="{StaticResource button}" />
                <ContentControl Content="{StaticResource button}" />
            </StackPanel>
        </Grid>
    </Window>
    VS设计视图看起来很正常,是这样的:

    可以为什么运行起来就变了呢?如图:



    825386224@qq.com

    2015年1月30日 4:31

答案

  • 你好,

    这里的Resources这么写有问题的,这里的默认的Button实例默认只有一个,你可以参看这里:

    https://msdn.microsoft.com/en-us/library/aa970778(v=vs.110).aspx

    这里有解释:

    “X:Shared is mapped to the XAML language XAML namespace and is recognized as a valid XAML language element by .NET Framework XAML Services and its XAML readers. However, the stated capabilities of x:Shared are only relevant for WPF applications and for the WPF XAML parser. In WPF, x:Shared is only useful as an attribute when it is applied to an object that exists within a WPF ResourceDictionary. Other usages do not throw parse exceptions or other errors, but they have no effect.

    The meaning of x:Shared is not specified in the XAML language specification. Other XAML implementations, such as those that build on .NET Framework XAML Services, do not necessarily provide resource-sharing support. Such XAML implementations could provide similar behavior in the supporting framework that also used x:Shared values.

    In WPF, the default x:Shared condition for resources is true. This condition means that any given resource request always returns the same instance.”

    所以你这里只要这么写就可以了:

    <Button Content="it is a button"
                    x:Key="button" 
                    Background="Blue"
                    x:Shared="false"/>
    大概解释就是WPF里面关于资源共享有一些设置,这里因为默认是true所以根据就近原则只有最后一个应用了这个资源的实例,其他的没有效果。



    Barry
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    2015年2月2日 7:19
    版主

全部回复

  • 你好,

    这里的Resources这么写有问题的,这里的默认的Button实例默认只有一个,你可以参看这里:

    https://msdn.microsoft.com/en-us/library/aa970778(v=vs.110).aspx

    这里有解释:

    “X:Shared is mapped to the XAML language XAML namespace and is recognized as a valid XAML language element by .NET Framework XAML Services and its XAML readers. However, the stated capabilities of x:Shared are only relevant for WPF applications and for the WPF XAML parser. In WPF, x:Shared is only useful as an attribute when it is applied to an object that exists within a WPF ResourceDictionary. Other usages do not throw parse exceptions or other errors, but they have no effect.

    The meaning of x:Shared is not specified in the XAML language specification. Other XAML implementations, such as those that build on .NET Framework XAML Services, do not necessarily provide resource-sharing support. Such XAML implementations could provide similar behavior in the supporting framework that also used x:Shared values.

    In WPF, the default x:Shared condition for resources is true. This condition means that any given resource request always returns the same instance.”

    所以你这里只要这么写就可以了:

    <Button Content="it is a button"
                    x:Key="button" 
                    Background="Blue"
                    x:Shared="false"/>
    大概解释就是WPF里面关于资源共享有一些设置,这里因为默认是true所以根据就近原则只有最后一个应用了这个资源的实例,其他的没有效果。



    Barry
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    2015年2月2日 7:19
    版主
  • 谢谢解疑

    825386224@qq.com

    2015年2月3日 3:21