none
WPF如何调用不同类库之间的文件夹呢?比如:A类库中的Resources文件夹,B类库如何访问A类库中的Resources文件夹呢? RRS feed

  • 问题

  • WPF如何调用不同类库之间的文件夹呢?比如:A类库中的Resources文件夹,B类库如何访问A类库中的Resources文件夹呢?
    2020年8月25日 9:31

答案

  • 你好,

    用法是一样的,比如我在A/Resources中放入一张图片名字为Myico.ico,在B中的UserControl使用:

     <Image Source="pack://application:,,,/A;component/Resources/Myico.ico" Width="100" ></Image>

    谢谢

    Daisy Tian


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 Marry_qing 2020年8月27日 2:32
    2020年8月27日 1:29

全部回复

  • 你好,

    先建两个类库,名字分别为A和B,如图所示:

    其中Dictionary_A.xaml的代码为:

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        xmlns:local="clr-namespace:A">
        
        <Style x:Key="StyleA" TargetType="{x:Type Button}">
            <Setter Property="Background" Value="Red"/>
        </Style>
    </ResourceDictionary>

    Dictionary_B.xaml的代码为:

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        xmlns:local="clr-namespace:B">
        <Style x:Key="StyleB" TargetType="{x:Type Button}">
            <Setter Property="Background" Value="LightYellow"/>
        </Style>
    </ResourceDictionary>

    在类库B中的reference中引入A,UserControl_B.xaml的代码为:

    <UserControl x:Class="B.UserControl_B"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 xmlns:local="clr-namespace:B"
                 mc:Ignorable="d" 
                 d:DesignHeight="450" d:DesignWidth="800">
        <UserControl.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="pack://application:,,,/A;component/Dictionary_A.xaml"/>
                    <ResourceDictionary Source="/B;Component/Dictionary_B.xaml"/>
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </UserControl.Resources>
        <StackPanel>
            <Button x:Name="button_A" Content="Use Dictionary_A" Style="{StaticResource StyleA}" Width="150" Height="30"></Button>
            <Button x:Name="button_B" Content="Use Dictionary_B" Style="{StaticResource StyleB}" Width="150" Height="30"></Button>
        </StackPanel>
    </UserControl>

    谢谢

    Daisy Tian


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2020年8月26日 2:52
  • 非常感谢您的回复,谢谢!

    如果A类中有个Resources文件夹的话,在B类中如何访问到这个文件夹呢?我的目的是想访问到这个Resources文件夹,谢谢!

    2020年8月26日 3:21
  • 你好,

    修改pack的uri就可以:

      <ResourceDictionary Source="pack://application:,,,/A;component/Resources/Dictionary_A.xaml"/>

    谢谢

    Daisy Tian


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2020年8月26日 5:28
  • 你好,

    用法是一样的,比如我在A/Resources中放入一张图片名字为Myico.ico,在B中的UserControl使用:

     <Image Source="pack://application:,,,/A;component/Resources/Myico.ico" Width="100" ></Image>

    谢谢

    Daisy Tian


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 Marry_qing 2020年8月27日 2:32
    2020年8月27日 1:29