|
| 1 | +# Configure WinFormium Applications |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +WinFormium uses the static method `CreateBuilder` of the `WinFormiumApp` class to create the application builder `AppBuilder`. In the builder you can set CEF's environment parameters, the application's run mode, the application's user data folder, the application's resource folder, etc. |
| 6 | + |
| 7 | +`AppBuilder` provides a series of methods for configuring WinFormium applications. After you configure these parameters, use the `Build` method to create a WinFormium application instance and use the `Run` method to start the application. |
| 8 | + |
| 9 | +```csharp |
| 10 | +var app = WinFormiumApp.CreateBuilder() |
| 11 | + // Configure the application here |
| 12 | + // ... |
| 13 | + .Build() |
| 14 | + .Run(); |
| 15 | +``` |
| 16 | + |
| 17 | +Please refer to this section to learn how to configure a WinFormium application. |
| 18 | + |
| 19 | +## Use startup configuration |
| 20 | + |
| 21 | +Use the `UseWinFormiumApp<T>()` generic method of the WinFormium application constructor `AppBuilder` to pass in the startup configuration class. For more information, please refer to ["Startup Configuration"](./Startup Configuration.md). |
| 22 | + |
| 23 | +## Enable built-in browser |
| 24 | + |
| 25 | +When using a WinFormium app, when a user clicks a link in the app, the WinFormium app automatically opens the system default browser to open the link. If you wish to use WinFormium's built-in browser window to open links, you can enable the built-in browser using the `AppBuilder.UseBuiltInBrowser()` method. |
| 26 | + |
| 27 | +```csharp |
| 28 | +var app = WinFormiumApp.CreateBuilder() |
| 29 | + .UseBuiltInBrowser() |
| 30 | + //... |
| 31 | + ; |
| 32 | +``` |
| 33 | + |
| 34 | +## Configure culture of the application |
| 35 | + |
| 36 | +WinFormium applications use the current system's language and culture by default, and you can configure the application's language and culture using the `AppBuilder.UseCulture()` method. The `UserCulture` method accepts a `string` type parameter, which is a language and culture identifier, for example `zh-CN` means Chinese Simplified, `en-US` means English United States. |
| 37 | + |
| 38 | +```csharp |
| 39 | +var app = WinFormiumApp.CreateBuilder() |
| 40 | + .UseCulture("en-US") |
| 41 | + //... |
| 42 | + ; |
| 43 | +``` |
| 44 | + |
| 45 | +## Singleton mode |
| 46 | + |
| 47 | +Use the `AppBuilder.UseSingleInstance()` method to configure the application to run in singleton mode. If the application is already running, you can choose to activate the already running application. The `UseSingleInstance` method accepts an `Aciton<OnApplicationInstanceRunningHanlder>` parameter. `OnApplicationInstanceRunningHanlder` contains the following parameters and methods: |
| 48 | + |
| 49 | +- int ProcessId { get; } |
| 50 | + The process ID of the application that is already running. |
| 51 | +- IntPtr MainWindowHandle { get; } |
| 52 | + The main form handle of the already running application. |
| 53 | +- Process RunningProcess { get; } |
| 54 | + The process object of an already running application. |
| 55 | +- void ActiveRunningInstance() |
| 56 | + Activate an already running application. |
| 57 | + |
| 58 | +```csharp |
| 59 | +var app = WinFormiumApp.CreateBuilder() |
| 60 | + .UseSingleApplicationInstanceMode(handler => |
| 61 | + { |
| 62 | + var retval = MessageBox.Show($"There is already an instance running: {handler.ProcessId}.\r\nDo you want to open its main form?", "Singleton mode is enabled", MessageBoxButtons.YesNo, MessageBoxIcon. Warning); |
| 63 | + if (retval == DialogResult.Yes) |
| 64 | + { |
| 65 | + handler.ActiveRunningInstance(); |
| 66 | + } |
| 67 | + }) |
| 68 | + //... |
| 69 | + ; |
| 70 | +``` |
| 71 | + |
| 72 | +## Configure services |
| 73 | + |
| 74 | +Use the `AppBuilder.UseServices()` method to configure the application's services, which accepts an `Action<IServiceCollection>` parameter, which contains a parameter of type `IServiceCollection` where you can register your service. Using `AppBuilder` the service will be registered in each process of the application. |
| 75 | + |
| 76 | +If the service needs to run in the main process, the service should be registered in the `WinFormiumMain` method of `WinFormiumStartup`. For more information about `WinFormiumStartup`, please refer to ["Startup Configuration"](./Startup.md). |
| 77 | + |
| 78 | +## Enable development tools menu |
| 79 | + |
| 80 | +Using DevTools debugging tools is very useful when designing WinFormium applications. You can open DevTools debugging tools in `Formium` using the `OpenDevTools` method. If you wish to enable the DevTools menu in your application, you can enable the DevTools menu using the `AppBuilder.UseDevToolsMenu()` method, which allows you to right-click anywhere to launch DevTools in the context menu. |
| 81 | + |
| 82 | +```csharp |
| 83 | +var app = WinFormiumApp.CreateBuilder() |
| 84 | + .UseDevToolsMenu() |
| 85 | + //... |
| 86 | + ; |
| 87 | +``` |
| 88 | + |
| 89 | +## Clear browser cache |
| 90 | + |
| 91 | +In the CEF interface, there is no method to clear the browser cache. Although WinFormium provides the `AppBuilder.ClearCache()` method to clear the browser cache, this is a very violent way to clear the cache. This method will delete the `Cache` folder in the CEF user data folder. Doing so The consequence is that all caches in your application will be cleared, including cookies, browser history, etc., so you cannot specify a certain type of cache to be deleted. |
| 92 | + |
| 93 | +Also, when CEF loads, it locks the user data folder so you cannot delete files in the user data folder, so you must clear the cache before CEF loads. |
| 94 | + |
| 95 | +```csharp |
| 96 | +var app = WinFormiumApp.CreateBuilder() |
| 97 | + .ClearCache() |
| 98 | + //... |
| 99 | + ; |
| 100 | +``` |
| 101 | + |
| 102 | +## See also |
| 103 | + |
| 104 | +- [Documentation](../Home.md) |
| 105 | +- [Startup Configuration](./Startup.md) |
| 106 | +- [Setup CEF](./Setup-CEF.md) |
| 107 | +- [The Subprocess](./Subprocess.md) |
0 commit comments