# .NET

Source: https://docs.interop.io/desktop/capabilities/data-sharing/intents/net/index.html

## Overview

The Intents API is accessible via the `io.Intents` object.

## Finding Intents

To find all registered Intents, use the `GetIntents()` method:

```csharp
var intents = await io.Intents.GetIntents();
```

To find a specific Intent, use the `AwaitIntent()` method:

```csharp
var intent = await io.Intents.AwaitIntent("ViewChart");
```

## Raising Intents

To raise an Intent, use the `Raise()` method. It accepts an Intent name (or Intent object) and an Intent request builder as arguments:

```csharp
await io.Intents.Raise("ViewChart",
    builder => builder.WithContext("instrument", new {id = new {ticker = "msft"}}));
```

## Targeting Intent Handlers

When raising an Intent, optionally target one or more Intent handlers using the Intent request builder to specify a handler:

```csharp
var intent = await io.Intents.AwaitIntent("ViewChart");
var handler = intent.Handlers[0];

await io.Intents.Raise(intent,
    // Pass the desired Intent handler.
    builder => builder.WithHandler(handler));
```

Or:

```csharp
await io.Intents.Raise("ViewChart",
    builder =>
        // Target a specific Intent handler.
        builder.WithTargetSelector(handler =>
            handler.ApplicationName.Contains("InstrumentChart")));
```

## Context

To pass initial context to the Intent handler:

```csharp
await io.Intents.Raise("ViewChart",
    builder => builder.WithContext("instrument", new {id = new {ticker = "msft"}}));
```

To handle the context in the Intent handler, use the `AddIntentListener()` method (see [Registering Intents Dynamically](#registering_intents_dynamicaly)):

```csharp
io.Intents.AddIntentListener("ViewChart",
    intent => intent.WithDisplayName("Intrument Chart"),
    context =>
    {
        Dispatcher.BeginInvoke((Action) (() =>
        {
            var wnd = new Window
            {
                // Handle the passed context.
                Content = new TextBox { Text = context.Data.AsString }
            };
            wnd.Show();
        }));
    });
```

To pass [app starting context](https://docs.interop.io/desktop/capabilities/app-management/net/index.md#starting_apps-app_context) to the Intent handler:

```csharp
// Create app context.
var context = AppManagerContext.CreateNew();
context.Set("instrument", "msft");

await io.Intents.Raise("ViewChart",
    builder => builder.WithAppStartContext(context));
```

## Registering Intents Dynamically

To register an Intent dynamically, use the `AddIntentListener()` method. It accepts an Intent name, an Intent builder and an Intent context handler as arguments:

```csharp
// Register an Intent dynamically.
io.Intents.AddIntentListener("ViewChart",
    // Build the Intent.
    intent => intent.WithDisplayName("Intrument Chart"),
    // Specify how to handle the passed context.
    context =>
    {
        Dispatcher.BeginInvoke((Action) (() =>
        {
            var wnd = new Window
            {
                // Use the context.
                Content = new TextBox { Text = context.Data.AsString }
            };
            wnd.Show();
        }));
    });
```

> ⚠️ *Note that when you register an Intent dynamically and the Intent isn't defined in the app definition file, your app must be running in order to handle the Intent. If your app isn't running when this Intent is raised, it won't be available as a possible Intent handler.*
