如何从xaml获取屏幕尺寸?

TTG*_*oup 6 c# wpf

我在C#上使用wpf来设计GUI,我想从xaml代码中获取屏幕大小(宽度和高度的值).

我知道如何从C#代码中获取它们

   Width = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;
   Height = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;
Run Code Online (Sandbox Code Playgroud)

但我不知道如何从XAML代码中获取它们.

Vla*_*den 14

这会奏效.您可以在此处阅读有关SystemParameters的更多信息

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <TextBlock Text="{Binding Source={x:Static SystemParameters.FullPrimaryScreenHeight}}" />
        <TextBlock Text="{Binding Source={x:Static SystemParameters.FullPrimaryScreenWidth}}" />
        <TextBlock Text="{Binding Source={x:Static SystemParameters.PrimaryScreenHeight}}" />
        <TextBlock Text="{Binding Source={x:Static SystemParameters.PrimaryScreenWidth}}" />
    </StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)


小智 8

查看SystemParameters类:http://msdn.microsoft.com/de-de/library/system.windows.systemparameters.fullprimaryscreenheight.aspx

你可以像这样使用它:

<Object Attribute="{x:Static SystemParameters.FullPrimaryScreenHeight}"/>
Run Code Online (Sandbox Code Playgroud)

(您也可以使用x:Static表示法来查询Windows窗体属性,但是如果您正在开发WPF应用程序,则可能需要使用SystemParameters类)