简体   繁体   中英

WPF Class Library with custom control - control won't show

After a couple of hours searching the web, I turn to you all:

My WPF class library (.NET 3.5, COM visible) has a form in it, which uses an UserControl and a theme file.

The problem: When using WPF Applications, the buttons work just fine, but in this .NET 3.5, COM Visible, class library, they won't show at all. Other objects from the library can be used and work. What could be the problem? I'm leaning towards the resource dictionary that can't be found, or some of the resources that cannot be found, but I can't put my finger on it. Any help would be most welcome!

Some code

The resources are set via the

<Window.Resources>
   <ResourceDictionary>
       <ResourceDictionary.MergedDictionaries>
          <ResourceDictionary  Source="pack://application:,,,/MyLibrary;component/Themes/Generic.xaml"/>
       </ResourceDictionary.MergedDictionaries>
   </ResourceDictionary>
</Window.Resources>

There we place - for now - a simple:

<Grid>
   <lib:ImageButton ImageSource="/MyLibrary;component/Images/some_image.png" />
</Grid>

Not forgetting the reference for the library in the project and the xamls:

xmlns:lib="clr-namespace:MyLibrary;assembly=MyLibrary"

The MyLibrary holds this ImageButton - a simple button extension mainly to hold an image. The WPF looks somewhat like:

<UserControl x:Class="MyLibrary.ImageButton"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:lib="clr-namespace:MyLibrary"
         x:Name="me"
         Width="auto"
         Height="22"
         HorizontalAlignment="Center" VerticalAlignment="Center">

<UserControl.Resources>
    <ResourceDictionary>
        <Style TargetType="{x:Type library:ImageButton}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=me, Path=HasText}" Value="False">
                    <Setter Property="Width" Value="22" />
                </DataTrigger>
                <DataTrigger Binding="{Binding ElementName=me, Path=HasText}" Value="True">
                    <Setter Property="Width" Value="auto" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ResourceDictionary>
</UserControl.Resources>

<Button x:Name="_button" Click="Button_Click" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Width="auto">
    <StackPanel Orientation="Horizontal">
        <Image Source="{Binding ElementName=me, Path=ImageSource}" Stretch="Uniform" />
        <TextBlock x:Name="_Text" Text="{Binding ElementName=me, Path=Text}" VerticalAlignment="Center" Margin="2, 0, 0, 0">
            <TextBlock.Resources>
                <Style TargetType="{x:Type TextBlock}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding ElementName=me, Path=HasText}" Value="False">
                            <Setter Property="Visibility" Value="Collapsed" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding ElementName=me, Path=HasText}" Value="True">
                            <Setter Property="Visibility" Value="Visible" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBlock.Resources>
        </TextBlock>
    </StackPanel>
</Button>
</UserControl>

The .cs side is fairly arbitrary I guess.

Finally, we have Generic.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/MyLibrary;component/Themes/MyTheme.xaml" />
    </ResourceDictionary.MergedDictionaries>

</ResourceDictionary>

Where "MyTheme" holds lots of templates, colours etc..

The problem most likely stems from the fact that your Class Library project has no idea that there are resource dictionaries you want to consume everywhere even though you merged your resource dictionaries in some file . The reason for this is that Visual Studio and Blend both look for an ApplicationDefinition file for global resource dictionaries to consume.

Class Library projects cannot have ApplicationDefinition files. However, there is a way you can share resources for design time viewing of your custom control project. A question on that topic and the answer can be found here (disclaimer: the link currently points to my answer).

Please take note that if you use the solution linked to above, you will still have to reference your resource dictionaries at the App.xaml level of any Application project that consumes your class library as your class library will not contain the specific styling..

Well this question is rather old but here goes:

After doing a lot of comparison i verified that the way to get it working is setting the following lines somewhere in your class library on the assembly level:

[assembly: ThemeInfo(
    ResourceDictionaryLocation.None, 
    ResourceDictionaryLocation.SourceAssembly 
)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM