Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blazor Hybrid WinForms tutorial (PR3) #24989

Merged
merged 6 commits into from
Feb 15, 2022
Merged

Conversation

guardrex
Copy link
Collaborator

@guardrex guardrex commented Feb 14, 2022

Addresses #24956

Internal Review Topic

Naming ...

  • The final app naming is "Windows Forms" before "Blazor" to match what we have in the .NET MAUI case. ".NET MAUI Blazor app" ... so ... "Windows Forms Blazor app" is the way that I went. Obviously (and given our earlier discussion on official names of Blazor Hybrid apps still a bit of a WIP), I'm just trying to maintain consistency.
  • WRT "WinForms" vs "Windows Forms," I checked the Windows Forms doc set to see what they do. They highly prefer the formal name "Windows Forms." They don't totally avoid "WinForms." They use it rarely ... e.g., in some sample app scenarios. I took the same approach here. "WinForms" is used in sample naming but not in the topic, which consistently goes with "Windows Forms."

@guardrex guardrex mentioned this pull request Feb 14, 2022
25 tasks
Copy link
Member

@danroth27 danroth27 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for putting this together, @guardrex.

I would prefer to simplify this tutorial to setup a single Razor component instead of the entire default Blazor project template. That should allow us to give inline instructions instead of copying from other projects.

Here are the high-level steps that I'd like to use:

  1. Create a new Windows Forms app
  2. Install the Microsoft.AspNetCore.Components.WebView.WindowsForms using the NuGet package manager
  3. Update the .csproj file to change the SDK to Microsoft.NET.Sdk.Razor
  4. Add an _Imports.razor file with @using Microsoft.AspNetCore.Components.Web
  5. Add a wwwroot folder
  6. Add wwwroot\index.html with the following:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>WinFormsApp1</title>
    <base href="/" />
    <link href="css/app.css" rel="stylesheet" />
    <link href="WinFormsApp1.styles.css" rel="stylesheet" />
</head>

<body>

    <div id="app">Loading...</div>

    <div id="blazor-error-ui">
        An unhandled error has occurred.
        <a href="" class="reload">Reload</a>
        <a class="dismiss">🗙</a>
    </div>

    <script src="_framework/blazor.webview.js"></script>

</body>

</html>
  1. Add wwwroot/css/app.css with the following
html, body {
    font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
}

.valid.modified:not([type=checkbox]) {
    outline: 1px solid #26b050;
}

.invalid {
    outline: 1px solid red;
}

.validation-message {
    color: red;
}

#blazor-error-ui {
    background: lightyellow;
    bottom: 0;
    box-shadow: 0 -1px 2px rgba(0, 0, 0, 0.2);
    display: none;
    left: 0;
    padding: 0.6rem 1.25rem 0.7rem 1.25rem;
    position: fixed;
    width: 100%;
    z-index: 1000;
}

    #blazor-error-ui .dismiss {
        cursor: pointer;
        position: absolute;
        right: 0.75rem;
        top: 0.5rem;
    }
  1. Update the .csproj file to copy wwwroot\** to the output directory:

I'm following up on how we can get rid of this step but for now I think it's needed.

<ItemGroup>
  <Content Update="wwwroot\**" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
  1. Add a Counter.razor file with the default Counter component code.
<h1>Counter</h1>

<p>Current count: @currentCount</p>

<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

@code {
    private int currentCount = 0;

    private void IncrementCount()
    {
        currentCount++;
    }
}
  1. Double-click on Form1.cs to open the designer.
  2. Use View > Toolbox, and find BlazorWebView
  3. Drag BlazorWebView into the Form1 designer.
  4. Right-click on this new item and choose Properties. Change its Dock value to Fill by clicking on the central rectangle:
  5. Inside the Form1 constructor, underneath InitializeComponent(), add the following code:
var services = new ServiceCollection();
services.AddBlazorWebView();
blazorWebView1.HostPage = "wwwroot\\index.html";
blazorWebView1.Services = services.BuildServiceProvider();
blazorWebView1.RootComponents.Add<Counter>("#app");
  1. Run the app.

@guardrex
Copy link
Collaborator Author

guardrex commented Feb 15, 2022

I would prefer to simplify this tutorial to setup a single Razor component instead of the entire default Blazor project template. That should allow us to give inline instructions instead of copying from other projects.

Sure thing. 👍

Due to the need to rewrite this, I probably won't have the WPF tutorial up until Wednesday morning. When I reach it, I'll write the WPF tutorial in the same vein as the rewrite guidance.

@guardrex
Copy link
Collaborator Author

guardrex commented Feb 15, 2022

❓s on the updates ...

  1. Do you want to include any further namespaces in the _Imports.razor file?
  2. Is it ok to name the project WinFormsBlazor (on the PR)? Seems nicer to me than WinFormsApp1. btw- If we keep this naming here, I'll update the MAUI tutorial later to use MauiBlazor (or similar naming). It's currently using kind'a crappy-er-ish 😄 MauiApp1 default naming.
  3. Leave the stylesheet link for WinFormsBlazor.styles.css in index.html?
  4. Do you want to send me a rounded-corners image for running-app.png? If so, please snap it with a little extra space around the image on a white or black background to facilitate the Photoshopping step.

@danroth27
Copy link
Member

  • Do you want to include any further namespaces in the _Imports.razor file?

I don't believe other namespaces are required, so let's keep it minimalistic.

  • Is it ok to name the project WinFormsBlazor (on the PR)? Seems nicer to me than WinFormsApp1. btw- If we keep this naming here, I'll update the MAUI tutorial later to use MauiBlazor (or similar naming). It's currently using kind'a crappy-er-ish 😄 MauiApp1 default naming.

Sure, WinFormsBlazor is fine.

  • Leave the stylesheet link for WinFormsBlazor.styles.css in index.html?

Yeah, let's leave it. It is a bit weird that it doesn't get generated if you don't have any component specific styles, but I think you'll need it pretty quickly.

  • Do you want to send me a rounded-corners image for running-app.png? If so, please snap it with a little extra space around the image on a white or black background to facilitate the Photoshopping step.

Nah, your Win10 snapshots are fine.

@guardrex
Copy link
Collaborator Author

I'll be on stand-by for feedback and updates.

I'm around the 🏠 all day right into the evening ... WELL ... until the US Curling Teams get on the ice!

Go Team Tabitha!

tabitha

Go Team Shuster!

shuster

Copy link
Member

@danroth27 danroth27 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

Co-authored-by: Daniel Roth <daroth@microsoft.com>
@guardrex guardrex merged commit c8e50a3 into main Feb 15, 2022
@guardrex guardrex deleted the guardrex/blazor-hybrid-pr3 branch February 15, 2022 18:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants