Skip to content

Configuration

mattkol edited this page Jul 13, 2021 · 1 revision

By default EdgeSharp comes with its own default configuration. Which can be completely altered or overridden depending on your needs and preferences. There are a few ways on how you can configure EdgeSharp.

The default values will be used.

This is by far the easiest way to configure EdgeSharp.

using EdgeSharp;
using EdgeSharp.Core.Configuration;
using EdgeSharp.Core.Defaults;
using System;

namespace MyEdgeSharpApp
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            // create a configuration with preset defaults
            var config = new Configuration();

            // your configuration
            config.StartUrl = "https://developer.microsoft.com/en-us/microsoft-edge/webview2/";
            config.WindowOptions.Title = "My Awesome EdgeSharp App!";
            //..

            AppBuilder
            .Create()
            .UseConfig<IConfiguration>(config)
            .UseApp<SampleEdgeSharpApp>()
            .Build()
            .Run(args);
        }
    }
}

If you want complete control or want to work from a blank slate, with your own default settings. Implementing a custom Configuration class is your best option.

Create a new class that inherits from IConfiguration.

using EdgeSharp.Core.Configuration;

namespace MyEdgeSharpApp
{
    class MyConfiguration : IConfiguration
    {
    }
}

After implementing the interface, the class will look something like this.

using EdgeSharp.Core.Configuration;

namespace MyEdgeSharpApp
{
    class MyConfiguration : IConfiguration
    {
        public string StartUrl { get => throw new System.NotImplementedException(); set => throw new System.NotImplementedException(); }
        public string SdkVersion { get => throw new System.NotImplementedException(); set => throw new System.NotImplementedException(); }
        ---
    }
}

You can now add your own values to each property.

Understand that EdgeSharp can't work without any defined settings, you will need to provide them. It might be useful to look at the default values that EdgeSharp uses. EdgeSharp.Core/Defaults/Configuration.cs

using EdgeSharp.Core.Configuration;
using EdgeSharp.Core.Defaults;

namespace MyEdgeSharpApp
{
    class MyConfiguration : IConfiguration
    {
        public string StartUrl { get; set; }
        public WindowOptions WindowOptions { get; set; }
        // ...

        public MyConfiguration() : base()
        {
            WindowOptions = new WindowOptions();
            StartUrl = "https://developer.microsoft.com/en-us/microsoft-edge/webview2/";
            WindowOptions.Title = "My Awesome EdgeSharp App!";
            //..
        }
    }
}

Of course there are different ways to implement this interface, it all depends on your programming style and preference.

using EdgeSharp.Core;
using EdgeSharp.Core.Configuration;

namespace My_EdgeSharp_App
{
    class MyConfiguration : IEdgeSharpConfiguration
    {
        public string AppName { get; set; } = "My Awesome EdgeSharp App";
        public EdgeSharpPlatform Platform { get; set; } = EdgeSharpRuntime.Platform;
        //..
    }
}

Now reference that class in the application builder with the UseConfiguration option.

using EdgeSharp;
using System;

namespace MyEdgeSharpApp
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            AppBuilder
            .Create()
            .UseConfig<MyConfiguration>()
            .UseApp<SampleEdgeSharpApp>()
            .Build()
            .Run(args);
        }
    }
}