【发布时间】:2012-06-16 07:38:39
【问题描述】:
我有以下代码
<Window x:Class="Netspot.DigitalSignage.Client.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" WindowStyle="SingleBorderWindow"
WindowStartupLocation="CenterScreen"
WindowState="Normal" Closing="Window_Closing">
任何获取高度/宽度的尝试都返回 NaN 或 0.0
谁能告诉我如何获得它?
这两种方法都不行
//Method1
var h = ((System.Windows.Controls.Panel)Application.Current.MainWindow.Content).ActualHeight;
var w = ((System.Windows.Controls.Panel)Application.Current.MainWindow.Content).ActualWidth;
//Method2
double dWidth = -1;
double dHeight = -1;
FrameworkElement pnlClient = this.Content as FrameworkElement;
if (pnlClient != null)
{
dWidth = pnlClient.ActualWidth;
dHeight = pnlClient.ActualWidth;
}
应用程序不会全屏运行。
【问题讨论】:
-
你在哪里尝试这个代码?如果您正在尝试窗口的构造函数,它将无法工作,否则
this.ActualHeight将为您提供窗口的实际高度 -
所以我必须在 window_loaded 之后这样做?好点我怎样才能调整一个尚不存在的窗口:)
-
没错,如果没有加载,你就无法获取窗口的大小
-
你为什么要获取高度/宽度?也许还有另一种方法可以做任何你想做的事情