Electron.net入门

Electron.Net 入门

安装环境

  • node.jsdownload
  • .netcoredownload
  • 能开发运行.netcore的环境,这里用的vsdownload

操作流程

  1. 新建一个工程
    Electron.net入门_第1张图片
  2. 选择.net core Electron.net入门_第2张图片
  3. 添加electron.net api的引用Electron.net入门_第3张图片
  4. 打开Startup.cs修改代码

        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseBrowserLink();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseStaticFiles();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
            if (HybridSupport.IsElectronActive)
            {
                // Open the Electron-Window here
                Task.Run(new Action(() =>
                {
                    ElectronBootstrap();
                }));
            }

        }

        public async void ElectronBootstrap()
        {
            //var browserWindow = await Electron.WindowManager.CreateWindowAsync(new BrowserWindowOptions
            //{
            //    Width = 1152,
            //    Height = 864,
            //    Frame = false,
            //    Show = true,
            //    Transparent = false
            //}, $"http://localhost:{ BridgeSettings.WebPort }/test.html");
            var browserWindow = await Electron.WindowManager.CreateWindowAsync(new BrowserWindowOptions
            {
                Width = 1152,
                Height = 864,
                Frame = false,
                Show = true,
                Transparent = false
            });
            browserWindow.OnReadyToShow += () => browserWindow.Show();
            browserWindow.SetTitle("Electron.NET API Demos");
            browserWindow.WebContents.OnCrashed += async (killed) =>
            {
                var options = new MessageBoxOptions("This process has crashed.")
                {
                    Type = MessageBoxType.info,
                    Title = "Renderer Process Crashed",
                    Buttons = new string[] { "Reload", "Close" }
                };
                var result = await Electron.Dialog.ShowMessageBoxAsync(options);

                if (result.Response == 0)
                {
                    browserWindow.Reload();
                }
                else
                {
                    browserWindow.Close();
                }
                //var browserWindow = await Electron.WindowManager.CreateWindowAsync(new BrowserWindowOptions
                //{
                //    Width = 1152,
                //    Height = 864,
                //    Show = false
                //});

                //browserWindow.OnReadyToShow += () => browserWindow.Show();
                //browserWindow.SetTitle("Electron.NET API Demos");
            };
            browserWindow.OnUnresponsive += async () => {
                var options = new MessageBoxOptions("This process is hanging.")
                {
                    Type = MessageBoxType.info,
                    Title = "Renderer Process Hanging",
                    Buttons = new string[] { "Reload", "Close" }
                };
                var result = await Electron.Dialog.ShowMessageBoxAsync(options);

                if (result.Response == 0)
                {
                    browserWindow.Reload();
                }
                else
                {
                    browserWindow.Close();
                }
            };
        }
  1. program.cs也添加如下的代码

 public static IWebHost BuildWebHost(string[] args)
        {
            return WebHost.CreateDefaultBuilder(args)
                .UseElectron(args)
                .UseStartup()
                .Build();
        }  
  1. 找到csproj文件添加下图中的一代码
    Electron.net入门_第4张图片
  2. 最后在工程文件夹下面执行
dotnet restore
dotnet electron init 
dotnet electronize start

8 确定安装了electron-package
安装代码

tip: 可以考虑淘宝镜像
结果如下图
Electron.net入门_第5张图片

你可能感兴趣的:(node-js,c#,桌面应用,dotnet)