我的wpf应用程序在Windows服务器上的不同行为

时间:2018-05-22 14:10:19

标签: c# wpf xaml

在我的wpf应用程序中发生了一些奇怪的事情,我有一个主窗口,其中包含一些用户可以在它们之间导航的页面。该应用程序在我的Windows 10操作系统上运行良好,但是当我在Windows服务器2012上运行时,如果我正在玩窗口大小的应用程序崩溃。

这里是主窗口xaml:

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      xmlns:fa="http://schemas.fontawesome.io/icons/"
        xmlns:local="clr-namespace:myApp"
         xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
         xmlns:UserControls="clr-namespace:myApp.View.UserControls"
        xmlns:Helpers="clr-namespace:myApp.Helpers" 
    x:Class="myApp.MainWindow"
        mc:Ignorable="d"       
        ResizeMode="CanResizeWithGrip"
        MinWidth="900"
        MinHeight="600"
    Background="#FFDEDEDE"
        Title="Preparation Tool" Height="600" Width="900" Icon="..\Resources\S_icon.ico" Foreground="White" WindowStyle="None">

1 个答案:

答案 0 :(得分:0)

看起来您需要在Window代码中处理窗口大小调整。当我用窗口制作任何东西(调整大小,移动)时,这段代码对我有用

    public Window1()
{
     InitializeComponent();

      this.SourceInitialized += Window1_SourceInitialized;
}

private void Window1_SourceInitialized(object sender, EventArgs e)
{
    WindowInteropHelper helper = new WindowInteropHelper(this);
    HwndSource source = HwndSource.FromHwnd(helper.Handle);
    source.AddHook(WndProc);
}

const int WM_SYSCOMMAND = 0x0112;
const int SC_MOVE = 0xF010;
const int WM_WINDOWPOSCHANGED = 0x0047;
const int WM_WINDOWPOSCHANGING = 0x0046;
const int WM_EXITSIZEMOVE = 0x0232;

private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{

    switch (msg)
    {
        case WM_SYSCOMMAND:
                int command = wParam.ToInt32() & 0xfff0;
                if (command == SC_MOVE && command == WM_WINDOWPOSCHANGED &&
                    command == WM_WINDOWPOSCHANGING && command == WM_EXITSIZEMOVE)
                    {
                        handled = true;
                    }
                break;
            default:
                break;
        }
        return IntPtr.Zero;
    }