3

我有一种情况,SolidColorBrush(在App.xaml中定义)在运行时无法解析,当我在样式中使用Brush作为StaticResource时。StaticResource not found

在设计时(使用Visual Studio 2010)找到画笔,因为当我更改画笔的颜色时,具有样式的UIElement用新颜色更新。

运行时期间引发XAMLParseException,找不到资源“color”。

这是正常行为吗?我认为StaticResource的解决方案从UIElements开始直到应用程序资源,并且应用程序资源是为应用程序的UIElements定义默认值(颜色,字体等)的好地方。

的App.xaml

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     x:Class="SilverlightApplication1.App" 
     > 
<Application.Resources> 
    <ResourceDictionary> 
     <SolidColorBrush Color="Green" x:Key="color"/> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="Styles.xaml"/> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Application.Resources> 

Styles.xaml

<ResourceDictionary 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 

<Style TargetType="Border"> 
    <Setter Property="BorderBrush" Value="{StaticResource color}" /> 
    <Setter Property="BorderThickness" Value="1" /> 
</Style> 

Main.xaml

<UserControl x:Class="SilverlightApplication1.MainPage" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d" 
d:DesignHeight="300" d:DesignWidth="400"> 

<Grid x:Name="LayoutRoot" Background="White"> 
    <Border Height="100" HorizontalAlignment="Left" Margin="130,146,0,0" Name="border1" VerticalAlignment="Top" Width="200" /> 
</Grid> 

+0

你有没有尝试添加的SolidColorBrush到Styles.XAML? – 2010-07-28 09:15:55

+0

@Ardman:这有效。但多数民众赞成在不希望我想在这里 – Jehof 2010-07-28 09:24:06

回答

2

我重构资源定义,并把的SolidColorBrush “色彩” 资源字典 “General.xaml”

我兼并了资源字典 “General.xaml” 在ResourceDictionary中的“伴奏的.xaml”。现在它没有任何问题。我认为这是与资源配合的方式。

General.xaml

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 

    <SolidColorBrush Color="Green" x:Key="color"/> 
</ResourceDictionary> 

Styles.xaml

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 

    <ResourceDictionary.MergedDictionaries> 
    <ResourceDictionary Source="General.xaml"/> 
    </ResourceDictionary.MergedDictionaries> 

    <Style TargetType="Border"> 
    <Setter Property="BorderBrush" Value="{StaticResource color}" /> 
    <Setter Property="BorderThickness" Value="1" /> 
    </Style> 
</ResourceDictionary> 
2

我最近有一些成功的将所有的样式合并到XAML资源字典,并在其合并规避这样的问题运行。在XAML中合并这些相同的字典并不能解决问题。

App.xaml.cs

public App() 
{ 
    if (DesignerProperties.IsInDesignTool == false) 
    { 
     this.Startup += this.Application_Startup; 
    } 
} 

private void Application_Startup(object sender, StartupEventArgs e) 
{   
    System.Uri uri = new System.Uri("/MRW.UI;component/Resources/Library.xaml", UriKind.RelativeOrAbsolute); 

    System.Windows.ResourceDictionary dictionary = new System.Windows.ResourceDictionary(); 
dictionary.Source = uri; 
    App.Current.Resources.MergedDictionaries.Add(dictionary); 
} 
+0

感谢您的答案。这也是我选择将新的ResourceDictionaries添加到ApplicationResources的方式,用于在运行时加载的模块 – Jehof 2012-05-04 05:46:30

+0

我最近发现的另一件事... – NigelTufnel 2012-05-04 18:14:58

+0

花费了太多的时间来编辑上述评论......另一件事是一个最近发现我的同事......你可以合并相同的字典在代码和XAML中没有错误。这将允许您在Visual Studio或Blend设计器中工作并解决上述问题。 – NigelTufnel 2012-05-04 18:21:02