开发者

Making multipe custom controls in WP7 Silverlight gives Weird exceptions

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>
  开发者_StackOverflow  </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:

Making multipe custom controls in WP7 Silverlight gives Weird exceptions

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


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 );
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜