Giter VIP home page Giter VIP logo

blazordesktop's Introduction

GitHub license Contributor Covenant GitHub issues

Blazor Desktop

Blazor Desktop allows you to create desktop apps using Blazor. Apps run inside of a .NET generic host with a WPF window thats fully managed using a similar template to Blazor WASM. preview

Getting Started

The easiest way to get started with Blazor Desktop is to install the templates, you can do so using the dotnet cli as follows:

dotnet new install BlazorDesktop.Templates::8.0.0

Once you have the templates installed, you can either create a new project from the template either in Visual Studio in the template picker: create

Or, you can create a new project using the cli as follows:

dotnet new blazordesktop -n MyBlazorApp

Tips & Tricks

The Blazor Desktop template is set up very similar to the Blazor WASM template, you can see the Program.cs file here:

using BlazorDesktop.Hosting;
using HelloWorld.Components;
using Microsoft.AspNetCore.Components.Web;

var builder = BlazorDesktopHostBuilder.CreateDefault(args);

builder.RootComponents.Add<Routes>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");

if (builder.HostEnvironment.IsDevelopment())
{
    builder.UseDeveloperTools();
}

await builder.Build().RunAsync();

You can add root components just the same as well as add additional services your app may need just the same.

There are however a few additional APIs and services that have been made available to help when working in the context of a WPF window.

Window Utilities

The window can have most of its common configuration done through the BlazorDesktopHostBuilder.Window APIs before you build your app in Program.cs.

To change your window title:

builder.Window.UseTitle("Hello");

Window size:

builder.Window
    .UseWidth(1920)
    .UseHeight(1080)
    .UseMinWidth(1280)
    .UseMinHeight(720)
    .UseMaxWidth(2560)
    .UseMaxHeight(1440);

Disable window resizing:

builder.Window.UseResizable(false);

Disable the window frame (allows you to use your own window chrome inside of Blazor):

builder.Window.UseFrame(false);

And change your window icon (uses favicon.ico as the default, base directory is wwwroot):

builder.Window.UseIcon("myicon.ico");

It is also possible to configure these values through appsettings.json like so:

{
  "Window": {
    "Title": "Hello Blazor",
    "Height": 1080,
    "Width": 1920,
    "MinHeight": 720,
    "MinWidth": 1280,
    "MaxHeight": 1440,
    "MaxWidth": 2560,
    "Frame": false,
    "Resizable": false,
    "Icon": "hello.ico"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  }
}

The Window object itself is also made available inside of the DI container, so you can access all properties on it by using the inject Razor keyword or requesting it through the constructor of a class added as a service.

Custom Window Chrome & Draggable Regions

It is possible to make your own window chrome for Blazor Desktop apps. As an example base setup you could do the following:

Set up the window to have no frame in Program.cs:

builder.Window.UseFrame(false);

Using the base template, if you were to edit MainLayout.razor and add a -webkit-app-region: drag; style to the top bar like so:

@inherits LayoutComponentBase

<div class="page">
    <div class="sidebar">
        <NavMenu />
    </div>

    <main>
        <div class="top-row px-4" style="-webkit-app-region: drag;">
            <a href="https://learn.microsoft.com/aspnet/core/" target="_blank">About</a>
        </div>

        <article class="content px-4">
            @Body
        </article>
    </main>
</div>

The top bar becomes draggable, applying the -webkit-app-region: drag; property to anything will make it able to be used to drag the window.

In terms of handling things such as the close button, you can inject the Window into any page and interact from it there.

Here is an example changing MainLayout.razor:

@using BlazorDesktop.Wpf

@inherits LayoutComponentBase
@inject BlazorDesktopWindow window

<div class="page">
    <div class="sidebar">
        <NavMenu />
    </div>

    <main>
        <div class="top-row px-4" style="-webkit-app-region: drag;">
            <a href="https://learn.microsoft.com/aspnet/core/" target="_blank">About</a>
        </div>

        <article class="content px-4">
            @Body
        </article>
    </main>
</div>

@code {
    void CloseWindow()
    {
        window.Close();
    }
}

To support fullscreen mode, you should also hide your custom window chrome when in fullscreen. You can check the current fullscreen status using the IsFullscreen property on the window. You can also monitor for it changing using the OnFullscreenChanged event.

Issues

Under the hood, Blazor Desktop uses WebView2 which has limitations right now with composition. Due to this, if you disable the window border through the Window.UseFrame(false) API, the top edge of the window is unusable as a resizing zone for the window. However all the other corners and edges work.

blazordesktop's People

Contributors

andrewbabbitt97 avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

blazordesktop's Issues

Non Windows Support

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Can this project only run on winsdk? I tried to run the exe file directly, but to no avail

Expected behavior

No response

Steps to reproduce

No response

Exceptions or error messages (if any)

No response

Version number

No response

Anything else?

No response

[Nice to have] Multi Window Support

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

No

Describe the solution you'd like

Currently the BlazorDesktop platform runs on a single window, understandably so. For some advanced cases, it might be useful to have multi window support. Maybe having an extension to the NavigationManager or having something like an IWindowManager service where it will also handle the main window as well as managing other windows, this might be a great addition in the future.

@inject NavigationManager Navigation
@inject IWindowManager WindowManager

@code
{

    void OnOpenInNewWindow()
    {
        Navigation.NavigateToInNewWindow("/products");
    }

    void OnOpenNewWindow()
    {
        WindowManager.Create("window-identifier");
        var window = WindowManager.GetWindow("window-identifier");
        window.NavigateTo("/products");
    }
    
}

Additional context

No response

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.