wpf x:在用户控件库中找不到类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12606304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 05:33:39  来源:igfitidea点击:

x:Type not found in user control library

c#wpfuser-controlsresourcedictionary

提问by FlyingStreudel

I'm trying to create a ResourceDictionaryinside a WPF UserControl Library project. When I add the following style:

我正在尝试ResourceDictionary在 WPF UserControl 库项目中创建一个。当我添加以下样式时:

<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>

I get an error saying:

我收到一条错误消息:

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

I am declaring x as:

我声明 x 为:

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

This works when I create a resource dictionary inside a WPF application project, but not inside a UserControl Library project. Any idea why?

这在我在 WPF 应用程序项目中创建资源字典时有效,但在 UserControl 库项目中无效。知道为什么吗?

回答by kroolk

This happened to me when I was writing an IE Extension and wanted to create WPF User Controls. Since the project was not originally a WPF project there was no reference to System.Xaml, adding said reference fixed the issue.

当我编写 IE 扩展并想要创建 WPF 用户控件时,这发生在我身上。由于该项目最初不是 WPF 项目,因此没有对System.Xaml 的引用,添加所述引用解决了该问题。

回答by pizycki

Had the same looking issue in my project. I've solved it by switching Target Framework from .NET 3.0 to 4.0.

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

回答by AlSki

I have to disagree, here's my decalaration from a UserControl that does work.

我不得不不同意,这是我从一个有效的 UserControl 的声明。

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

Is there any chance that the error is telling you exactly what is wrong? Have you got all the assemblies referenced that you need?

错误是否有可能告诉您究竟出了什么问题?你有没有引用你需要的所有程序集?

Creating a new WPF application I get the following.

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

WPF default references

WPF 默认引用

回答by gerd

Are you missing the root

你缺根吗

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

, i.e. where do you define x? Apart from that

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

<Style TargetType="Button">

also works.

也有效。