📅  最后修改于: 2020-11-19 08:26:35             🧑  作者: Mango
控件的布局对于应用程序可用性非常重要且至关重要。它用于在应用程序中安排一组GUI元素。选择布局面板时,需要考虑某些重要事项。他们是-
如果应用程序已在不同的屏幕分辨率上使用,则控件的固定像素排列将不起作用。 XAML提供了一组丰富的内置布局面板,以适当的方式排列GUI元素。
我们将从简单的固定布局开始。然后,我们将研究Silverlight设计支持的动态布局方案。我们将看到渗透到所有用户界面元素中的与布局相关的属性和概念。
最简单的布局是由Canvas元素提供的。 “画布”面板是基本的布局面板,在其中可以使用相对于“画布”任一侧(例如左,右,上和下)的坐标显式定位子元素。
通常,“画布”用于2D图形元素(例如“椭圆”,“矩形”等)。它不用于UI元素,因为在调整XAML应用程序的大小,对其进行本地化或缩放时,指定绝对坐标会带来麻烦。
下面给出的是Canvas类的常用属性。
Sr. No. | Property & Description |
---|---|
1 |
Background Gets or sets a Brush that fills the panel content area. (Inherited from Panel) |
2 |
Children Gets a UIElementCollection of child elements of this Panel. (Inherited from Panel.) |
3 |
Height Gets or sets the suggested height of the element. (Inherited from FrameworkElement.) |
4 |
ItemHeight Gets or sets a value that specifies the height of all items that are contained within a WrapPanel. |
5 |
ItemWidth Gets or sets a value that specifies the width of all items that are contained within a WrapPanel. |
6 |
LogicalChildren Gets an enumerator that can iterate the logical child elements of this Panel element. (Inherited from Panel.) |
7 |
LogicalOrientation The Orientation of the panel, if the panel supports layout in only a single dimension. (Inherited from Panel.) |
8 |
LeftProperty Identifies the Canvas.Left XAML attached property. |
9 |
Margin Gets or sets the outer margin of an element. (Inherited from FrameworkElement.) |
10 |
Name Gets or sets the identifying name of the element. The name provides a reference so that code-behind, such as event handler code, can refer to a markup element after it is constructed during processing by a XAML processor. (Inherited from FrameworkElement.) |
11 |
Orientation Gets or sets a value that specifies the dimension in which child content is arranged. |
12 |
Parent Gets the logical parent element of this element. (Inherited from FrameworkElement.) |
13 |
Resources Gets or sets the locally-defined resource dictionary. (Inherited from FrameworkElement.) |
14 |
Style Gets or sets the style used by this element when it is rendered. (Inherited from FrameworkElement.) |
15 |
TopProperty Identifies the Canvas.Top XAML attached property. |
16 |
Width Gets or sets the width of the element. (Inherited from FrameworkElement.) |
17 |
ZIndexProperty Identifies the Canvas.ZIndex XAML attached property. |
下面给出的是Canvas的常用方法。
Sr. No. | Method & Description |
---|---|
1 |
GetLeft Gets the value of the Canvas.Left XAML attached property for the target element. |
2 |
GetTop Gets the value of the Canvas.Top XAML attached property for the target element. |
3 |
GetZIndex Gets the value of the Canvas.ZIndex XAML attached property for the target element. |
4 |
SetLeft Sets the value of the Canvas.Left XAML attached property for a target element. |
5 |
SetTop Sets the value of the Canvas.Top XAML attached property for a target element. |
6 |
SetZIndex Sets the value of the Canvas.ZIndex XAML attached property for a target element. |
下面的示例演示如何将子元素添加到Canvas中。以下是XAML实现,其中在具有不同偏移属性的Canvas内创建了一个Ellipse。
编译并执行上述代码后,您将看到以下输出。