【问题标题】:Making multipe custom controls in WP7 Silverlight gives Weird exceptions在 WP7 Silverlight 中制作多个自定义控件会出现奇怪的异常
【发布时间】: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" 添加到&lt;phone:PhoneApplicationPage .. /&gt; 并且它工作正常。现在,我想实现另一个控件(因为我希望有一个单独的并且完全是一个用于自定义 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”引起的。经过我的一些技巧之后,仍然没有任何效果,但是当我使用 &lt;UILib:PaginationBar .. /&gt; 时设计器没有加载,而是给出了错误 System.Reflection.TargetInvocationException (调用目标引发了异常)。当我运行该项目时,它给出了一些 XamlParseException 错误。这是我能够从中获得一些细节的唯一例外,我认为这些细节都没有一点用处。无论如何,这就是我通过 XamlParseException 得到的:

我不知道如何继续。任何帮助是极大的赞赏。感谢期待:)

【问题讨论】:

    标签: .net silverlight xaml windows-phone-7 xamlparseexception


    【解决方案1】:

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

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

    【讨论】:

    • 实际上由于我输入文本的方式有问题或者天知道是什么问题,问题没有正确显示。我实际上提出了两个不同的代码集。在第二部分中,我确实提到了 TargetType 为“local:PaginationBar”。非常感谢您的回复,但是既然我已经正确编辑了我的问题,我可以打扰您重新阅读我的问题吗?我对此感到抱歉:)
    • 非常感谢!!事实证明,由于我是从我之前制作的控件中复制粘贴代码,所以我忘了更改这部分。 P.S:我已经按照您是联合创始人的博客中的教程制作了两个控件。干得好!干杯:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-16
    • 2014-12-21
    • 1970-01-01
    • 1970-01-01
    • 2015-06-26
    相关资源
    最近更新 更多