即使引用了 System.Windows,VS2010 也找不到类型 ControlTemplate
我想在这里学习Silverlight,创建一个自定义控件模板,但是VS2010拒绝识别 ControlTemplate
输入标记代码,即使我引用了 System.Windows
和 < code>System.Windows.Controls 程序集(当项目基于标准 Silverlight 应用程序模板时,这是默认设置)。我正在尝试重新创建在另一个SO堆栈上看到的内容。
我尝试将此代码直接放入文件(即 ImageButton.xaml)中,而没有其他任何内容:
<ControlTemplate x:Key="ImageButtonTemplate">
<Image Source="{TemplateBinding Content}" />
</ControlTemplate>
I'm trying to learn Silverlight here, creating a custom control template, however VS2010 refuses to recognize the ControlTemplate
type in markup code, even though I have referenced the System.Windows
and System.Windows.Controls
assemblies (which is by default when basing the project on the standard Silverlight Application template). I'm trying to recreate this seen on another SO stack.
I've tried putting this code directly into a file (i.e. ImageButton.xaml) and nothing else:
<ControlTemplate x:Key="ImageButtonTemplate">
<Image Source="{TemplateBinding Content}" />
</ControlTemplate>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果不了解更多上下文,例如您将其放入什么类型的文件,以及 Visual Studio 中的确切错误是什么,则很难权威地回答这个问题。我想您会收到如下错误:
未找到类型“ControlTemplate”。验证您是否缺少程序集引用,并且所有引用的程序集均已构建。
或者可能:
属性“Content”不支持“ControlTemplate”类型的值
这些是由于放置模板放在错误的位置 - 例如,如果您创建一个新的
UserControl
(通过添加 -> 新项目)并删除文件的内容并粘贴到您的代码中,那么您将得到这个错误,因为 xaml 没有对ControlTemplate
的引用。放置
ControlTemplate
的最佳位置是可重用的位置,例如新的“资源字典”(同样,通过添加 -> 新项目 -> Silverlight 资源字典添加它),然后放置代码在
标记内。如果您想将其放置在
UserControl
中(第二个错误的来源),那么您应该将其添加到该控件的Resources
部分,例如:It's a bit hard to answer this question authoritatively without knowing a little more context, such as what type of file you are placing this in, and what the exact error is from Visual Studio. I imagine that you're getting an error such as:
The type 'ControlTemplate' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built.
or possibly:
Property 'Content' does not support values of type 'ControlTemplate'
These are caused by placing the template in the wrong place - for example, if you create a new
UserControl
(via Add -> New Item) and delete the contents of the file and paste in your code, then you will get this error, since the xaml has no references toControlTemplate
.The best place to put your
ControlTemplate
is somewhere reusable, such as a new "Resource Dictionary" (again, add it via Add -> New Item -> Silverlight Resource Dictionary) and then place your code inside the<ResourceDictionary ...></ResourceDictionary>
tags.If you want to place it within a
UserControl
(the source of the second error) then you should add it to theResources
section of that control, for example: