【问题标题】:Where is my Microsoft.Windows.Themes assembly?我的 Microsoft.Windows.Themes 程序集在哪里?
【发布时间】:2012-03-28 07:08:55
【问题描述】:

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

using Microsoft.Windows.Themes;

现在会引发错误:

命名空间中不存在类型或命名空间名称“主题” 'Microsoft.Windows'(您是否缺少程序集引用?)

我想既然我不能影响编译后的 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;
        }
    }
}

【问题讨论】:

  • 我正在使用反射器在 PresentationFramework.dll 下搜索,我似乎无法找到任何主题 .dll 的物理位置?

标签: c# .net wpf visual-studio xaml


【解决方案1】:

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

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

【讨论】:

  • “你的 XAML 中引用了什么”?我没有提及任何此类事情。我应该选择哪一个?稍后我会在我的问题中发布我的 XAML。
  • 嗯,我将您的代码放到了一个新的 WPF 项目中的一个新的用户控件中,它不会为我生成该 using 语句。 using 语句究竟出现在代码的哪个位置?
  • 我附上了已编译的 XAML -> C# 代码。它显示using。我试过 Clean Solution,然后 Rebuild Solution 没有区别。
  • 我怀疑您的Menu 中包含特定主题的元素。尝试添加对 PresentationFramework.Aero.dll 的引用,看看会发生什么。
  • 添加PresentationFramework.Aero.dll 后工作正常。嗯。
猜你喜欢
  • 2021-04-03
  • 2011-02-24
  • 1970-01-01
  • 2013-10-19
  • 2015-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多