Table of Contents

Method AsRunnable

Namespace
Terminal.Gui.ViewBase
Assembly
Terminal.Gui.dll

AsRunnable<TView, TResult>(TView, Func<TView, TResult?>)

Converts any View into a runnable with typed result extraction.

public static RunnableWrapper<TView, TResult> AsRunnable<TView, TResult>(this TView view, Func<TView, TResult?> resultExtractor) where TView : View

Parameters

view TView

The view to wrap. Cannot be null.

resultExtractor Func<TView, TResult>

Function that extracts the result from the view when stopping. Called automatically when the runnable session ends.

Returns

RunnableWrapper<TView, TResult>

A RunnableWrapper<TView, TResult> that wraps the view.

Type Parameters

TView

The type of view to make runnable.

TResult

The type of result data to extract.

Examples

// Make a TextField runnable with string result
var runnable = new TextField { Width = 40 }
    .AsRunnable(tf => tf.Text);

app.Run(runnable);
Console.WriteLine($"User entered: {runnable.Result}");
runnable.Dispose();

// Make a ColorPicker runnable with Color? result
var colorRunnable = new ColorPicker()
    .AsRunnable(cp => cp.SelectedColor);

app.Run(colorRunnable);
Console.WriteLine($"Selected: {colorRunnable.Result}");
colorRunnable.Dispose();

// Make a FlagSelector runnable with enum result
var flagsRunnable = new FlagSelector<SelectorStyles>()
    .AsRunnable(fs => fs.Value);

app.Run(flagsRunnable);
Console.WriteLine($"Selected styles: {flagsRunnable.Result}");
flagsRunnable.Dispose();

Remarks

This method wraps the view in a RunnableWrapper<TView, TResult> and automatically subscribes to IsRunningChanging to extract the result when the session stops.

The result is extracted before the view is disposed, ensuring all data is still accessible.

Exceptions

ArgumentNullException

Thrown if view or resultExtractor is null.

AsRunnable<TView>(TView)

Converts any View into a runnable without result extraction.

public static RunnableWrapper<TView, object> AsRunnable<TView>(this TView view) where TView : View

Parameters

view TView

The view to wrap. Cannot be null.

Returns

RunnableWrapper<TView, object>

A RunnableWrapper<TView, TResult> that wraps the view.

Type Parameters

TView

The type of view to make runnable.

Examples

// Make a view runnable without result extraction
var colorPicker = new ColorPicker();
var runnable = colorPicker.AsRunnable();

app.Run(runnable);

// Access the wrapped view directly to get the result
Console.WriteLine($"Selected: {runnable.WrappedView.SelectedColor}");
runnable.Dispose();

Remarks

Use this overload when you don't need to extract a typed result, but still want to run the view as a blocking session. The wrapped view can still be accessed via WrappedView after running.

Exceptions

ArgumentNullException

Thrown if view is null.