简体   繁体   English

WPF MessageBox 窗口样式

[英]WPF MessageBox window style

How to apply the default Windows style to the standard MessageBox in WPF?如何将默认 Windows 样式应用于 WPF 中的标准MessageBox

For example, when I execute next code:例如,当我执行下一个代码时:

MessageBox.Show("Hello Stack Overflow!", "Test", MessageBoxButton.OKCancel, 
    MessageBoxImage.Exclamation);

I'm getting message box:我收到消息框:

在此处输入图片说明

But in WinForms everything is OK with style:但是在 WinForms 中,样式一切正常:

MessageBox.Show("Hello Stack Overflow!", "Test", MessageBoxButtons.OKCancel, 
    MessageBoxIcon.Exclamation);

在此处输入图片说明

According to this page, WPF picks up the old styles for some of the controls.根据页面,WPF 为某些控件选取了旧样式。

To get rid of it, you have to create a custom app.manifest file (Add -> New item -> Application Manifest File) and paste the following code in it (right after the /trustInfo - Tag ):要摆脱它,您必须创建一个自定义 app.manifest 文件(添加 -> 新项目 -> 应用程序清单文件)并将以下代码粘贴到其中(紧跟在 /trustInfo - Tag 之后):

<!-- Activate Windows Common Controls v6 usage (XP and Vista): -->
<dependency>
  <dependentAssembly>
    <assemblyIdentity
      type="win32"
      name="Microsoft.Windows.Common-Controls"
      version="6.0.0.0"
      processorArchitecture="*"
      publicKeyToken="6595b64144ccf1df"
      language="*"/>
  </dependentAssembly>
</dependency>

Then you have to compile your solution with this app.manifest (set it in the project properties -> Application -> Point to the new manifest in "Icons and manifest").然后,您必须使用此 app.manifest 编译您的解决方案(在项目属性 -> 应用程序 -> 指向“图标和清单”中的新清单中进行设置)。

If you start your application now it should look like the WinForms- MessageBox.如果您现在启动您的应用程序,它应该看起来像 WinForms-MessageBox。

as how i triggered it, "redirecting" the usual references to the Forms ones (they work the same, but are named differently):至于我如何触发它,“重定向”对表单的通常引用(它们的工作方式相同,但名称不同):

using MessageBox = System.Windows.Forms.MessageBox;
using MessageBoxImage = System.Windows.Forms.MessageBoxIcon;
using MessageBoxButton = System.Windows.Forms.MessageBoxButtons;
using MessageBoxResult = System.Windows.Forms.DialogResult;

namespace ... class ...

    public MainWindow()
    {
        InitializeComponent();

        System.Windows.Forms.Application.EnableVisualStyles();
    }

    public void do()
    {
        // updated style, but good syntax for a later solution
        MessageBox.Show("Some Message", "DEBUG", MessageBoxButton.OK, MessageBoxImage.Question);
    }

... the manifest solution did not work for me. ...清单解决方案对我不起作用。

The reason that the WinForms one works the way that it does is because visual styles are turned on (ie using Common Controls v6) in its Main function. WinForms 以这种方式工作的原因是因为在其 Main 函数中启用了视觉样式(即使用 Common Controls v6)。 If you remove the call to System.Windows.Forms.Application.EnableVisualStyles() , then the WinForms Message Box will look just like the WPF one.如果您删除对System.Windows.Forms.Application.EnableVisualStyles()的调用,则 WinForms 消息框将与 WPF 中的一样。

This doesn't happen for a WPF app, possibly because all of the WPF controls are rendered so there is no need to use the new version of Common Controls. WPF 应用程序不会发生这种情况,可能是因为所有 WPF 控件都已呈现,因此无需使用新版本的通用控件。

You might try calling EnableVisualStyles() somewhere in the start up of your WPF application.您可以尝试在 WPF 应用程序启动时的某处调用EnableVisualStyles() I don't know if it will work or not, but it's worth a try.我不知道它是否会起作用,但值得一试。 This will require a reference to System.Windows.Forms, though.不过,这将需要对 System.Windows.Forms 的引用。

此外,对于 WPF,我建议使用具有WPF 消息框扩展 WPF 工具包

Create a new manifest and paste this:创建一个新的清单并粘贴:

<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
  <assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
        <requestedExecutionLevel level="asInvoker" uiAccess="false" />
      </requestedPrivileges>
    </security>
  </trustInfo>
  <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
    <application>
    </application>
  </compatibility>
  <dependency>
    <dependentAssembly>
      <assemblyIdentity
      type="win32"
      name="Microsoft.Windows.Common-Controls"
      version="6.0.0.0"
      processorArchitecture="*"
      publicKeyToken="6595b64144ccf1df"
      language="*"
    />
    </dependentAssembly>
  </dependency>
</assembly>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM