的高度/寬度我有以下代碼獲取窗口WPF
<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'會給你窗口的實際高度 – Habib
,所以我必須在window_loaded之後做到這一點?好點我怎麼才能確定一個尚未存在的窗口:) –
的確如此,如果窗口沒有加載,你無法獲得窗口的大小 – Habib