You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: aspnetcore/blazor/webassembly-native-dependencies.md
+71-72Lines changed: 71 additions & 72 deletions
Original file line number
Diff line number
Diff line change
@@ -36,64 +36,62 @@ Prebuilt dependencies typically must be built using the same version of Emscript
36
36
37
37
## Use native code
38
38
39
-
Add a simple native C function to a Blazor WebAssembly app:
39
+
This section demonstrates how to add a simple native C function to a Blazor WebAssembly app.
40
40
41
-
1. Create a new Blazor WebAssembly project.
42
-
1. Add a `Test.c` file to the project.
43
-
1. Add a C function for computing factorials.
41
+
Create a new Blazor WebAssembly project.
44
42
45
-
`Test.c`:
43
+
Add a `Test.c` file to the project with a C function for computing factorials.
46
44
47
-
```c
48
-
intfact(int n)
49
-
{
50
-
if (n == 0) return 1;
51
-
return n * fact(n - 1);
52
-
}
53
-
```
45
+
`Test.c`:
54
46
55
-
1. Add a `NativeFileReference` for `Test.c` in the app's project file:
47
+
```c
48
+
intfact(int n)
49
+
{
50
+
if (n == 0) return 1;
51
+
return n * fact(n - 1);
52
+
}
53
+
```
56
54
57
-
```xml
58
-
<ItemGroup>
59
-
<NativeFileReference Include="Test.c" />
60
-
</ItemGroup>
61
-
```
55
+
Add a `NativeFileReference` MS Build item for `Test.c` in the app's project file (`.csproj`):
62
56
63
-
1. In a Razor component, add a <xref:System.Runtime.InteropServices.DllImportAttribute> for the `fact` function in the generated `Test` library and call the `fact` method from .NET code in the component.
57
+
```xml
58
+
<ItemGroup>
59
+
<NativeFileReference Include="Test.c" />
60
+
</ItemGroup>
61
+
```
64
62
65
-
`Pages/NativeCTest.razor`:
63
+
In a Razor component, add a [`[DllImport]` attribute](xref:System.Runtime.InteropServices.DllImportAttribute) for the `fact` function in the generated `Test` library and call the `fact` method from .NET code in the component.
66
64
67
-
```razor
68
-
@page "/native-c-test"
69
-
@using System.Runtime.InteropServices
65
+
`Pages/NativeCTest.razor`:
70
66
71
-
<PageTitle>Native C</PageTitle>
67
+
```razor
68
+
@page "/native-c-test"
69
+
@using System.Runtime.InteropServices
72
70
73
-
<h1>Native C Test</h1>
71
+
<PageTitle>Native C</PageTitle>
74
72
75
-
<p>
76
-
@@fact(3) result: @fact(3)
77
-
</p>
73
+
<h1>Native C Test</h1>
78
74
79
-
@code {
80
-
[DllImport("Test")]
81
-
static extern int fact(int n);
82
-
}
83
-
```
75
+
<p>
76
+
@@fact(3) result: @fact(3)
77
+
</p>
78
+
79
+
@code {
80
+
[DllImport("Test")]
81
+
static extern int fact(int n);
82
+
}
83
+
```
84
84
85
85
When you build the app with the .NET WebAssembly build tools installed, the native C code is compiled and linked into the .NET WebAssembly runtime (`dotnet.wasm`). After the app is built, run the app to see the rendered factorial value.
86
86
87
87
## C++ managed method callbacks
88
88
89
-
Label managed methods that are passed to C++ with the `[UnmanagedCallersOnly]` attribute.
90
-
91
-
The method marked with the `[UnmanagedCallersOnly]` attribute must be `static`. To call an instance method in a Razor component, pass a `GCHandle` for the instance to C++ and then pass it back to native. Alternatively, use some other method to identify the instance of the component.
89
+
Label managed methods that are passed to C++ with the [`[UnmanagedCallersOnly]` attribute](xref:System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute). The method marked with the attribute must be `static`. To call an instance method in a Razor component, pass a <xref:System.Runtime.InteropServices.GCHandle> for the instance to C++ and then pass it back to native. Alternatively, use some other method to identify the instance of the component.
92
90
93
-
The method marked with `[DllImport]` must use a C# 9.0 function pointer rather than a delegate type for the callback argument.
91
+
The method marked with the [`[DllImport]`attribute](xref:System.Runtime.InteropServices.DllImportAttribute)must use a [function pointer (C# 9.0 or later)](/dotnet/csharp/language-reference/proposals/csharp-9.0/function-pointers) rather than a delegate type for the callback argument.
94
92
95
93
> [!NOTE]
96
-
> For C# function pointer types in `[DllImport]` methods, use `IntPtr` in the method signature on the managed side instead of `delegate *unmanaged<int, void>`. For more information, see [[WASM] callback from native code to .NET: Parsing function pointer types in signatures is not supported (dotnet/runtime #56145)](https://github.com/dotnet/runtime/issues/56145).
94
+
> For C# function pointer types in [`[DllImport]`](xref:System.Runtime.InteropServices.DllImportAttribute) methods, use <xref:System.IntPtr> in the method signature on the managed side instead of `delegate *unmanaged<int, void>`. For more information, see [[WASM] callback from native code to .NET: Parsing function pointer types in signatures is not supported (`dotnet/runtime`#56145)](https://github.com/dotnet/runtime/issues/56145).
97
95
98
96
## Package native dependencies in a NuGet package
99
97
@@ -103,55 +101,56 @@ NuGet packages can contain native dependencies for use on WebAssembly. These lib
103
101
104
102
[SkiaSharp](https://github.com/mono/SkiaSharp) is a cross-platform 2D graphics library for .NET based on the native [Skia graphics library](https://skia.org/) with support for Blazor WebAssembly.
105
103
106
-
To use SkiaSharp in a Blazor WebAssembly app:
104
+
The section demonstrates how to implement SkiaSharp in a Blazor WebAssembly app.
107
105
108
-
1.Add a package reference to the [`SkiaSharp.Views.Blazor`](https://www.nuget.org/packages/SkiaSharp.Views.Blazor) package in a Blazor WebAssembly project. Use Visual Studio's process for adding packages to an app (**Manage NuGet Packages** with **Include prerelease** selected) or execute the [`dotnet add package`](/dotnet/core/tools/dotnet-add-package) command in a command shell:
106
+
Add a package reference to the [`SkiaSharp.Views.Blazor`](https://www.nuget.org/packages/SkiaSharp.Views.Blazor) package in a Blazor WebAssembly project. Use Visual Studio's process for adding packages to an app (**Manage NuGet Packages** with **Include prerelease** selected) or execute the [`dotnet add package`](/dotnet/core/tools/dotnet-add-package) command in a command shell with the `--prerelease` option:
1.Build the app, which might take several minutes. Run the app and navigate to the `NativeDependencyExample` component at `/native-dependency-example`.
151
+
Build the app, which might take several minutes. Run the app and navigate to the `NativeDependencyExample` component at `/native-dependency-example`.
0 commit comments