C# WPF 以 XAML 样式指定文件夹

C# WPF specify folders in XAML style

我有一个结构如下的项目:

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>

但是报错:

The name "Proj/Views/Dashboard" does not exist in the namespace "clr-namespace:Proj.Styles"

我该如何解决这个问题?

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

因此要引用类型 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>