【问题标题】:Application width and height declared at one place在一处声明的应用程序宽度和高度
【发布时间】:2015-01-24 02:30:15
【问题描述】:

我有一个带有多个导航页面的 WPF 应用程序。到目前为止,应用程序的高度和宽度都在 MainWindow 和每个页面中声明。 所以这里是主窗口

<Window x:Class="MyApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="My Application"  Height="350" Width="725" >

然后每个页面也有如下所示的高度和宽度

<Page x:Class="MyApp.Page1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" 
     d:DesignHeight="350" d:DesignWidth="725"
    Title="Page1"  >

能否有一个集中的地方,我可以有高度和宽度,而不是在这么多地方?

【问题讨论】:

  • 我知道这之前一定有人问过。
  • 那个重复与关闭不是同一个问题。重复是一页的大小。
  • @Blam 我有点假设 OP 能够注意到细微差别并计算出将资源放在资源字典中,并将静态资源放在他想要的位置。

标签: c# wpf xaml


【解决方案1】:
<Application x:Class="SubSetOf.App"  
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:sys="clr-namespace:System;assembly=mscorlib"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <sys:Double x:Key="SysHeight">100</sys:Double>
        <sys:Double x:Key="SysWidth">200</sys:Double>
    </Application.Resources>
</Application>

<Page x:Class="SubSetOf.Page1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" 
      d:DesignHeight="{StaticResource SysHeight}"
      d:DesignWidth="{StaticResource SysWidth}" 
      Height="{StaticResource SysHeight}" 
      Width="{StaticResource SysWidth}"
      Title="Page1">
    <Grid>
        <TextBox Background="pink"/>
    </Grid>
</Page>

【讨论】:

    【解决方案2】:

    请注意,您在Page 中看到的宽度和高度仅供设计器使用,在编译时会被忽略(通过设置mc:Ignorable="d" 属性)。

    否则您可以将宽度和高度存储为资源,但我不确定这对设计师是否真的有用

    【讨论】:

      【解决方案3】:

      将值保存为资源,然后将每个页面的值绑定到该资源。

      编辑:

      要将值保存为资源,在visual studio下的属性窗口中,单击宽度(和高度)文本框右侧的小框,然后选择Convert To New Resource。在那里选择Application Resource 并为该值命名。

      为了使用此资源,请设置如下值:

      Width = {StaticResource WidthResource}
      

      【讨论】:

      • 在您的资源中:&lt;System:Double x:Key="StandardHeight"&gt;500&lt;/System:Double&gt; 然后在您的视图中Width="{StaticResource StandardHeight}"
      • @johnsmith 我更新了我的帖子,详细介绍了资源
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-03
      相关资源
      最近更新 更多