为什么 WindowStartupLocation = CenterScreen 将我的窗口放置在屏幕中心以外的其他地方? [英] Why does WindowStartupLocation = CenterScreen place my window somewhere other than the center of the screen?

查看:59
本文介绍了为什么 WindowStartupLocation = CenterScreen 将我的窗口放置在屏幕中心以外的其他地方?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是窗口声明:

<Window
    x:Class="Pse.ExperimentBase.SplashWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Style="{StaticResource _windowStyle}"
    WindowStartupLocation="CenterScreen"
    WindowStyle="None">

这是样式声明:

<Style
    x:Key="_windowStyle"
    TargetType="Window">
    <Setter
        Property="Width"
        Value="{Binding Path=InitialWindowWidth}" />
    <Setter
        Property="Height"
        Value="{Binding Path=InitialWindowHeight}" />
    <Setter
        Property="Icon"
        Value="Resources/MyIcon.ico" />
    <Setter
        Property="Background"
        Value="{StaticResource _fadedOrangeBrush}" />
    <Setter
        Property="FontSize"
        Value="11" />
</Style>

讨论:

屏幕为 1280 X 1024.窗口大小(由 InitialWindowWidth、InitialWindowHeight 绑定确定)为 800 X 600.

The screen is 1280 X 1024. The window size (determined by the InitialWindowWidth, InitialWindowHeight bindings) is 800 X 600.

当窗口打开时,它显示 188、141(左、上).这基本上是它应该在的位置的西北".如果我计算真正的居中值,它应该是 240、212(左、上).

When the Window opens, it appears 188, 141 (left, top). This is basically "northwest" of where it should be. If I calculate the true centered values, it should be 240, 212 (left, top).

线索?

总是出现问题的第一个窗口.如果我打开同一窗口的第二个实例,它将显示在正确的位置.

It's always the first window that has the problem. If I open a second instance of the same window, it will show up in the correct location.

另一个线索?

如果我在打开第一个实例之前创建两个实例,则第二个窗口实例只会显示在正确的位置.

The second window instance will only show up in the correct location if I create both instances before opening the first one.

所以...

Window win1 = windowFactory.CreateSplashWindow();
win1.Show();
Window win2 = windowFactory.CreateSplashWindow();
win2.Show();
win1.Hide();

...抵消 win1 和 win2

...offsets both win1 and win2

但是……

Window win1 = windowFactory.CreateSplashWindow();
Window win2 = windowFactory.CreateSplashWindow();
win1.Show();
win2.Show();
win1.Hide();

...偏移 win1 但显示 win2 死点.

...offsets win1 but shows win2 dead center.

所以我的问题是:

这是怎么回事???

又一个线索.如果我添加...

Yet another clue. If I add...

Width="800"
Height="600"

...对于我的 Window 声明,偏移问题消失了.但是,这对我来说不是解决方案,因为我需要将窗口大小作为存储在设置文件中的首选项.这就是为什么我的 Style 中有数据绑定的原因.(请注意,宽度和高度的数据绑定工作正常——所有窗口都显示为正确的大小).

...to my Window declaration, the offset problem disappears. This isn't a solution for me, though, because I need the window size to be a preference stored in a settings file. That's why I have the data binding in my Style. (And note, the data binding for the Width and Height works fine--all windows show up as the correct size).

编辑 #2:

我在调试模式下检查了宽度和高度并得出了这个有趣的结论:

I did checking of widths and heights while in debug mode and came to this interesting conclusion:

如果我创建了多个窗口实例但没有打开任何窗口,则所有窗口实例的 Width 和 Height 属性都是NaN".

If I create multiple window instances but don't open any windows, the Width and Height properties of all window instances are "NaN".

如果我只打开一个实例,所有窗口实例的宽度和高度属性突然变为有效数字,而不仅仅是窗口的宽度和高度我打开了.

If I then open just one of the instances, the width and height properties of all window instance are suddenly valid numbers, not just the width and height of the window I opened.

这告诉我 WPF 推迟数据绑定,直到在第一个窗口上调用 Show().

This tells me that WPF defers data-binding until Show() is called on the first window.

--> 所以现在问题变成了:我可以强制 WPF 在显示第一个窗口之前 进行数据绑定吗?

--> So now the question becomes: Can I force WPF to do the data-binding before I show the first window?

编辑 #3:

我刚刚向 Microsoft 提交了一份关于此问题的错误报告.这是链接:

I just submitted submitted a bug report on this issue to Microsoft. Here is the link:

https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=477598

编辑 #4:解决方法

对此还没有真正的解决方案,但我刚刚确认您可以使用虚拟窗口解决该问题.请按照以下步骤操作:

No real resolution on this yet, but I just confirmed that you can work around the problem using a dummy window. Follow these steps:

  1. 创建一个名为BlankWindow"的新 XPF 窗口(请参阅下面的 XAML).
  2. 创建一个 BlankWindow 实例以及您希望在屏幕上居中的窗口 (FirstWindow).
  3. 执行以下代码序列:

...

BlankWindow.Show()
FirstWindow.Show()
BlankWindow.Hide()

我的虚拟窗口的 XAML 仅此而已:

The XAML for my dummy window is nothing more than this:

<Window
    x:Class="Pse.ExperimentBase.Windows.BlankWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="BlankWindow"
    Height="0"
    Width="0"
    WindowStyle="None">
    <Grid></Grid>
</Window>

在FirstWindow"打开之前我没有看到明显的闪烁,所以我认为这是一个足够好的解决方法,直到 MS 开始修复错误.

I see no noticeable flicker before "FirstWindow" opens, so I consider it a good enough workaround until MS get around to fixing the bug.

推荐答案

您的 WidthHeight 绑定是否会妨碍您?如果您根本不设置WidthHeight,窗口是否出现在正确的位置?如果是这样,那是否与您设置绑定时的位置相同?

Could your Width and Height bindings be getting in the way? If you don't set the Width and Height at all, does the window appear in the correct location? If so, is that the same location as when you have the bindings set as above?

如果这一切都是真的,那么听起来 WPF 在从绑定中获取 WidthHeight 的值之前正在计算窗口的位置.我不确定解决这个问题的好方法 - 可能首先手动设置 WidthHeight(在 XAML 或构造函数中),然后设置绑定一旦显示出来?

If all that is true, then it sounds like WPF is computing the location of the window prior to getting the value of Width and Height from your bindings. I'm not sure of a good way to get around this - maybe set Width and Height manually at first (either in XAML or the constructor), then set up the bindings once it has been displayed?

这篇关于为什么 WindowStartupLocation = CenterScreen 将我的窗口放置在屏幕中心以外的其他地方?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆