代码之家  ›  专栏  ›  技术社区  ›  mrid

c wpf以xaml样式指定文件夹

  •  0
  • mrid  · 技术社区  · 6 年前

    我有一个具有以下结构的项目:

    Proj
    ├──Views
    ├   ├──Dashboard.xaml
    ├   ├──Dashboard.cs
    ├
    ├──Styles
        ├──DashboardStyle.xaml
    

    在我的 DashboardStyle.xaml ,我有以下代码:

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:Proj.Styles">
    
        <Style x:Key="MyWindowStyle" TargetType="local:Proj/Views/Dashboard">
            ....
            ....
        </Style>
    
    </ResourceDictionary>
    

    但它给出了错误:

    命名空间“clr namespace:proj.styles”中不存在名称“proj/views/dashboard”

    如何解决此问题?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Klaus Gütter    6 年前

    类型是使用名称空间和类型名称引用的,而不是通过物理文件路径引用的。

    所以要引用类型 Proj.Views.Dashboard ,添加相应的命名空间作为XML命名空间声明,并在targetType属性中使用它,例如

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:Proj.Styles"
                    xmlns:views="clr-namespace:Proj.Views" >
    
        <Style x:Key="MyWindowStyle" TargetType="views:Dashboard">
            ....
            ....
        </Style>
    
    </ResourceDictionary>