加载窗口WPF时的黑色空白窗口

时间:2017-01-04 10:04:15

标签: c# wpf

在我的应用程序中,在菜单上单击,我们将启动一个窗口。加载时,窗口将为黑色几秒钟,然后将出现控件。我已将图像附在此处以供参考。请帮助我。enter image description here

2 个答案:

答案 0 :(得分:0)

您是否在项目中导入并使用WindowChrome类(System.Windows.Shell)?当我将这个类导入我的项目时,这个问题就发生了。

答案 1 :(得分:0)

你应该读到这个:

How to fix the WPF form resize - controls lagging behind and black background?

您可以按照@@ ghord的建议调用SetClassLong方法来更改显示的黑位的颜色:

public static class WindowExtensions
{
    private const int GCL_HBRBACKGROUND = -10;
    private const int COLOR_WINDOW = 5;

    public static void SetClassLong(this Window window)
    {
        //change the background colour of the window to "hide" possible black rendering artifacts
        IntPtr handle = new WindowInteropHelper(window).EnsureHandle();
        if (handle != IntPtr.Zero)
            SetClassLong(handle, GCL_HBRBACKGROUND, SafeNativeMethods.GetSysColorBrush(COLOR_WINDOW));
    }

    private static IntPtr SetClassLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong)
    {
        if (IntPtr.Size > 4)
            return SafeNativeMethods.SetClassLongPtr64(hWnd, nIndex, dwNewLong);
        else
            return new IntPtr(SafeNativeMethods.SetClassLongPtr32(hWnd, nIndex, unchecked((uint)dwNewLong.ToInt32())));
    }
}

public partial class MainWindow : Window, IViewFor<MainWindowViewModel>
{
    public MainWindow()
    {
        InitializeComponent();
    }

    protected override void OnSourceInitialized(EventArgs e)
    {
        base.OnSourceInitialized(e);
        //change the background colour of the window to "hide" possible black rendering artifacts
        this.SetClassLong();
    }
}