首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >StaticResource未找到

StaticResource未找到
EN

Stack Overflow用户
提问于 2010-07-28 09:11:43
回答 2查看 4.3K关注 0票数 3

在我的情况下,SolidColorBrush (在App.xaml中定义)不能在运行时被解析,当我将画笔作为StaticResource在样式中使用时。

在设计期间(使用Visual 2010)可以找到画笔,因为当我更改画笔的颜色时,带有样式的UIElement将被更新为新的颜色。

在运行时引发XAMLParseException时,无法找到资源“颜色”。

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

App.xaml

代码语言:javascript
复制
<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

代码语言:javascript
复制
<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

代码语言:javascript
复制
<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>

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-09-01 08:19:34

我重构了资源定义,并将SolidColorBrush“颜色”放在ResourceDictionary "General.xaml“中。

我将ResourceDictionary "General.xaml“合并到ResourceDictionary "Styles.xaml”中。现在它的工作没有任何问题。我认为这是使用资源的方式。

General.xaml

代码语言:javascript
复制
<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

代码语言:javascript
复制
<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
EN

Stack Overflow用户

发布于 2012-05-03 19:22:54

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

App.xaml.cs

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3351414

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档