NanUI

Resources

Overview

The WinFormium framework provides a series of resource handlers that you can use to manage your resource files to facilitate browser loading. WinFormium’s resource handler is implemented based on CEF’s CefSchemeHandlerFactory and CefResourceHandler interfaces and encapsulates them to make them easier to use.

The principle of the resource handler is to map a URL address to a series of resource file sources. This URL address can be the http:// or https:// protocol, or it can be any customized protocol. When the browser loads this URL address, the resource handler will load the corresponding resource file based on the file source.

However, it should be noted that the resource handler is essentially implemented by intercepting the browser’s request and returning the resource file stream. Therefore, the resources provided by the resource handler can only be used in the current WinFormium application process and cannot be accessed outside the process. Yes, this is essentially different from running a Web Server locally (for example: IIS, Nginx, etc.). For example, if you register an address like https://my.resource.local, you can access this address in the current WinFormium application, but it cannot be accessed in other applications, even on the same computer.

Currently the WinFormium framework provides the following resource handlers:

Register resource handler

You can use AppBuilder to register resource handlers during the WinFormium application creation phase, or you can use ConfigureServices of the WinFormiumStartup class to configure services to register resource handlers during the WinFormium application configuration phase.

AppBuilder

class Program
{
     [STAThread]
     static void Main(){
         var builder = WinFormiumApp.CreateBuilder();

         var app = builder
         //...
         .UseLocalFileResource(new LocalFileResourceOptions
         {
             Scheme = "http",
             DomainName = "files.app.local",
             PhysicalFilePath = "<RESOURCE_FILES_DIR>"
         })
         .build();

         app.Run();

     }
}

WinFormiumStartup

class MyApp : WinFormiumStartup
{
     //...

     public override void ConfigureServices(IServiceCollection services)
     {
         //...
         services.AddLocalFileResource(new LocalFileResourceOptions
         {
             Scheme = "http",
             DomainName = "files.app.local",
             PhysicalFilePath = "<RESOURCE_FILES_DIR>"
         });
         //...
     }

}

Use resources

In the example in the previous section, we registered a local file resource handler (in two different ways). The URL address of this resource handler is http://files.app.local, and its resource file source in the local <RESOURCE_FILES_DIR> folder.

In a WinFormium form, you can use this URL to access resource files in a specified folder. For example, access: http://files.app.local/index.html, the local file resource handler will map the address to the local file <RESOURCE_FILES_DIR>\index.html. If there are multiple levels of directories, such as: http://files.app.local/subdir/index.html, the local file resource handler will map the address to the local file <RESOURCE_FILES_DIR>\subdir\index.html .

class MyWindow : Formium
{
     publicMyWindow()
     {
         Url = "http://files.app.local/"; // There is no need to specify index.html here
                                          //because the resource handler will automatically map to index.html.
                                          // This is achieved through the DefaultFileName property of ResourceSchemeHandlerOptions.
     }
}

See also