Method AsRunnable
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
viewTViewThe view to wrap. Cannot be null.
resultExtractorFunc<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
TViewThe type of view to make runnable.
TResultThe 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
vieworresultExtractoris 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
viewTViewThe view to wrap. Cannot be null.
Returns
- RunnableWrapper<TView, object>
A RunnableWrapper<TView, TResult> that wraps the view.
Type Parameters
TViewThe 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
viewis null.