2

我按照教程制作了自定义控件。我基本上做的是创建一个新项目,添加一个文件CategoryBar.cs和一个Themes用文件调用的目录Themes\generic.xaml(编译类型设置为“资源”)。然后我写了一个类CategoryBar.csgeneric.xaml用一个 ResourceDictionary 填满。让我们将此项目称为“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>

在这里,我在其中创建了一个类PaginationBarPaginationBar.cs并且全部设置完毕,但是当我尝试在我的应用程序的 xaml 文件中使用它时,它在设计器视图中显示一个白色填充的矩形,左上角有一个十字,它说导致“Control_TargetTypeMismatch”异常。经过我的一些技巧之后,仍然没有任何效果,但是设计器在我使用时没有加载<UILib:PaginationBar .. />,而是给出了一个错误System.Reflection.TargetInvocationException(调用目标抛出了异常)。当我运行该项目时,它给出了一些 XamlParseException 错误。这是我能够从中获得一些细节的唯一例外,我认为这些细节都没有一点用处。无论如何,这就是我使用 XamlParseException 得到的:XamlParseException 详细信息

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

4

1 回答 1

1

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

 public PaginationBar ()
    {
        DefaultStyleKey = typeof(PaginationBar );
    }
于 2011-06-04T07:00:21.260 回答