Skip to content

Commit ee25cfd

Browse files
authored
Merge pull request #77 from egvijayanand/working
Native Embedding WinUI 3 Sample
2 parents b9d6b4f + cc2cfdf commit ee25cfd

30 files changed

+491
-1
lines changed

README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ Completed samples will be uploaded into this repository so that it is accessible
44

55
Join me on [**Developer Thoughts**](https://egvijayanand.in/), an exclusive blog for .NET MAUI and Blazor, for articles on working with these samples.
66

7-
Available under the `src` directory:
7+
_Note: Samples are in the process of migrating to the latest framework version, so there would be a change in the directory structure, so kindly bear with this._
8+
9+
Made available in the `src\NET_9\` directory:
10+
* `EmbeddedWindows` - .NET MAUI Page embedded in a Native WinUI 3 App, targeting .NET 9 (`net9.0-windows10.0.19041.0`)
11+
- Refer to this [article](https://egvijayanand.in/2024/02/29/dotnet-maui-native-embedding/) for working with this sample
12+
13+
Made available in the the `src\` directory:
814

915
* `TestApp` - .NET MAUI Shell sample
1016
* `MenuApp` - .NET MAUI sample app with multi-level menu definition in both XAML and C# _(Will work only on Desktop form factor)_
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.5.33103.201
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EmbeddedWindows", "EmbeddedWindows\EmbeddedWindows.csproj", "{A96B3752-FF0F-402B-A85A-634E934202F1}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MauiLib", "MauiLib\MauiLib.csproj", "{E93EB175-0FD3-4BFD-B81A-CDAF33998932}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionProperties) = preSolution
12+
HideSolutionNode = FALSE
13+
EndGlobalSection
14+
GlobalSection(ExtensibilityGlobals) = postSolution
15+
SolutionGuid = {33E05B3D-B1D3-476F-9E8D-E1E471B55750}
16+
EndGlobalSection
17+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
18+
Debug|Any CPU = Debug|Any CPU
19+
Debug|x64 = Debug|x64
20+
Debug|x86 = Debug|x86
21+
Release|Any CPU = Release|Any CPU
22+
Release|x64 = Release|x64
23+
Release|x86 = Release|x86
24+
EndGlobalSection
25+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
26+
{A96B3752-FF0F-402B-A85A-634E934202F1}.Debug|Any CPU.ActiveCfg = Debug|x86
27+
{A96B3752-FF0F-402B-A85A-634E934202F1}.Debug|Any CPU.Build.0 = Debug|x86
28+
{A96B3752-FF0F-402B-A85A-634E934202F1}.Debug|x64.ActiveCfg = Debug|x64
29+
{A96B3752-FF0F-402B-A85A-634E934202F1}.Debug|x64.Build.0 = Debug|x64
30+
{A96B3752-FF0F-402B-A85A-634E934202F1}.Debug|x86.ActiveCfg = Debug|x86
31+
{A96B3752-FF0F-402B-A85A-634E934202F1}.Debug|x86.Build.0 = Debug|x86
32+
{A96B3752-FF0F-402B-A85A-634E934202F1}.Release|Any CPU.ActiveCfg = Release|x86
33+
{A96B3752-FF0F-402B-A85A-634E934202F1}.Release|Any CPU.Build.0 = Release|x86
34+
{A96B3752-FF0F-402B-A85A-634E934202F1}.Release|x64.ActiveCfg = Release|x64
35+
{A96B3752-FF0F-402B-A85A-634E934202F1}.Release|x64.Build.0 = Release|x64
36+
{A96B3752-FF0F-402B-A85A-634E934202F1}.Release|x86.ActiveCfg = Release|x86
37+
{A96B3752-FF0F-402B-A85A-634E934202F1}.Release|x86.Build.0 = Release|x86
38+
{E93EB175-0FD3-4BFD-B81A-CDAF33998932}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
39+
{E93EB175-0FD3-4BFD-B81A-CDAF33998932}.Debug|Any CPU.Build.0 = Debug|Any CPU
40+
{E93EB175-0FD3-4BFD-B81A-CDAF33998932}.Debug|x64.ActiveCfg = Debug|Any CPU
41+
{E93EB175-0FD3-4BFD-B81A-CDAF33998932}.Debug|x64.Build.0 = Debug|Any CPU
42+
{E93EB175-0FD3-4BFD-B81A-CDAF33998932}.Debug|x86.ActiveCfg = Debug|Any CPU
43+
{E93EB175-0FD3-4BFD-B81A-CDAF33998932}.Debug|x86.Build.0 = Debug|Any CPU
44+
{E93EB175-0FD3-4BFD-B81A-CDAF33998932}.Release|Any CPU.ActiveCfg = Release|Any CPU
45+
{E93EB175-0FD3-4BFD-B81A-CDAF33998932}.Release|Any CPU.Build.0 = Release|Any CPU
46+
{E93EB175-0FD3-4BFD-B81A-CDAF33998932}.Release|x64.ActiveCfg = Release|Any CPU
47+
{E93EB175-0FD3-4BFD-B81A-CDAF33998932}.Release|x64.Build.0 = Release|Any CPU
48+
{E93EB175-0FD3-4BFD-B81A-CDAF33998932}.Release|x86.ActiveCfg = Release|Any CPU
49+
{E93EB175-0FD3-4BFD-B81A-CDAF33998932}.Release|x86.Build.0 = Release|Any CPU
50+
EndGlobalSection
51+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<Application
3+
x:Class="EmbeddedWindows.App"
4+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
5+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
6+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
7+
xmlns:local="using:EmbeddedWindows"
8+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
9+
mc:Ignorable="d">
10+
<Application.Resources>
11+
<ResourceDictionary>
12+
<ResourceDictionary.MergedDictionaries>
13+
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
14+
<!-- Other merged dictionaries here -->
15+
</ResourceDictionary.MergedDictionaries>
16+
<!-- Other app resources here -->
17+
<Color x:Key="Primary">#512BD4</Color>
18+
19+
<SolidColorBrush x:Key="PrimaryBrush" Color="{StaticResource Primary}" />
20+
<SolidColorBrush x:Key="WhiteBrush" Color="White" />
21+
<SolidColorBrush x:Key="BlackBrush" Color="Black" />
22+
23+
<x:Double x:Key="AppFontSize">14</x:Double>
24+
25+
<Style x:Key="MyLabel" TargetType="TextBlock">
26+
<Setter Property="Foreground"
27+
Value="{StaticResource PrimaryBrush}" />
28+
</Style>
29+
30+
<Style x:Key="Action" TargetType="Button">
31+
<Setter Property="FontSize"
32+
Value="{StaticResource AppFontSize}" />
33+
<Setter Property="Padding"
34+
Value="14,10" />
35+
</Style>
36+
37+
<Style x:Key="PrimaryAction"
38+
TargetType="Button"
39+
BasedOn="{StaticResource Action}">
40+
<Setter Property="Background"
41+
Value="{StaticResource PrimaryBrush}" />
42+
<Setter Property="CornerRadius"
43+
Value="8" />
44+
<Setter Property="Foreground"
45+
Value="{StaticResource WhiteBrush}" />
46+
</Style>
47+
</ResourceDictionary>
48+
</Application.Resources>
49+
</Application>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespace EmbeddedWindows
2+
{
3+
/// <summary>
4+
/// Provides application-specific behavior to supplement the default Application class.
5+
/// </summary>
6+
public partial class App : Application
7+
{
8+
/// <summary>
9+
/// Initializes the singleton application object. This is the first line of authored code
10+
/// executed, and as such is the logical equivalent of main() or WinMain().
11+
/// </summary>
12+
public App()
13+
{
14+
this.InitializeComponent();
15+
}
16+
17+
/// <summary>
18+
/// Invoked when the application is launched normally by the end user. Other entry points
19+
/// will be used such as when the application is launched to open a specific file.
20+
/// </summary>
21+
/// <param name="args">Details about the launch request and process.</param>
22+
protected override void OnLaunched(LaunchActivatedEventArgs args)
23+
{
24+
m_window = new MainWindow();
25+
m_window?.Activate();
26+
}
27+
28+
private Window? m_window;
29+
}
30+
}
Loading
Loading
Loading
Loading
Loading
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project Sdk="Microsoft.NET.Sdk">
3+
<PropertyGroup>
4+
<TargetFramework>net9.0-windows10.0.19041.0</TargetFramework>
5+
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
6+
<OutputType>WinExe</OutputType>
7+
8+
<!-- WinUI 3 -->
9+
<UseWinUI>true</UseWinUI>
10+
<EnableMsixTooling>true</EnableMsixTooling>
11+
<WindowsPackageType>None</WindowsPackageType>
12+
13+
<!-- Project Options -->
14+
<Nullable>enable</Nullable>
15+
<!--<ImplicitUsings>enable</ImplicitUsings>-->
16+
<RootNamespace>EmbeddedWindows</RootNamespace>
17+
<EnableDefaultXamlItems>false</EnableDefaultXamlItems>
18+
19+
<!-- App Options -->
20+
<UseRidGraph>true</UseRidGraph>
21+
<Platforms>x86;x64;arm64</Platforms>
22+
<ApplicationManifest>app.manifest</ApplicationManifest>
23+
<PublishProfile>win10-$(Platform).pubxml</PublishProfile>
24+
<RuntimeIdentifiers>win10-x86;win10-x64;win10-arm64</RuntimeIdentifiers>
25+
</PropertyGroup>
26+
27+
<ItemGroup>
28+
<Content Include="Assets\SplashScreen.scale-200.png" />
29+
<Content Include="Assets\LockScreenLogo.scale-200.png" />
30+
<Content Include="Assets\Square150x150Logo.scale-200.png" />
31+
<Content Include="Assets\Square44x44Logo.scale-200.png" />
32+
<Content Include="Assets\Square44x44Logo.targetsize-24_altform-unplated.png" />
33+
<Content Include="Assets\StoreLogo.png" />
34+
<Content Include="Assets\Wide310x150Logo.scale-200.png" />
35+
<!-- Custom Fonts -->
36+
<MauiFont Include="Resources\Fonts\*" />
37+
</ItemGroup>
38+
39+
<ItemGroup>
40+
<PackageReference Include="Microsoft.Maui.Controls" Version="9.0.0-preview.1.9973" />
41+
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="9.0.0-preview.1.9973" />
42+
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.*" />
43+
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.*" />
44+
<Manifest Include="$(ApplicationManifest)" />
45+
</ItemGroup>
46+
47+
<ItemGroup>
48+
<ProjectReference Include="..\MauiLib\MauiLib.csproj" />
49+
</ItemGroup>
50+
51+
<!--
52+
Defining the "Msix" ProjectCapability here allows the Single-project MSIX Packaging
53+
Tools extension to be activated for this project even if the Windows App SDK Nuget
54+
package has not yet been restored.
55+
-->
56+
<ItemGroup Condition="'$(DisableMsixProjectCapabilityAddedByProject)'!='true' and '$(EnableMsixTooling)'=='true'">
57+
<ProjectCapability Include="Msix" />
58+
</ItemGroup>
59+
60+
<!--
61+
Defining the "HasPackageAndPublishMenuAddedByProject" property here allows the Solution
62+
Explorer "Package and Publish" context menu entry to be enabled for this project even if
63+
the Windows App SDK Nuget package has not yet been restored.
64+
-->
65+
<PropertyGroup Condition="'$(DisableHasPackageAndPublishMenuAddedByProject)'!='true' and '$(EnableMsixTooling)'=='true'">
66+
<HasPackageAndPublishMenu>true</HasPackageAndPublishMenu>
67+
</PropertyGroup>
68+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
global using Microsoft.UI.Xaml;
2+
3+
global using MauiLib;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<Package
4+
xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
5+
xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
6+
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
7+
IgnorableNamespaces="uap rescap">
8+
9+
<Identity
10+
Name="559b8d5a-85a6-462d-8ec8-f49df3a3d69a"
11+
Publisher="CN=User Name"
12+
Version="1.0.0.0" />
13+
14+
<Properties>
15+
<DisplayName>EmbeddedWindows</DisplayName>
16+
<PublisherDisplayName>User Name</PublisherDisplayName>
17+
<Logo>Assets\StoreLogo.png</Logo>
18+
</Properties>
19+
20+
<Dependencies>
21+
<TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.17763.0" MaxVersionTested="10.0.19041.0" />
22+
<TargetDeviceFamily Name="Windows.Desktop" MinVersion="10.0.17763.0" MaxVersionTested="10.0.19041.0" />
23+
</Dependencies>
24+
25+
<Resources>
26+
<Resource Language="x-generate"/>
27+
</Resources>
28+
29+
<Applications>
30+
<Application Id="App"
31+
Executable="$targetnametoken$.exe"
32+
EntryPoint="$targetentrypoint$">
33+
<uap:VisualElements
34+
DisplayName="EmbeddedWindows"
35+
Description="EmbeddedWindows"
36+
BackgroundColor="transparent"
37+
Square150x150Logo="Assets\Square150x150Logo.png"
38+
Square44x44Logo="Assets\Square44x44Logo.png">
39+
<uap:DefaultTile Wide310x150Logo="Assets\Wide310x150Logo.png" />
40+
<uap:SplashScreen Image="Assets\SplashScreen.png" />
41+
</uap:VisualElements>
42+
</Application>
43+
</Applications>
44+
45+
<Capabilities>
46+
<rescap:Capability Name="runFullTrust" />
47+
</Capabilities>
48+
</Package>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
https://go.microsoft.com/fwlink/?LinkID=208121.
4+
-->
5+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
6+
<PropertyGroup>
7+
<PublishProtocol>FileSystem</PublishProtocol>
8+
<Platform>arm64</Platform>
9+
<RuntimeIdentifier>win10-arm64</RuntimeIdentifier>
10+
<PublishDir>bin\$(Configuration)\$(TargetFramework)\$(RuntimeIdentifier)\publish\</PublishDir>
11+
<SelfContained>true</SelfContained>
12+
<PublishSingleFile>False</PublishSingleFile>
13+
<PublishReadyToRun Condition="'$(Configuration)' == 'Debug'">False</PublishReadyToRun>
14+
<PublishReadyToRun Condition="'$(Configuration)' != 'Debug'">True</PublishReadyToRun>
15+
<!--
16+
See https://github.com/microsoft/CsWinRT/issues/373
17+
<PublishTrimmed>True</PublishTrimmed>
18+
-->
19+
</PropertyGroup>
20+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
https://go.microsoft.com/fwlink/?LinkID=208121.
4+
-->
5+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
6+
<PropertyGroup>
7+
<PublishProtocol>FileSystem</PublishProtocol>
8+
<Platform>x64</Platform>
9+
<RuntimeIdentifier>win10-x64</RuntimeIdentifier>
10+
<PublishDir>bin\$(Configuration)\$(TargetFramework)\$(RuntimeIdentifier)\publish\</PublishDir>
11+
<SelfContained>true</SelfContained>
12+
<PublishSingleFile>False</PublishSingleFile>
13+
<PublishReadyToRun Condition="'$(Configuration)' == 'Debug'">False</PublishReadyToRun>
14+
<PublishReadyToRun Condition="'$(Configuration)' != 'Debug'">True</PublishReadyToRun>
15+
<!--
16+
See https://github.com/microsoft/CsWinRT/issues/373
17+
<PublishTrimmed>True</PublishTrimmed>
18+
-->
19+
</PropertyGroup>
20+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
https://go.microsoft.com/fwlink/?LinkID=208121.
4+
-->
5+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
6+
<PropertyGroup>
7+
<PublishProtocol>FileSystem</PublishProtocol>
8+
<Platform>x86</Platform>
9+
<RuntimeIdentifier>win10-x86</RuntimeIdentifier>
10+
<PublishDir>bin\$(Configuration)\$(TargetFramework)\$(RuntimeIdentifier)\publish\</PublishDir>
11+
<SelfContained>true</SelfContained>
12+
<PublishSingleFile>False</PublishSingleFile>
13+
<PublishReadyToRun Condition="'$(Configuration)' == 'Debug'">False</PublishReadyToRun>
14+
<PublishReadyToRun Condition="'$(Configuration)' != 'Debug'">True</PublishReadyToRun>
15+
<!--
16+
See https://github.com/microsoft/CsWinRT/issues/373
17+
<PublishTrimmed>True</PublishTrimmed>
18+
-->
19+
</PropertyGroup>
20+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"profiles": {
3+
"EmbeddedWindows (Package)": {
4+
"commandName": "MsixPackage"
5+
},
6+
"EmbeddedWindows (Unpackaged)": {
7+
"commandName": "Project"
8+
}
9+
}
10+
}
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<Window
3+
x:Class="EmbeddedWindows.MainWindow"
4+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
5+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
6+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
7+
xmlns:local="using:EmbeddedWindows"
8+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
9+
mc:Ignorable="d">
10+
</Window>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using Microsoft.Maui.Hosting;
2+
using Microsoft.Maui.Platform;
3+
using Microsoft.Maui;
4+
using Microsoft.Maui.Embedding;
5+
using Microsoft.Extensions.DependencyInjection;
6+
7+
namespace EmbeddedWindows
8+
{
9+
/// <summary>
10+
/// An empty window that can be used on its own or navigated to within a Frame.
11+
/// </summary>
12+
public sealed partial class MainWindow : Window
13+
{
14+
public MainWindow()
15+
{
16+
this.InitializeComponent();
17+
18+
var mauiApp = MauiApp.CreateBuilder()
19+
.UseMauiEmbedding<MyApp>()
20+
.ConfigureFonts(fonts =>
21+
{
22+
fonts.AddFont("OpenSans-Regular.ttf", "OS400");
23+
fonts.AddFont("OpenSans-SemiBold.ttf", "OS600");
24+
})
25+
.Build();
26+
// While doing .NET MAUI Embedding, this call is required so that the Application object instance gets resolved.
27+
var _ = mauiApp.Services.GetRequiredService<IApplication>();
28+
Content = new MauiPage().ToPlatform(new MauiContext(mauiApp.Services));
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)