代码之家  ›  专栏  ›  技术社区  ›  Jehof

找不到StaticResource

  •  3
  • Jehof  · 技术社区  · 15 年前

    我遇到这样的情况:当我以样式使用solidColorBrush作为staticResource时,在运行时无法解析solidColorBrush(在app.xaml中定义)。

    在设计期间(使用Visual Studio 2010),会发现画笔,因为当我更改画笔的颜色时,样式为的ui元素将更新为新颜色。

    在运行时引发xamlparseException,但找不到资源“color”。

    这是正常行为吗?我认为静态资源的解析,从ui元素开始到应用程序资源,并且应用程序资源是为应用程序的ui元素定义默认值(颜色、字体等)的好地方。

    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>
    

    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>
    

    MAM.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 回复  |  直到 13 年前
        1
  •  2
  •   Jehof    14 年前

    我重构了资源定义,并将solidColorBrush“color”放在资源字典“general.xaml”中。

    我将resourcedictionary“general.xaml”合并到resourcedictionary“styles.xaml”中。现在它可以毫无问题地工作了。我认为这是利用资源的方法。

    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>
    
        2
  •  2
  •   NigelTufnel    13 年前

    最近,我通过将所有样式放入XAML资源字典并在运行时将它们合并,成功地绕过了这些问题。在XAML中合并这些相同的字典并不能解决这个问题。

    代码文件

    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);
    }
    
    推荐文章