如何:使用自动布局创建按钮How to: Use Automatic Layout to Create a Button

本示例介绍如何使用自动布局方法在可本地化的应用程序中创建按钮。This example describes how to use the automatic layout approach to create a button in a localizable application.

本地化用户界面 (UI)user interface (UI)是一个耗时的过程。Localization of a 用户界面 (UI)user interface (UI) can be a time consuming process. 通常,本地化人员除翻译文本外,还需要重新调整元素大小并重新定位元素。Often localizers need to resize and reposition elements in addition to translating text. 在过去每种语言的UIUI改编需要进行调整。In the past each language that a UIUI was adapted for required adjustment. 现在使用的功能Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF)可以设计减少必需的调整的元素。Now with the capabilities of Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) you can design elements that reduce the need for adjustment. 种编写可以更轻松地重设大小和重新定位应用程序的方法称为automatic layoutThe approach to writing applications that can be more easily resized and repositioned is called automatic layout.

示例Example

以下两个可扩展应用程序标记语言 (XAML)Extensible Application Markup Language (XAML)示例创建的应用程序实例化按钮; 一个使用英文文本,另一个使用西班牙文本。The following two 可扩展应用程序标记语言 (XAML)Extensible Application Markup Language (XAML) examples create applications that instantiate a button; one with English text and one with Spanish text. 请注意,除文本之外,代码都一样;按钮会配合文字进行调整。Notice that the code is the same except for the text; the button adjusts to fit the text.

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="ButtonLoc.Pane1"
    Name="myWindow"
    SizeToContent="WidthAndHeight"
    >

<DockPanel> 
    <Button FontSize="28" Height="50">My name is Hope.</Button>
</DockPanel>
</Window>
<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="ButtonLoc.Pane1"
    Name="myWindow"
    SizeToContent="WidthAndHeight"
    >

 <DockPanel> 
    <Button FontSize="28" Height="50">Me llamo Esperanza.</Button>
  </DockPanel>
</Window>

下图显示了与自动调整大小的按钮的代码示例的输出:The following graphic shows the output of the code samples with auto-resizable buttons:

具有不同语言的文本的同一按钮

请参阅See also