【发布时间】:2011-06-04 06:50:38
【问题描述】:
我按照教程制作了自定义控件。我基本上做的是创建一个新项目,添加一个文件CategoryBar.cs 和一个名为Themes 的目录和一个文件Themes\generic.xaml(编译类型设置为'resource')。然后我写了一个类CategoryBar.cs,用ResourceDictionary 填充generic.xaml。让我们将此项目称为“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,0,0,8">
<!-- The grid rowdefs, coldefs and whatever makes up the grid -->
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
在我添加对该库的引用的项目中,这一切都运行得很好。我将属性xmlns:EULib="clr-namespace:UILib;assembly=UILib" 添加到<phone:PhoneApplicationPage .. /> 并且它工作正常。现在,我想实现另一个控件(因为我希望有一个单独的并且完全是一个用于自定义 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,0,0,8">
<!-- The grid rowdefs, 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,0,0,8">
<!-- The grid rowdefs, coldefs and whatever makes up the grid -->
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
在这里,我在PaginationBar.cs 中创建了一个类PaginationBar 并且已全部设置好,但是当我尝试在我的应用程序的xaml 文件中使用它时,它在设计器视图中显示了一个带有十字的白色填充矩形在它的左上角,它说异常是由“Control_TargetTypeMismatch”引起的。经过我的一些技巧之后,仍然没有任何效果,但是当我使用 <UILib:PaginationBar .. /> 时设计器没有加载,而是给出了错误 System.Reflection.TargetInvocationException (调用目标引发了异常)。当我运行该项目时,它给出了一些 XamlParseException 错误。这是我能够从中获得一些细节的唯一例外,我认为这些细节都没有一点用处。无论如何,这就是我通过 XamlParseException 得到的:
我不知道如何继续。任何帮助是极大的赞赏。感谢期待:)
【问题讨论】:
标签: .net silverlight xaml windows-phone-7 xamlparseexception