> For the complete documentation index, see [llms.txt](https://avaloniachina.gitbook.io/avalonia/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://avaloniachina.gitbook.io/avalonia/docs/data-binding/binding-to-tasks-and-observables.md).

# 绑定到任务和可观察对象

您可以使用`^`流绑定运算符订阅任务或可观察对象。

## 示例1：绑定到可观察对象

例如，如果`DataContext.Name`是一个`IObservable<string>`，那么下面的示例将绑定到每个值生成时可观察对象的每个字符串的长度。

```markup
<TextBlock Text="{Binding Name^.Length}"/>
```

## 示例2: 绑定到任务

如果需要做一些繁重的工作来加载属性的内容，则可以绑定到`async Task<TResult>`的结果。

假设您有下列的视图模型，它在一个耗时的任务中生成一些文本：

```csharp
public Task<string> MyAsyncText => GetTextAsync();

private async Task<string> GetTextAsync()
{
  await Task.Delay(1000); // 延时只是为了演示
  return "Hello from async operation";
}
```

您可以通过以下方式绑定到结果：

```markup
<TextBlock Text="{Binding MyAsyncText^, FallbackValue='Wait a second'}" />
```

{% hint style="info" %}
注：您可以使用`FallbackValue`显示一些加载指示符。
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://avaloniachina.gitbook.io/avalonia/docs/data-binding/binding-to-tasks-and-observables.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
