c# - 我的 Microsoft.Windows.Themes 程序集在哪里?

标签 c# .net wpf visual-studio xaml

我在 XAML 中编写了一个用户控件,在将其编译为 C# 后,生成的 C# 中有这一行:

using Microsoft.Windows.Themes;

现在抛出一个错误:

The type or namespace name 'Themes' does not exist in the namespace 'Microsoft.Windows' (are you missing an assembly reference?)

我想既然我不能影响编译后的 C# 的输出(没有什么可以从 XAML 中删除),我只是将其添加为引用。

但是,“添加引用”对话框不包含 Microsoft.Windows.Themes。这不是 .NET 的一部分吗?

这是导致此问题的示例 XAML 用户控件:

<UserControl x:Class="GG.UserControls.MainMenu"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">

    <StackPanel>
        <Menu VerticalAlignment="Top" Background="{x:Null}">
            ... // Just static menu items.
        </Menu>
    </StackPanel>

</UserControl>

及其背后的 C# 代码:

using System;
using System.Windows.Controls;

namespace GG.UserControls
{
    /// <summary>
    /// Interaction logic for MainMenu.xaml
    /// </summary>
    public partial class MainMenu : UserControl
    {
        public MainMenu()
        {
            InitializeComponent();
        }
    }
}

下面是编译的 C# 示例:

#pragma checksum "..\..\..\..\UserControls\MainMenu.xaml" "{406ea660-64cf-4c82-b6f0-42d48172a799}" "5A85C5B16565514165AD23641F944BC6"
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.17020
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using GG;
using GG.UserControls;
using Microsoft.Windows.Shell;
using Microsoft.Windows.Themes; // <---- WTF?
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Automation;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Effects;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Windows.Media.TextFormatting;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Shell;


namespace GG.UserControls {


    /// <summary>
    /// ChangesetHistory
    /// </summary>
    public partial class MainMenu : System.Windows.Controls.UserControl, System.Windows.Markup.IComponentConnector {

        private bool _contentLoaded;

        /// <summary>
        /// InitializeComponent
        /// </summary>
        [System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
        public void InitializeComponent() {
            if (_contentLoaded) {
                return;
            }
            _contentLoaded = true;
            System.Uri resourceLocater = new System.Uri("/GG;component/usercontrols/mainmenu.xaml", System.UriKind.Relative);

            #line 1 "..\..\..\..\UserControls\MainMenu.xaml"
            System.Windows.Application.LoadComponent(this, resourceLocater);

            #line default
            #line hidden
        }

        [System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
        [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
        [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Design", "CA1033:InterfaceMethodsShouldBeCallableByChildTypes")]
        [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]
        [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily")]
        void System.Windows.Markup.IComponentConnector.Connect(int connectionId, object target) {
            this._contentLoaded = true;
        }
    }
}

最佳答案

Microsoft.Windows.Themes 位于主题特定的 PresentationFramework 程序集中。根据您的 XAML 中引用的内容,您需要添加对以下内容之一的引用:

  • PresentationFramework.Aero.dll
  • PresentationFramework.AeroLite.dll
  • PresentationFramework.Classic.dll
  • PresentationFramework.Luna.dll
  • PresentationFramework.Royale.dll

关于c# - 我的 Microsoft.Windows.Themes 程序集在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9657672/

相关文章:

c# - 使用 C# 在 Excel 中合并单元格

c# - LINQ to Entities 无法识别方法 'Boolean Contains [Int32]

c# - 在 C# 中使用通配符解析相对路径

.net - 电子邮件地址域是否需要点?

wpf - 在XAML风格中,如何将纯色背景更改为渐变?

c# - 设置整个窗口的前景色

c# - 使用 IConverter 处理 WPF/XAML/MVVM 中的 {NewItemPlaceholder}

C# 在你的 winforms 中嵌入 youtube/videos

c# - C++中的Singleton Service类

c# - 如何在前瞻中匹配字符串的结尾?