2012-09-26 64 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來解決這個問題。

相關問題