3

スタイルのブラシをStaticResourceとして使用すると、実行時にSolidColorBrush(App.xamlで定義)を解決できない状況があります。

設計時に(Visual Studio 2010を使用して)ブラシが見つかりました。ブラシの色を変更すると、スタイルのあるUIElementが新しい色で更新されます。

実行時にXAMLParseExceptionが発生し、リソース「color」が見つかりません。

これは正常な動作ですか?StaticResourceの解決は、UIElementsからApplicationリソースまでであり、Applicationリソースは、アプリケーションの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>

4

2 に答える 2

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);
}
于 2012-05-03T19:22:54.433 に答える
2

リソース定義をリファクタリングし、SolidColorBrush の「Color」を ResourceDictionary の「General.xaml」に配置しました。

ResourceDictionary "Styles.xaml" に ResourceDictionary "General.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>

スタイル.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>
于 2010-09-01T08:19:34.270 に答える