Skip to content

Commit

Permalink
Merge pull request #5 from 239573049/main
Browse files Browse the repository at this point in the history
Add a Chinese document
  • Loading branch information
zdpcdt committed Jul 24, 2023
2 parents fb10354 + aefb58f commit 88ad355
Show file tree
Hide file tree
Showing 26 changed files with 459 additions and 502 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@
description: CONCEPTS
---

# Application Lifetimes
# 应用程序生命周期

Not all platforms are created equal! For example, the lifetime management that you may be used to developing with in Windows Forms or WPF can operate only on desktop-style platforms. _Avalonia UI_ is a cross-platform framework; so to make your application portable, it provides several different lifetime models for your application, and also allows you to control everything manually if the target platform permits.
并非所有平台都是相同的!例如,您可能习惯于在Windows Forms或WPF中开发的生命周期管理仅适用于桌面平台。_Avalonia UI_是一个跨平台框架;因此,为了使您的应用程序可移植,它提供了几种不同的应用程序生命周期模型,并且还允许您在目标平台允许的情况下手动控制一切。

## How do lifetimes work?
## 生命周期如何工作?

For a desktop application, you initialise like this:
对于桌面应用程序,您可以这样初始化:

```csharp
class Program
{
// This method is needed for IDE previewer infrastructure
// 这个方法是为了IDE预览器基础设施而需要的
public static AppBuilder BuildAvaloniaApp()
=> AppBuilder.Configure<App>().UsePlatformDetect();

// The entry point. Things aren't ready yet, so at this point
// you shouldn't use any Avalonia types or anything that expects
// a SynchronizationContext to be ready
// 入口点。此时还没有准备好,所以在这个点上
// 您不应该使用任何Avalonia类型或任何期望
// 准备好SynchronizationContext的东西
public static int Main(string[] args)
=> BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);
}
```

Then the main window is created in the `Application` class:
然后在`Application`类中创建主窗口:

```csharp
public override void OnFrameworkInitializationCompleted()
Expand All @@ -40,80 +40,80 @@ public override void OnFrameworkInitializationCompleted()
}
```

This method is called when the framework has initilized and the `ApplicationLifetime` property contains the chosen lifetime if any.&#x20;
当框架初始化完成时,将调用此方法,`ApplicationLifetime`属性包含所选择的生命周期(如果有)。

:::info
If you run the application in design mode (this uses the IDE previewer process), then `ApplicationLifetime` is null.
如果在设计模式下运行应用程序(这使用IDE预览器进程),则`ApplicationLifetime`为null。
:::

## Lifetime Interfaces
## 生命周期接口

_Avalonia UI_ provides a range of interfaces to allow you to choose a level of control that is suitable for your application. These are provided by the `BuildAvaloniaApp().Start[Something]` family of methods.
_Avalonia UI_提供了一系列接口,允许您选择适合您的应用程序的控制级别。这些接口由`BuildAvaloniaApp().Start[Something]`系列方法提供。

### IControlledApplicationLifetime

Provided by:
由以下方法提供:

* `StartWithClassicDesktopLifetime`
* `StartLinuxFramebuffer`

Allows you to subscribe to `Startup` and `Exit` events and permits explicitly shutting down of the application by calling the `Shutdown` method. This interface gives you control of the application's exit procedures.
允许您订阅`Startup``Exit`事件,并通过调用`Shutdown`方法显式关闭应用程序。此接口使您可以控制应用程序的退出过程。

### IClassicDesktopStyleApplicationLifetime

Inherits: `IControlledApplicationLifetime`
继承自:`IControlledApplicationLifetime`

Provided by:
由以下方法提供:

* `StartWithClassicDesktopLifetime`

Allows you to control your application lifetime in the manner of a Windows Forms or WPF application. This interface provides a way to access the list of the currently opened windows, to set a main window, and has three shutdown modes:
允许您以Windows Forms或WPF应用程序的方式控制应用程序的生命周期。此接口提供了一种访问当前打开窗口列表的方法,设置主窗口的方法,并具有三种关闭模式:

* `OnLastWindowClose` - shuts down the application when the last window is closed
* `OnMainWindowClose` - shuts down the application when the main window is closed (if it has been set).
* `OnExplicitShutdown` - disables automatic shutdown of the application, you need to call the `Shutdown` method in your code.
* `OnLastWindowClose` - 当最后一个窗口关闭时关闭应用程序
* `OnMainWindowClose` - 当主窗口关闭时关闭应用程序(如果已设置)。
* `OnExplicitShutdown` - 禁用应用程序的自动关闭,您需要在代码中调用`Shutdown`方法。

### ISingleViewApplicationLifetime

Provided by:
由以下方法提供:

* `StartLinuxFramebuffer`
* mobile platforms&#x20;
* 移动平台

Some platforms do not have a concept of a desktop main window and only allow one view on the device's screen at a time. For these platforms the lifetime allows you to set and change the main view class (`MainView`) instead.&#x20;
某些平台没有桌面主窗口的概念,只允许在设备屏幕上同时显示一个视图。对于这些平台,生命周期允许您设置和更改主视图类(`MainView`)。

:::info
To implement the navigation stack on platforms like this (with a single main view), you can use [_ReactiveUI_ routing](https://www.reactiveui.net/docs/handbook/routing/) or another routing control.
要在这样的平台上实现导航堆栈(具有单个主视图),您可以使用[_ReactiveUI_路由](https://www.reactiveui.net/docs/handbook/routing/)或其他路由控件。
:::

## Manual Lifetime Management
## 手动管理生命周期

If you need to, you can take full control of your application's lifetime management. For example on a desktop platform you can pass a delegate to `AppMain` to the `BuildAvaloniaApp.Start` method, and then manage things manually from there:
如果需要,您可以完全控制应用程序的生命周期管理。例如,在桌面平台上,您可以将委托传递给`BuildAvaloniaApp.Start`方法的`AppMain`,然后从那里手动管理事

```csharp
class Program
{
// This method is needed for IDE previewer infrastructure
// 这种方法是IDE预览器基础设施所必需的
public static AppBuilder BuildAvaloniaApp()
=> AppBuilder.Configure<App>().UsePlatformDetect();

// The entry point. Things aren't ready yet, so at this point
// you shouldn't use any Avalonia types or anything that expects
// a SynchronizationContext to be ready
// 入口点。事情还没准备好,所以现在
// 你不应该使用任何Avalonia类型或任何期望
// 准备一个SynchronizationContext
public static int Main(string[] args)
=> BuildAvaloniaApp().Start(AppMain, args);

// Application entry point. Avalonia is completely initialized.
// 应用程序入口点。Avalonia已完全初始化。
static void AppMain(Application app, string[] args)
{
// A cancellation token source that will be
// used to stop the main loop
// 一个取消令牌源,它将
// 用于停止主循环
var cts = new CancellationTokenSource();

// Do you startup code here
// 你在这里启动代码
new Window().Show();

// Start the main loop
// 启动主循环
app.Run(cts.Token);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@
description: CONCEPTS
---

# Attached Properties
# 附加属性

_Avalonia UI_ controls support the **attached property** concept. This is a property applied to a child control that references its container control.&#x20;
_Avalonia UI_ 控件支持**附加属性**的概念。这是应用于子控件的属性,它引用其容器控件。

In XMAL attached properties are defined as attributes of the child control element, using the format: `ContainerClassName.AttachedPropertyName="value"`
XMAL 中,附加属性被定义为子控件元素的属性,使用以下格式:`ContainerClassName.AttachedPropertyName="value"`

Here are some scenarios where an attached property is used:
以下是一些使用附加属性的场景:

## Attached Control
## 附加控件

An additional control is attached to a 'host control' for some purpose. This can be used where the control usually only allows a single child in its content zone. In this scenario the attached control is not counted as part of the content, but it will be used in some other way by the container. Examples include: context menus, tool tips and flyouts. &#x20;
附加控件是附加到“主控件”上的额外控件,用于某些目的。这可以在控件通常只允许一个子控件在其内容区域时使用。在这种情况下,附加控件不被视为内容的一部分,但容器会以其他方式使用它。示例包括:上下文菜单、工具提示和弹出窗口。

<img src='/img/gitbook-import/assets/image (9).png' alt=''/>
<img src='/img/gitbook-import/assets/image (9).png' alt='' />

## Layout Control
## 布局控件

Attached layout properties are used in scenarios where the container control has to know something about the child controls it is going to arrange. Examples include: grids, dock panels and relative panels.
附加布局属性用于容器控件需要了解将要排列的子控件的情况的场景。示例包括:网格、停靠面板和相对面板。

<img src='/img/gitbook-import/assets/image (17).png' alt=''/>
<img src='/img/gitbook-import/assets/image (17).png' alt='' />

:::info
For a full list of the _Avalonia UI_ built-in controls, see the reference [here](../reference/controls/).
有关 _Avalonia UI_ 内置控件的完整列表,请参阅[此处](../reference/controls/)的参考。
:::

&#x20;&#x20;
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@
description: CONCEPTS
---

# Control Trees
# 控件树

_Avalonia UI_ creates control trees from the XAML files in an application so that it can render the UI presentation and manage the application functionality. &#x20;
_Avalonia UI_ 从应用程序的 XAML 文件中创建控件树,以便能够渲染 UI 并管理应用程序的功能。

## Logical Tree
## 逻辑树

The logical control tree represents the application controls (including the main window) in the hierarchy in which they are defined in the XAML. For example: and control (button) inside another control (stack panel) in a window will have the 3-layer logical tree shown here:
逻辑控件树以 XAML 中定义的层次结构表示应用程序控件(包括主窗口)。例如:窗口中的一个控件(按钮)在另一个控件(堆栈面板)内部,将形成一个三层的逻辑树。

<img src='/img/gitbook-import/assets/image (61).png' alt=''/>
<img src='/img/gitbook-import/assets/image (61).png' alt='' />

While your application is running, you can show the _Avalonia Dev Tools_ window (hit F12). This displays the logical tree on its **Logical Tree** tab.
在应用程序运行时,您可以打开 _Avalonia Dev Tools_ 窗口(按 F12 键)。这将在其 **逻辑树** 选项卡上显示逻辑树。

## Visual Tree&#x20;
## 可视树

The visual control tree contains everything that is actually being run by _Avalonia UI_. It shows all the properties set on the controls, and all the additional parts that have been added by _Avalonia UI_ in order to present the UI and manage the application functionality. &#x20;
可视控件树包含 _Avalonia UI_ 实际运行的所有内容。它显示了控件上设置的所有属性,以及 _Avalonia UI_ 添加的所有额外部分,以呈现 UI 并管理应用程序的功能。

<img src='/img/gitbook-import/assets/image (15) (2).png' alt=''/>
<img src='/img/gitbook-import/assets/image (15) (2).png' alt='' />

You can see the visual control tree on the **Visual Tree** tab of the _Avalonia Dev Tools_ window.
您可以在 _Avalonia Dev Tools_ 窗口的 **可视树** 选项卡上查看可视控件树。

## Events&#x20;
## 事件

An essential part of application functionality management performed by _Avalonia UI_, is the generation and propagation of events. The **Events** tab logs the source and propagation of events as you move around, and otherwise interact with the running application.
_Avalonia UI_ 执行应用程序功能管理的一个重要部分是事件的生成和传播。**事件** 选项卡记录了事件的源和传播,当您在运行的应用程序中移动或与其交互时。

<img src='/img/gitbook-import/assets/image (1) (1) (2).png' alt=''/>
<img src='/img/gitbook-import/assets/image (1) (1) (2).png' alt='' />
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ title: Deep Dives

# Deep Dives

This section contains pages that will help you understand some of the concepts used by _Avalonia UI._ This is background and theory information, often supported by code samples you can work through. This section is arranged alphabetically by title.&#x20;
本节包含的页面将帮助您理解_Avalonia UI使用的一些概念。_这是背景和理论信息,通常由您可以完成的代码示例支持。本节按标题的字母顺序排列#x20;

:::tip
If you are new to _Avalonia UI_, we recommend that you complete the ['Get Started'](../get-started) before you read these pages.
如果您是新的_Avalonia UI_,我们建议您在阅读这些页面之前完成['开始'](../get-started)
:::

:::tip
If you are looking for practical advice on how to achieve any of the above, learn the [Basics](../basics/).
如果你正在寻找关于如何实现上述任何一个的实用建议,请学习[基础知识](../basics/).
:::
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
description: CONCEPTS
---

# Input
# 输入

Avalonia operates on the abstraction called pointer devices.
Avalonia在抽象层面上操作指针设备。

Various Controls that implement `ICommandSource` have a `HotKey` property
实现`ICommandSource`的各种控件都有一个`HotKey`属性。

控件通常检测和响应用户输入。Avalonia的[输入系统](../input)使用直接事件和路由事件来支持文本输入、焦点管理和鼠标定位。


Controls most often detect and respond to user input. The Avalonia [input system](../input) uses both [direct and routed events](../input/routed-events) to support text input, focus management, and mouse positioning.

Applications often have complex input requirements. Avalonia provides a [command system](../../basics/user-interface/adding-interactivity) that separates user-input actions from the code that responds to those actions.
应用程序通常具有复杂的输入要求。Avalonia提供了一个[命令系统](../../basics/user-interface/adding-interactivity),将用户输入操作与响应这些操作的代码分离开来。
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
description: CONCEPTS - Input
---

# Gestures
# 手势

Controls can detect gestures using Gesture Recognizers. These recognizers are hosted in controls, and listen for and track pointer events that the control receives, sending gesture events when they have detected a gesture has started.
控件可以使用手势识别器来检测手势。这些识别器托管在控件中,监听控件接收到的指针事件,并在检测到手势开始时发送手势事件。

All gesture recognizers derive from the base class `GestureRecognizer`, and can be attached to a control using the control's `GestureRecognizers` property. The following shows an Image hosting a `ScrollGestureRecognizer`;
所有手势识别器都派生自基类`GestureRecognizer`,可以使用控件的`GestureRecognizers`属性将其附加到控件上。以下示例显示了一个托管了`ScrollGestureRecognizer`的图像:

```xml
<Image Stretch="UniformToFill"
Expand All @@ -20,7 +20,6 @@ All gesture recognizers derive from the base class `GestureRecognizer`, and can
</Image>
```


```csharp title='C#'
image.GestureRecognizers.Add(new ScrollGestureRecognizer()
{
Expand All @@ -29,12 +28,12 @@ image.GestureRecognizers.Add(new ScrollGestureRecognizer()
});
```

## More Information
## 更多信息

:::info
You can view more information on the available gesture recognizers [here](../../reference/gestures)
您可以在[这里](../../reference/gestures)查看更多关于可用手势识别器的信息。

You can view the source for related classes
您可以查看相关类的源代码:

[GestureRecognizer](https://github.com/AvaloniaUI/Avalonia/blob/master/src/Avalonia.Base/Input/GestureRecognizers/GestureRecognizer.cs)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
description: CONCEPTS - Input
---

# Keyboard and Hotkeys
# 键盘和快捷键

Various Controls that implement `ICommandSource` have a `HotKey` property that you can set or bind to. Pressing the hotkey will execute the command [bound](../../basics/user-interface/adding-interactivity#commands) to the Control.
实现了`ICommandSource`接口的各种控件都有一个`HotKey`属性,您可以设置或绑定它。按下快捷键将执行与控件绑定的命令。

```markup
<Menu>
Expand All @@ -14,25 +14,25 @@ Various Controls that implement `ICommandSource` have a `HotKey` property that y
</Menu>
```

You can also use the static methods of the `HotKeyManager` class to set and get hotkeys from code:
您还可以使用`HotKeyManager`类的静态方法来从代码中设置和获取快捷键:

```csharp
InitializeComponent();
HotKeyManager.SetHotKey(saveMenuItem, new KeyGesture(Key.S, KeyModifiers.Control));
```

## Keys and Modifiers
## 键和修饰键

A Hotkey must have one [Key](http://reference.avaloniaui.net/api/Avalonia.Input/Key/) and zero or more [KeyModifiers](http://reference.avaloniaui.net/api/Avalonia.Input/KeyModifiers/). When setting a Hotkey in XAML using the `HotKey` property, the string will be parsed as a [KeyGesture](http://reference.avaloniaui.net/api/Avalonia.Input/KeyGesture/). [Enum.Parse](https://docs.microsoft.com/en-us/dotnet/api/system.enum.parse) is used to parse the key and modifiers but synonyms like `Ctrl` instead of `Control` or `Win` instead of `Meta` can be used.
一个快捷键必须有一个[Key](http://reference.avaloniaui.net/api/Avalonia.Input/Key/)和零个或多个[KeyModifiers](http://reference.avaloniaui.net/api/Avalonia.Input/KeyModifiers/)。在XAML中使用`HotKey`属性设置快捷键时,字符串将被解析为[KeyGesture](http://reference.avaloniaui.net/api/Avalonia.Input/KeyGesture/)。解析键和修饰键使用了[Enum.Parse](https://docs.microsoft.com/en-us/dotnet/api/system.enum.parse),但可以使用`Ctrl`代替`Control``Win`代替`Meta`等同义词。

### Reference
### 参考

* [HotKeyManager](http://reference.avaloniaui.net/api/Avalonia.Controls/HotKeyManager/)
* [KeyGesture](http://reference.avaloniaui.net/api/Avalonia.Input/KeyGesture/)
* [KeyModifiers](http://reference.avaloniaui.net/api/Avalonia.Input/KeyModifiers/)
* [Key](http://reference.avaloniaui.net/api/Avalonia.Input/Key/)

### Source code
### 源代码

* [HotkeyManager.cs](https://github.com/AvaloniaUI/Avalonia/blob/master/src/Avalonia.Controls/HotkeyManager.cs)
* [KeyGesture.cs](https://github.com/AvaloniaUI/Avalonia/blob/master/src/Avalonia.Input/KeyGesture.cs)
* [KeyGesture.cs](https://github.com/AvaloniaUI/Avalonia/blob/master/src/Avalonia.Input/KeyGesture.cs)
Loading

0 comments on commit 88ad355

Please sign in to comment.