You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

XAML设计器报错:无法定位自定义ToggleButton资源,求解决

问题描述

在WPF项目中自定义了ToggleButtonOnOff控件,自动生成Themes/Generic.xaml并引用控件资源字典ToggleButtonResource.xaml。控件运行时正常,但在XAML设计器中添加该控件时,报错:

Cannot locate resource 'resources/customcontrols/togglebuttonresource.xaml'.

尝试将资源字典添加到Control.Resources或直接引用Generic.xaml均无效,设计器无法正常显示控件。

相关代码与结构

视图控件代码(PAStatusAndControlView.xaml)

<Controls:MetroContentControl
             x:Class="PATestGUI.AHPA.PAStatusAndControl.Views.PAStatusAndControlView"
             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:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
             xmlns:cal="http://www.caliburnproject.org" 
             xmlns:cc="clr-namespace:PATestGUI.Resources.CustomControls"
             xmlns:resources="clr-namespace:PATestGUI.Resources"
             mc:Ignorable="d" HorizontalAlignment="Center" VerticalAlignment="Top" HorizontalContentAlignment="Center" VerticalContentAlignment="Top" FontFamily="Calibri" BorderThickness="1">
    <Control.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/Themes/Generic.xaml" />
            </ResourceDictionary.MergedDictionaries>
            <Style .../> <!-- 其他样式 -->
       </ResourceDictionary>
    </Control.Resources>
    <Grid>
        <cc:ToggleButtonOnOff />
    </Grid>
</Controls:MetroContentControl>

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="pack://application:,,,/Resources/CustomControls/ToggleButtonResource.xaml" />
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

ToggleButtonOnOff.cs核心代码

public class ToggleButtonOnOff : ToggleButton
{
    static ToggleButtonOnOff()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(ToggleButtonOnOff), new FrameworkPropertyMetadata(typeof(ToggleButtonOnOff)));
    }

    // 依赖属性定义省略
}

ToggleButtonResource.xaml核心代码

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:PATestGUI.Resources.CustomControls">
    <Style TargetType="{x:Type local:ToggleButtonOnOff}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:ToggleButtonOnOff}">
                    <!-- 控件模板内容省略 -->
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

项目结构

所有文件位于同一项目内:

  • PATestGUI
    • Resources
      • CustomControls
        • ToggleButtonOnOff.cs
        • ToggleButtonResource.xaml
    • Themes
      • Generic.xaml
    • AHPA
      • PAStatusAndControl
        • Views
          • PAStatusAndControlView.xaml
解决方案

按以下步骤排查修复:

1. 检查资源文件生成属性

确保ToggleButtonResource.xaml和Generic.xaml的生成操作设置为Resource:

  • 右键文件 → 属性
  • 生成操作改为Resource,复制到输出目录设为不复制

2. 修正资源路径引用

设计器对pack URI解析存在大小写敏感或稳定性问题,改用相对路径或明确程序集的pack URI:

  • 相对路径方案(修改Generic.xaml):
    <ResourceDictionary Source="../Resources/CustomControls/ToggleButtonResource.xaml" />
    
  • 明确程序集方案(项目名称为PATestGUI):
    <ResourceDictionary Source="pack://application:,,,/PATestGUI;component/Resources/CustomControls/ToggleButtonResource.xaml" />
    

3. 移除视图中多余的Generic.xaml引用

自定义控件会通过DefaultStyleKey自动加载Themes/Generic.xaml,无需在视图的Control.Resources中重复引用,删除如下代码:

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="pack://application:,,,/Themes/Generic.xaml" />
</ResourceDictionary.MergedDictionaries>

4. 修复模板绑定拼写错误

ToggleBtnBase样式中存在属性名拼写错误,修正后重新编译:

<!-- 原错误:BordderThickness -->
<Setter Property="BorderThickness" Value="{Binding BorderThickness, RelativeSource={RelativeSource TemplatedParent}}" />

5. 清理重建项目

  • 执行清理解决方案(Build → Clean Solution)
  • 执行重建解决方案(Build → Rebuild Solution)
  • 关闭并重新打开XAML设计器,或重启Visual Studio

内容的提问来源于stack exchange,提问作者zynovij

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 22:35:56