Scripting in Application (C#)
In this article: The scripting language based on C# which takes advantage of power of .NET. Please refer to here for more information about language.Language Version
- C# 10: ULogViewer 2.0
- C# 11: ULogViewer 3.0
- C# 12: ULogViewer 4.0
Namespaces
The following namespaces are imported by default:- System
- System.Collections.Generic
- System.Linq
- Microsoft.Extensions.Logging
-
CarinaStudio
To access types and functions provided by CarinaStudio.AppBase.Core.
-
CarinaStudio.Collections
To access types and functions provided by CarinaStudio.AppBase.Core.
-
CarinaStudio.AppSuite.ControlsDeprecated in ULogViewer 3.0+
To access types and functions of user interface.
-
CarinaStudio.AppSuite.ScriptingDeprecated in ULogViewer 3.0+
To access types and functions of script runtime environment.
- CarinaStudio.ULogViewerDeprecated in ULogViewer 3.0+
CarinaStudio.* namespaces should not be imported manually starting from ULogViewer 3.0. All necessary types will be imported automatically.
β¬οΈ Back to top
Global Variables
There are 3 global variables in script runtime environment:App
Allows script accessing the application-level functions. The followings are members of App:- ExecuteCommand(string command, CancellationToken cancellationToken): intULogViewer 3.0+
-
ExecuteCommand(string command, Action<Process, CancellationToken> action, CancellationToken cancellationToken): intULogViewer 3.0+
Execute external command and get exit code of command.action is a call-back function to let you send data to standard input and read data from standard output/error from process of command, for example:App.ExecuteCommand("my_command", (process, cancellationToken) =>
{
var reader = process.StandardOutput;
var line = reader.ReadLine();
while (line is not null)
{
// Process the read line...
line = reader.ReadLine();
}
}); -
FindCommandPath(string command, CancellationToken cancellationToken): string?ULogViewer 3.0+
Find absolute path to the given command. Will returns null if command cannot be found.
-
GetFormattedString(string key, params object?[] args): string?
Get format defined in application resources and generate formatted string with given arguments.
- GetString(string key): string?
-
GetString(string key, string? defaultString): string?
Get string resource with given key.
- GetStringNonNull(string key): string
-
GetStringNonNull(string key, string defaultString): string
Get string resource with given key which makes sure that the string won't be null.
-
IsDebugMode: boolULogViewer 3.0+
Check whether application is running in debug mode or not.
-
IsMainThread: bool
Check whether current thread is the main thread of application or not.
-
MainThreadSynchronizationContext: SynchronizationContext
Get System.Threading.SynchronizationContext of main thread of application.
CancellationToken
System.Threading.CancellationToken for checking whether cancellation of script running has been requested or not.
You can use IsCancellationRequested global property directly starting from ULogViewer 3.0.
Context
Allows accessing functions according to purpose of script. The actual instance of context is also decided by the purpose of script. The followings are basic members of Context:-
Data: IDictionary<string, object>
A dictionary which stores custom data. The data may be transferred across scripts if scripts belong to same set of same purpose.
- GetString(string key): string?ULogViewer 3.0+
-
GetString(string key, string? defaultString): string?ULogViewer 3.0+
Get the string with given key defined in context by calling PrepareStrings(). Will fallback to string defined in application if it cannot be found in context, or fallback to defaultString if string cannot be found in application either.
-
Logger: ILogger
Allow writing log to log buffer and file of ULogViewer.You can use Log*() global methods directly starting from ULogViewer 3.0.
- PrepareStrings(Action<IDictionary<string, string>> preparation)ULogViewer 3.0+
-
PrepareStrings(string? cultureName, Action<IDictionary<string, string>> preparation)ULogViewer 3.0+
Setup string table for specific culture. The format of cultureName is languagecode2-country/regioncode2. For ex, en-US. You can also pass null to cultureName to define default string table.preparation is a call-back function to setup string table. For example:Context.PrepareStrings(null, table =>
{
table["Message"] = "Hello";
});
Context.PrepareStrings("zh-TW", table =>
{
table["Message"] = "εε";
});String tables are dedicated for each context. Therefore, string tables setup through PrepareStrings() can only be accessed by its context. - ShowMultipleItemsSelectionDialog(string? message, IList items): IList<int>ULogViewer 4.0+
-
ShowMultipleItemsSelectionDialog(string? message, IList items, int defaultItemIndex): IList<int>ULogViewer 4.0+
Show dialog to let user select one or more items. The returned value is the list of indices of selected items.
- ShowSingleItemSelectionDialog(string? message, IList items): intULogViewer 4.0+
-
ShowSingleItemSelectionDialog(string? message, IList items, int defaultItemIndex): intULogViewer 4.0+
Show dialog to let user select one item. The returned value is the index of selected item, or -1 if nothing is selected.
- ShowMessageDialog(string? message): MessageDialogResult
- ShowMessageDialog(string? message, MessageDialogIcon icon): MessageDialogResult
-
ShowMessageDialog(string? message, MessageDialogIcon icon, MessageDialogButtons buttons): MessageDialogResult
Shows message dialog and wait for result selected by user.Values defined in MessageDialogIcon:
- Error
- Information (default)
- Question
- Success
- Warning
- OK (default)
- OKCancel
- YesNo
- YesNoCancel
- Cancel
- No
- OK
- Yes
- ShowTextInputDialog(string? message): string?
-
ShowTextInputDialog(string? message, string? initText): string?
Shows text input dialog and wait for text input by user.