在我的情况下,SolidColorBrush (在App.xaml中定义)不能在运行时被解析,当我将画笔作为StaticResource在样式中使用时。
在设计期间(使用Visual 2010)可以找到画笔,因为当我更改画笔的颜色时,带有样式的UIElement将被更新为新的颜色。
在运行时引发XAMLParseException时,无法找到资源“颜色”。
这是正常的行为吗?我认为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>
发布于 2010-09-01 08:19:34
我重构了资源定义,并将SolidColorBrush“颜色”放在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>发布于 2012-05-03 19:22:54
最近,通过将所有样式放入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);
}https://stackoverflow.com/questions/3351414
复制相似问题