在 WP7 Silverlight 中制作多个自定义控件会出现奇怪的异常
我按照教程制作了自定义控件。我基本上所做的是创建一个新项目,添加一个文件 CategoryBar.cs
和一个名为 Themes
的目录,其中包含一个文件 Themes\generic.xaml
(编译类型设置为“资源”)。然后我编写了一个类 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"
添加到
并且工作正常。现在,我想实现另一个控件(因为我想要为自定义 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”。经过我的一些技巧之后,仍然没有任何效果,但是当我使用
时,设计器不会加载,而是给出错误 System.Reflection .TargetInitationException
(调用目标已引发异常)。当我运行该项目时,它给出了一些 XamlParseException 错误。这是我能够获得一些细节的唯一例外,我认为这些细节都没有一点用处。无论如何,这就是我通过 XamlParseException 得到的结果:
我不知道如何继续。非常感谢任何帮助。谢谢期待:)
I followed a tutorial to make a Custom Control. What I basically did was make a new project, add a file CategoryBar.cs
and a directory called Themes
with a file Themes\generic.xaml
(with Compile type set to 'resource'). Then I wrote a class CategoryBar.cs
, filled up the generic.xaml
with a ResourceDictionary. Let's call this project the '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>
And it all runs completely fine in a project where I add a reference to this library. I added the attribute xmlns:EULib="clr-namespace:UILib;assembly=UILib"
to <phone:PhoneApplicationPage .. />
and it's working fine. Now, I wanted to implement another control (as I want to have one separate and exactly one library for custom UI controls). So now my generic.xaml looks like:
<?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>
Here, I've created a class PaginationBar
in PaginationBar.cs
and it's all setup, but when I try to use it in my application's xaml file, it shows a white-filled rectangle in the designer view with a cross at it's top left corner and it says that an exception was caused 'Control_TargetTypeMismatch'. After a few trickeries of mine, nothing still worked, but the Designer just doesn't load when I use <UILib:PaginationBar .. />
and instead gives an error System.Reflection.TargetInvocationException
(Exception has been thrown by the target of an invocation). When I run the project, it gives some XamlParseException error. This is the only exception I'm able to get some details out of, none of which I think are even remotely useful. Anyhow, this is what I get with the XamlParseException:
I have no clue how to proceed. Any help is greatly appreciated. Thanks in anticipation :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
验证 PaginationBar 是否在同一命名空间中定义:“clr-namespace:ErnestUILib”。还要验证您是否在控件的构造函数中设置了正确的 DefaultStyleKey:
Verify that PaginationBar is define in the same namespace: "clr-namespace:ErnestUILib". Also verify that you have set the right DefaultStyleKey in the constructor of your control: