StaticResource не найден - PullRequest
       40

StaticResource не найден

3 голосов
/ 28 июля 2010

У меня есть ситуация, когда SolidColorBrush (определенный в App.xaml) не может быть разрешен во время выполнения, когда я использую кисть в стиле как StaticResource.

Во время разработки (с помощью 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>

Ответы [ 2 ]

2 голосов
/ 03 мая 2012

Недавно я добился некоторого успеха, обходя подобные проблемы, помещая все стили в словари ресурсов 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);
}
2 голосов
/ 01 сентября 2010

Я реорганизовал определения ресурсов и поместил SolidColorBrush "Color" в ResourceDictionary "General.xaml"

Я объединил ResourceDictionary "General.xaml" в ResourceDictionary "Styles.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>
...