Wpf 在控件初始化期间访问资源

Wpf 在控件初始化期间访问资源,wpf,silverlight,Wpf,Silverlight,假设我们有以下控件定义 <ctrl:ChildWindow x:Class="Control.Editor" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ctrl="clr-

假设我们有以下控件定义


<ctrl:ChildWindow x:Class="Control.Editor"
                  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                  xmlns:ctrl="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" 
                  xmlns:local="clr-namespace:Control"
                  Width="400" Height="300"
                  local:AttachedProperties.DialogResult="{Binding Path=DialogResult}"
                  Title="{Binding Path=Caption}" Style="{StaticResource Title}" DataContext="{Binding}" HasCloseButton="False">
    <ctrl:ChildWindow.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/Control;component/Resources/BaseAppearance.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </ctrl:ChildWindow.Resources>
</ctrl:ChildWindow>


我建议访问您的资源,并在控件的.Loaded()事件中执行工作

编辑:再想想。。。我想我知道你现在在做什么。。。您的App.xaml类中有一个资源集,但您希望在控件中访问它

解决此问题的简单方法是将其设置为DynamicSource。。。但这并没有那么有效

属性选项卡中App.xaml上的BuildAction设置为什么?
如果是应用程序定义。。。然后,您应该能够像当前一样访问资源。

我找到了一种不使用代码隐藏的常用方法。我知道这是可能的^^


<ctrl:ChildWindow x:Class="Control.Editor"
                  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                  xmlns:ctrl="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" 
                  xmlns:local="clr-namespace:Control"
                  Width="400" Height="300"
                  local:AttachedProperties.DialogResult="{Binding Path=DialogResult}"
                  Title="{Binding Path=Caption}" DataContext="{Binding}" HasCloseButton="False">
    <ctrl:ChildWindow.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/Control;component/Resources/BaseAppearance.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </ctrl:ChildWindow.Resources>
    <crtl:ChildWindow.Style>
        <StaticResource ResourceKey="Title" />
    </crtl:ChildWindow.Style>
</ctrl:ChildWindow>


WPF还是Silverlight?解决方案会有所不同。实际上对于Silverlight,但我希望有一个通用的解决方案。正如我前面提到的,我没有访问App类的权限。因此,我无法在那里加载资源。当我有这个选择的时候,这会很容易。DynamicSource不是最佳解决方案,因为它在Silverlight中无法工作。或者我在这一点上错了?我误解了你的意思,你没有访问App类的权限。我相信你对Silverlight的看法是正确的。。。在这种情况下。。。我认为.Loaded()事件中的代码可能是最好的选择。(除非您有权访问某个ApplicationDefinition类,该类与自定义控件共享资源……在这种情况下,您可以在那里加载资源字典)。嗯,我认为加载的事件不起作用,因为InitializeComponents()(崩溃站点)是在构造函数中执行的,这肯定是在引发加载的事件之前执行的。您必须首先从中删除Style=“{StaticResource Title}”,因为我认为这是导致崩溃的原因。然后,您可以在加载的事件中手动将ChildWindow的样式设置为标题资源。(如果这不是导致崩溃的原因,你能提供更多的代码吗?)。你说得对。我应该多花几秒钟考虑你的解决方案。我希望能找到一个没有代码隐藏的解决方案。从中吸取的教训是,避免代码背后的任何东西都会让我变得不灵活。谢谢