2010-08-13 46 views
0

我正在尝试为我的测试Silverlight应用程序创建一个Syles.xaml文件。以下是我在App.xaml文件:Silverlight中的XAML样式不被识别

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation" 
      xmlns:uriMapper="clr-namespace:System.Windows.Navigation;assembly=System.Windows.Controls.Navigation" 
      x:Class="MVCSilverlight.App" 
      > 
    <Application.Resources> 
     <Style x:Key="NavigationContainerStyle" TargetType="StackPanel"> 
      <Setter Property="Background" Value="Black" /> 
      <Setter Property="Orientation" Value="Horizontal" /> 
      <Setter Property="Height" Value="50" /> 
      <Setter Property="Width" Value="500" /> 
     </Style> 
    </Application.Resources> 
</Application> 

的问题是,当我包括在这个应用程序,VS2010不承认它,当我运行应用程序,它不会显示,因为有尝试查找资源名称/值时出错。下面是如何被使用它的一个例子:

<StackPanel Style="{StaticResource NavigationContainerStyle}"> 
</StackPanel> 

我也试图把样式文件,包括它在App.xaml中,但没有工作要么。

有人可以给我一些想法,为什么发生这种情况?

+1

上面的代码示例“应该”按原样正常工作。你看到什么错误? – Scrappydog 2010-08-13 14:49:59

回答

2

只要App仍然被设置为项目设置中的启动对象,并且InitializeComponent()仍然在App.xaml.c中被调用,XAML看起来应该可以正常工作。

如果您将样式放入Styles.xaml文件中,则需要使用merged resource dictionary将其合并到App资源中,或直接将其合并到要使用它的UserControl资源中。

<Application.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="Styles.xaml"/> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Application.Resources>