2012-09-26 61 views
13

我试图在WPF UserControl Library项目中创建ResourceDictionary。当我添加以下样式:x:在用户控件库中找不到类型

<Style TargetType="{x:Type Button}"> 
    <Setter Property="Background" Value="{StaticResource ResourceKey=GreyBrush}"/> 
    <Setter Property="BorderBrush" Value="{StaticResource ResourceKey=LightBlueBrush}"/> 
    <Setter Property="BorderThickness" Value="1"/> 
    <Style.Triggers> 
     <Trigger Property="IsMouseOver" Value="True"> 
      <Setter Property="Background" Value="{StaticResource ResourceKey=OrangeBrush}"/> 
     </Trigger> 
     <EventTrigger RoutedEvent="Click"> 
      <BeginStoryboard> 
       <Storyboard> 
        <ColorAnimation Storyboard.TargetProperty="Background.Color" To="{StaticResource ResourceKey=LightOrange}" Duration="0:0:.1"/> 
       </Storyboard> 
      </BeginStoryboard> 
     </EventTrigger> 
    </Style.Triggers> 
</Style> 

我得到一个错误说:

The type 'x:Type' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built. 

我声明X为:

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 

这工作时,我里面创建一个资源字典WPF应用程序项目,但不在UserControl Library项目中。任何想法为什么?

+0

不知道为什么会发生,解决方法是只删除{x:Type}文本,即'TargetType =“Button”'。在Silverlight AFAIK中发生同样的错误。 – Patrick

+0

但并不总是会发生。至少上述样式适用于新创建的(.Net 4.0)WPF用户控件库项目中的资源字典。 – Clemens

+0

我正在使用VS2012 Professional并创建.Net 3.5 WPF用户控制库。 – FlyingStreudel

回答

31

我在编写IE扩展时想要创建WPF用户控件时发生了这种情况。由于该项目本来不是WPF项目,因此没有提及System.Xaml,增加了所述参考解决了该问题。

+1

这为我修好了!该项目进行了编译,但始终显示错误(特别是生产力动力工具扩展的解决方案资源管理器错误功能令人讨厌)。 – yourbuddypal

+1

遇到同样的问题/解决方案,但罪魁祸首是“System.Presentation”。 –

1

我不得不不同意,这是我从一个用户控件工作的decalaration。

<UserControl x:Class="RedGreenRefactor.View.TestResultsGraph" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 

有没有可能错误告诉你到底发生了什么问题?你有没有引用你需要的所有程序集?

创建一个新的WPF应用程序我得到以下内容。

WPF default references

+0

这是3.5或4.0库吗?我猜测4.0作为Microsoft.CSharp和System.Xaml不是3。5 – FlyingStreudel

+0

4.5实际上,但你总是可以创建一个新的空wpf并比较任何版本 – AlSki

+0

wpf应用程序项目和控制库之间的引用是相同的。 – FlyingStreudel

-1

是否缺少根

<ResourceDictionary xmlns="..." 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 

,即你在哪里定义X?除此之外,

<Style TargetType="Button"> 

也有效。

2

在我的项目中遇到同样的问题。我已经通过将Target Framework从.NET 3.0切换到4.0来解决这个问题。

相关问题