Table of Contents

Method RunView

Namespace
Terminal.Gui.App
Assembly
Terminal.Gui.dll

RunView<TView, TResult>(IApplication, TView, Func<TView, TResult?>, Func<Exception, bool>?)

Runs any View as a runnable session, extracting a typed result via a function.

public static TResult? RunView<TView, TResult>(this IApplication app, TView view, Func<TView, TResult?> resultExtractor, Func<Exception, bool>? errorHandler = null) where TView : View

Parameters

app IApplication

The application instance. Cannot be null.

view TView

The view to run as a blocking session. Cannot be null.

resultExtractor Func<TView, TResult>

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

errorHandler Func<Exception, bool>

Optional handler for unhandled exceptions during the session.

Returns

TResult

The extracted result, or null if the session was canceled.

Type Parameters

TView

The type of view to run.

TResult

The type of result data to extract.

Examples

var app = Application.Create();
app.Init();

// Run a TextField and get the entered text
var text = app.RunView(
    new TextField { Width = 40 },
    tf => tf.Text);
Console.WriteLine($"You entered: {text}");

// Run a ColorPicker and get the selected color
var color = app.RunView(
    new ColorPicker(),
    cp => cp.SelectedColor);
Console.WriteLine($"Selected color: {color}");

// Run a FlagSelector and get the selected flags
var flags = app.RunView(
    new FlagSelector<SelectorStyles>(),
    fs => fs.Value);
Console.WriteLine($"Selected styles: {flags}");

app.Shutdown();

Remarks

This method wraps the view in a RunnableWrapper<TView, TResult>, runs it as a blocking session, and returns the extracted result. The wrapper is NOT disposed automatically; the caller is responsible for disposal.

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

Exceptions

ArgumentNullException

Thrown if app, view, or resultExtractor is null.

RunView<TView>(IApplication, TView, Func<Exception, bool>?)

Runs any View as a runnable session without result extraction.

public static TView RunView<TView>(this IApplication app, TView view, Func<Exception, bool>? errorHandler = null) where TView : View

Parameters

app IApplication

The application instance. Cannot be null.

view TView

The view to run as a blocking session. Cannot be null.

errorHandler Func<Exception, bool>

Optional handler for unhandled exceptions during the session.

Returns

TView

The view that was run, allowing access to its state after the session ends.

Type Parameters

TView

The type of view to run.

Examples

var app = Application.Create();
app.Init();

// Run a ColorPicker without automatic result extraction
var colorPicker = new ColorPicker();
app.RunView(colorPicker);

// Access the view's state directly
Console.WriteLine($"Selected: {colorPicker.SelectedColor}");

app.Shutdown();

Remarks

This method wraps the view in a RunnableWrapper<TView, TResult> and runs it as a blocking session. The wrapper is NOT disposed automatically; the caller is responsible for disposal.

Use this overload when you don't need automatic result extraction, but still want the view to run as a blocking session. Access the view's properties directly after running.

Exceptions

ArgumentNullException

Thrown if app or view is null.