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