【发布时间】:2014-10-17 17:11:10
【问题描述】:
我正在开发一个自定义控件,这是我目前所拥有的:
主题/Generic.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Themes/ColorPicker.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
控件/ColorPicker.cs
namespace TrigDebugUtil.Controls
{
public class ColorPicker : Control
{
#region Private Fields
#endregion //Private Fields
#region Properties
public BitmapImage ColorWheelImage
{
get;
private set;
}
#endregion //Properties
static ColorPicker()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ColorPicker), new FrameworkPropertyMetadata(typeof(ColorPicker)));
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
WriteableBitmap ColorDrawboard;
}
}
}
主题/ColorPicker.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TrigDebugUtil.Controls">
<Style TargetType="{x:Type local:ColorPicker}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:ColorPicker}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<!-- <Image Source="{TemplateBinding ColorWheelImage}" /> -->
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
App.xaml
<Application x:Class="TrigDebugUtil.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cont="clr-namespace:TrigDebugUtil.Controls"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Themes/Generic.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style TargetType="{x:Type cont:ColorPicker}" BasedOn="{StaticResource ColorPicker}" />
</ResourceDictionary>
</Application.Resources>
</Application>
我现在收到十几个错误,都在说:
命名空间“TrigDebugUtil.Controls”中不存在类型或命名空间“ColorPicker”
搜索资源字典“Themes/Generic.xaml”时出错
如果我用
替换有问题的行pack://application:,,,/Trig.DebugUtil;component/Themes/ColorPicker.xaml
错误被此项目未引用的警告“Trig.DebugUtil”程序集替换(免费翻译)
不管错误如何,项目似乎都可以编译(控制台输出中没有错误消息,只是在错误列表中)
【问题讨论】:
-
App.xaml和ColorPicker控件是位于同一个程序集下还是不同程序集下? -
两个文件是同一个项目的一部分,因此它应该是同一个程序集。但是我应该注意,解决方案中还有其他项目(但是没有引用它们)并且我在项目的属性和 Assembly.cs 中更改了程序集名称
-
由于您已经在单独的 xaml 中为 ColorPicker
ColorPicker.xaml声明了模板,因此您需要将其合并到您的应用资源下。尝试合并或将默认模板移动到Generic.xaml下。 -
我实际上将模板从 Generic.xaml 移到了一个单独的文件中(之前的问题几乎相同)。在 Generic.xaml 中包含 ColorPicker.xaml,在 app.xaml 中包含 generic.xaml 还不够吗?
-
试试
BasedOn="{StaticResource {x:Type cont:ColorPicker}}",看看能不能解决你的问题。
标签: c# wpf visual-studio xaml visual-studio-2013