此示例介绍如何使用自动布局方法在可本地化应用程序中创建按钮。
用户界面(UI)的本地化可能是一个耗时的过程。 本地化人员通常除了翻译文本之外,还需要调整元素的大小和重新定位元素。 在过去,UI 的每种语言都经过了必要的调整。 现在,借助 Windows Presentation Foundation(WPF)的功能,可以设计可减少调整需求的元素。 编写可更轻松地调整大小和重新定位的应用程序的方法称为 automatic layout
。
示例:
以下两个可扩展应用程序标记语言(XAML)示例创建实例化按钮的应用程序;一个包含英语文本,一个包含西班牙语文本。 请注意,代码除了文本之外都是相同的,按钮会根据文本调整大小。
<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>
下图显示了包含自动调整大小的按钮的代码示例的输出: