问题描述
||
我按照教程制作了自定义控件。我基本上所做的就是创建一个新项目,添加文件
CategoryBar.cs
和目录Themes
并添加文件directory2ѭ(Compile类型设置为\'resource \')。然后,我写了一个类CategoryBar.cs
,并用ResourceDictionary填充了ѭ4class。让我们将此项目称为\'UILib \':
<?xml version=\"1.0\" encoding=\"utf-8\" ?>
<ResourceDictionary xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"
xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\"
xmlns:vsm=\"clr-namespace:System.Windows;assembly=System.Windows\"
xmlns:local=\"clr-namespace:ErnestUILib\">
<Style targettype=\"local:CategoryBar\">
<Setter Property=\"Background\" Value=\"Black\" />
<Setter Property=\"Template\">
<Setter.Value>
<ControlTemplate targettype=\"local:CategoryBar\">
<Grid x:Name=\"GridView\" Background=\"{TemplateBinding Background}\" Margin=\"0,8\">
<!-- The grid rowdefs,coldefs and whatever makes up the grid -->
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
在我向该库添加引用的项目中,这一切都可以很好地运行。我在<phone:PhoneApplicationPage .. />
上添加了属性xmlns:EULib=\"clr-namespace:UILib;assembly=UILib\"
,它工作正常。现在,我想实现另一个控件(因为我想为自定义UI控件提供一个单独且恰好一个的库)。所以现在我的generic.xaml看起来像:
<?xml version=\"1.0\" encoding=\"utf-8\" ?>
<ResourceDictionary xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"
xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\"
xmlns:vsm=\"clr-namespace:System.Windows;assembly=System.Windows\"
xmlns:local=\"clr-namespace:ErnestUILib\">
<!-- THE NEW CUSTOM CONTROL -->
<Style targettype=\"local:PaginationBar\">
<Setter Property=\"Background\" Value=\"Black\" />
<Setter Property=\"Template\">
<Setter.Value>
<ControlTemplate targettype=\"local:PaginationBar\">
<Grid x:Name=\"GridView\" Background=\"{TemplateBinding Background}\" Margin=\"0,coldefs and whatever makes up the grid -->
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!-- THE PREVIoUS CUSTOM CONTROL -->
<Style targettype=\"local:CategoryBar\">
<Setter Property=\"Background\" Value=\"Black\" />
<Setter Property=\"Template\">
<Setter.Value>
<ControlTemplate targettype=\"local:CategoryBar\">
<Grid x:Name=\"GridView\" Background=\"{TemplateBinding Background}\" Margin=\"0,coldefs and whatever makes up the grid -->
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
在这里,我已经在PaginationBar.cs
中创建了一个PaginationBar
类,并且已完成所有设置,但是当我尝试在我的应用程序的xaml文件中使用它时,它在设计器视图中显示了一个白色填充的矩形,带有一个十字在它的左上角,它说是引起了'Control_targettypeMismatch \'异常。经过几次欺骗之后,仍然没有任何效果,但是当我使用<UILib:PaginationBar .. />
时,Designer只是没有加载,而是给出了错误System.Reflection.TargetInvocationException
(调用的目标抛出了异常)。当我运行项目时,它会出现一些XamlParseException错误。这是我能够获得一些细节的唯一例外,我认为这些细节都没有用。无论如何,这就是XamlParseException带来的结果:
我不知道如何进行。任何帮助是极大的赞赏。谢谢期待:)
解决方法
验证是否在同一名称空间中定义了PaginationBar:\“ clr-namespace:ErnestUILib \”。还要验证您是否在控件的构造函数中设置了正确的DefaultStyleKey:
public PaginationBar ()
{
DefaultStyleKey = typeof(PaginationBar );
}