在 WP7 Silverlight 中制作多个自定义控件会出现奇怪的异常

发布于 2024-11-11 18:40:57 字数 3866 浏览 6 评论 0原文

我按照教程制作了自定义控件。我基本上所做的是创建一个新项目,添加一个文件 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 得到的结果:XamlParseExceptionDetails

我不知道如何继续。非常感谢任何帮助。谢谢期待:)

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:XamlParseExceptionDetails

I have no clue how to proceed. Any help is greatly appreciated. Thanks in anticipation :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一百个冬季 2024-11-18 18:40:57

验证 PaginationBar 是否在同一命名空间中定义:“clr-namespace:ErnestUILib”。还要验证您是否在控件的构造函数中设置了正确的 DefaultStyleKey:

 public PaginationBar ()
    {
        DefaultStyleKey = typeof(PaginationBar );
    }

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:

 public PaginationBar ()
    {
        DefaultStyleKey = typeof(PaginationBar );
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文