Skip to content

Commit 8cc970c

Browse files
authored
[Blazor] Adds static web assets support for Blazor desktop (#31041)
* Wires up static web assets support on the WebViewManager.
1 parent 58b37d6 commit 8cc970c

18 files changed

+402
-2
lines changed

AspNetCore.sln

+15
Original file line numberDiff line numberDiff line change
@@ -1626,6 +1626,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "testassets", "testassets",
16261626
EndProject
16271627
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WpfTestApp", "src\Components\WebView\Platforms\Wpf\testassets\WpfTestApp\WpfTestApp.csproj", "{036C6BDA-7B69-4E8C-A921-822DA5972A56}"
16281628
EndProject
1629+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebviewAppShared", "src\Components\WebView\Samples\WebviewAppShared\WebviewAppShared.csproj", "{64C3BAC8-C4F8-466A-9E84-0400EE54B25A}"
1630+
EndProject
16291631
Global
16301632
GlobalSection(SolutionConfigurationPlatforms) = preSolution
16311633
Debug|Any CPU = Debug|Any CPU
@@ -7685,6 +7687,18 @@ Global
76857687
{036C6BDA-7B69-4E8C-A921-822DA5972A56}.Release|x64.Build.0 = Release|Any CPU
76867688
{036C6BDA-7B69-4E8C-A921-822DA5972A56}.Release|x86.ActiveCfg = Release|Any CPU
76877689
{036C6BDA-7B69-4E8C-A921-822DA5972A56}.Release|x86.Build.0 = Release|Any CPU
7690+
{64C3BAC8-C4F8-466A-9E84-0400EE54B25A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
7691+
{64C3BAC8-C4F8-466A-9E84-0400EE54B25A}.Debug|Any CPU.Build.0 = Debug|Any CPU
7692+
{64C3BAC8-C4F8-466A-9E84-0400EE54B25A}.Debug|x64.ActiveCfg = Debug|Any CPU
7693+
{64C3BAC8-C4F8-466A-9E84-0400EE54B25A}.Debug|x64.Build.0 = Debug|Any CPU
7694+
{64C3BAC8-C4F8-466A-9E84-0400EE54B25A}.Debug|x86.ActiveCfg = Debug|Any CPU
7695+
{64C3BAC8-C4F8-466A-9E84-0400EE54B25A}.Debug|x86.Build.0 = Debug|Any CPU
7696+
{64C3BAC8-C4F8-466A-9E84-0400EE54B25A}.Release|Any CPU.ActiveCfg = Release|Any CPU
7697+
{64C3BAC8-C4F8-466A-9E84-0400EE54B25A}.Release|Any CPU.Build.0 = Release|Any CPU
7698+
{64C3BAC8-C4F8-466A-9E84-0400EE54B25A}.Release|x64.ActiveCfg = Release|Any CPU
7699+
{64C3BAC8-C4F8-466A-9E84-0400EE54B25A}.Release|x64.Build.0 = Release|Any CPU
7700+
{64C3BAC8-C4F8-466A-9E84-0400EE54B25A}.Release|x86.ActiveCfg = Release|Any CPU
7701+
{64C3BAC8-C4F8-466A-9E84-0400EE54B25A}.Release|x86.Build.0 = Release|Any CPU
76887702
EndGlobalSection
76897703
GlobalSection(SolutionProperties) = preSolution
76907704
HideSolutionNode = FALSE
@@ -8488,6 +8502,7 @@ Global
84888502
{99EE7769-3C81-477B-B947-0A5CBCD5B27D} = {F0849E7E-61DB-4849-9368-9E7BC125DCB0}
84898503
{94D0D6F3-8632-41DE-908B-47A787D570FF} = {5241CF68-66A0-4724-9BAA-36DB959A5B11}
84908504
{036C6BDA-7B69-4E8C-A921-822DA5972A56} = {94D0D6F3-8632-41DE-908B-47A787D570FF}
8505+
{64C3BAC8-C4F8-466A-9E84-0400EE54B25A} = {D3B76F4E-A980-45BF-AEA1-EA3175B0B5A1}
84918506
EndGlobalSection
84928507
GlobalSection(ExtensibilityGlobals) = postSolution
84938508
SolutionGuid = {3E8720B3-DBDD-498C-B383-2CC32A054E8F}

src/Components/ComponentsNoDeps.slnf

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"src\\Components\\WebView\\Platforms\\Wpf\\testassets\\WpfTestApp\\WpfTestApp.csproj",
4545
"src\\Components\\WebView\\Samples\\BlazorWinFormsApp\\BlazorWinFormsApp.csproj",
4646
"src\\Components\\WebView\\Samples\\BlazorWpfApp\\BlazorWpfApp.csproj",
47+
"src\\Components\\WebView\\Samples\\WebviewAppShared\\WebviewAppShared.csproj",
4748
"src\\Components\\WebView\\WebView\\src\\Microsoft.AspNetCore.Components.WebView.csproj",
4849
"src\\Components\\WebView\\WebView\\test\\Microsoft.AspNetCore.Components.WebView.Test.csproj",
4950
"src\\Components\\Web\\src\\Microsoft.AspNetCore.Components.Web.csproj",

src/Components/WebView/Samples/BlazorWinFormsApp/BlazorWinFormsApp.csproj

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk.Razor">
1+
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
44
<TargetFramework>$(DefaultNetCoreTargetFramework)-windows</TargetFramework>
@@ -11,6 +11,10 @@
1111
<Reference Include="Microsoft.AspNetCore.Components.WebView.WindowsForms" />
1212
</ItemGroup>
1313

14+
<ItemGroup>
15+
<ProjectReference Include="..\WebviewAppShared\WebviewAppShared.csproj" />
16+
</ItemGroup>
17+
1418
<ItemGroup>
1519
<Content Update="wwwroot\**">
1620
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>

src/Components/WebView/Samples/BlazorWinFormsApp/Pages/Index.razor

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
@page "/"
22
@inject AppState AppState
3+
@using WebviewAppShared
34

45
<h3>Hello, world!</h3>
56

@@ -8,6 +9,9 @@
89
<button @onclick="IncrementCount">Increment</button>
910
<button @onclick="TriggerException">Throw</button>
1011

12+
<p>This is a shared component</p>
13+
<SharedComponent />
14+
1115
@code {
1216
void IncrementCount()
1317
{

src/Components/WebView/Samples/BlazorWinFormsApp/wwwroot/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<title>Blazor WinForms app</title>
88
<base href="/" />
99
<link href="css/app.css" rel="stylesheet" />
10+
<link href="BlazorWinFormsApp.styles.css" rel="stylesheet" />
1011
</head>
1112

1213
<body>

src/Components/WebView/Samples/BlazorWpfApp/BlazorWpfApp.csproj

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
<IsShippingPackage>false</IsShippingPackage>
88
</PropertyGroup>
99

10+
<ItemGroup>
11+
<ProjectReference Include="..\WebviewAppShared\WebviewAppShared.csproj" />
12+
</ItemGroup>
13+
1014
<ItemGroup>
1115
<Reference Include="Microsoft.AspNetCore.Components.WebView.Wpf" />
1216
</ItemGroup>

src/Components/WebView/Samples/BlazorWpfApp/Pages/Index.razor

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@page "/"
2-
2+
@using WebviewAppShared
33
// NOTE: The full namespace is included here to work around this bug: https://github.com/dotnet/aspnetcore/issues/30851
44
@inject BlazorWpfApp.AppState AppState
55

@@ -10,6 +10,9 @@
1010
<button @onclick="IncrementCount">Increment</button>
1111
<button @onclick="TriggerException">Throw</button>
1212

13+
<p>This is a shared component</p>
14+
<SharedComponent />
15+
1316
@code {
1417
void IncrementCount()
1518
{

src/Components/WebView/Samples/BlazorWpfApp/wwwroot/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<title>Blazor WPF app</title>
88
<base href="/" />
99
<link href="css/app.css" rel="stylesheet" />
10+
<link href="BlazorWpfApp.styles.css" rel="stylesheet" />
1011
</head>
1112

1213
<body>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using Microsoft.JSInterop;
2+
using System;
3+
using System.Threading.Tasks;
4+
5+
namespace WebviewAppShared
6+
{
7+
// This class provides an example of how JavaScript functionality can be wrapped
8+
// in a .NET class for easy consumption. The associated JavaScript module is
9+
// loaded on demand when first needed.
10+
//
11+
// This class can be registered as scoped DI service and then injected into Blazor
12+
// components for use.
13+
14+
public class ExampleJsInterop : IAsyncDisposable
15+
{
16+
private readonly Lazy<Task<IJSObjectReference>> moduleTask;
17+
18+
public ExampleJsInterop(IJSRuntime jsRuntime)
19+
{
20+
moduleTask = new(() => jsRuntime.InvokeAsync<IJSObjectReference>(
21+
"import", "./_content/WebviewAppShared/exampleJsInterop.js").AsTask());
22+
}
23+
24+
public async ValueTask<string> Prompt(string message)
25+
{
26+
var module = await moduleTask.Value;
27+
return await module.InvokeAsync<string>("showPrompt", message);
28+
}
29+
30+
public async ValueTask DisposeAsync()
31+
{
32+
if (moduleTask.IsValueCreated)
33+
{
34+
var module = await moduleTask.Value;
35+
await module.DisposeAsync();
36+
}
37+
}
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div class="my-component">
2+
This Blazor component is defined in the <strong>WebviewAppShared</strong> package.
3+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.my-component {
2+
border: 2px dashed red;
3+
padding: 1em;
4+
margin: 1em 0;
5+
background-image: url('background.png');
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Razor">
2+
3+
<PropertyGroup>
4+
<TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework>
5+
</PropertyGroup>
6+
7+
8+
<ItemGroup>
9+
<SupportedPlatform Include="browser" />
10+
</ItemGroup>
11+
12+
<ItemGroup>
13+
<Reference Include="Microsoft.AspNetCore.Components.Web" />
14+
</ItemGroup>
15+
16+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@using Microsoft.AspNetCore.Components.Web
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// This is a JavaScript module that is loaded on demand. It can export any number of
2+
// functions, and may import other JavaScript modules if required.
3+
4+
export function showPrompt(message) {
5+
return prompt(message, 'Type anything here');
6+
}

src/Components/WebView/WebView/src/Microsoft.AspNetCore.Components.WebView.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
<Reference Include="Microsoft.Extensions.Configuration.Json" />
3333
<Reference Include="Microsoft.Extensions.Configuration.Binder" />
3434
<Reference Include="Microsoft.Extensions.FileProviders.Embedded" />
35+
<Reference Include="Microsoft.Extensions.FileProviders.Composite" />
3536
<Reference Include="Microsoft.Extensions.Logging" />
3637

3738
<ProjectReference

0 commit comments

Comments
 (0)