Skip to content

Commit

Permalink
Merge pull request #78 from egvijayanand/working
Browse files Browse the repository at this point in the history
Native Embedding WinUI 3 Sample (.NET 8)
  • Loading branch information
egvijayanand committed Feb 29, 2024
2 parents ee25cfd + 021f989 commit b4ddfc2
Show file tree
Hide file tree
Showing 31 changed files with 493 additions and 1 deletion.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ Completed samples will be uploaded into this repository so that it is accessible

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

_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._
_Note: Samples are in the process of migrating to the latest framework version, so there would be a change in the directory structure. Kindly bear with this._

Made available in the `src\NET_8\` directory:
* `EmbeddedWindows` - .NET MAUI Page embedded in a Native WinUI 3 App, targeting .NET 8 (`net8.0-windows10.0.19041.0`)
- Refer to this [article](https://egvijayanand.in/2024/02/29/dotnet-maui-native-embedding/) for working with this sample

Made available in the `src\NET_9\` directory:
* `EmbeddedWindows` - .NET MAUI Page embedded in a Native WinUI 3 App, targeting .NET 9 (`net9.0-windows10.0.19041.0`)
Expand Down
5 changes: 5 additions & 0 deletions src/NET_8/EmbeddedWindows/Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Project>
<PropertyGroup>
<MauiVersion>8.0.7</MauiVersion>
</PropertyGroup>
</Project>
51 changes: 51 additions & 0 deletions src/NET_8/EmbeddedWindows/EmbeddedWindows.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.33103.201
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EmbeddedWindows", "EmbeddedWindows\EmbeddedWindows.csproj", "{A96B3752-FF0F-402B-A85A-634E934202F1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MauiLib", "MauiLib\MauiLib.csproj", "{E93EB175-0FD3-4BFD-B81A-CDAF33998932}"
EndProject
Global
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {33E05B3D-B1D3-476F-9E8D-E1E471B55750}
EndGlobalSection
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{A96B3752-FF0F-402B-A85A-634E934202F1}.Debug|Any CPU.ActiveCfg = Debug|x86
{A96B3752-FF0F-402B-A85A-634E934202F1}.Debug|Any CPU.Build.0 = Debug|x86
{A96B3752-FF0F-402B-A85A-634E934202F1}.Debug|x64.ActiveCfg = Debug|x64
{A96B3752-FF0F-402B-A85A-634E934202F1}.Debug|x64.Build.0 = Debug|x64
{A96B3752-FF0F-402B-A85A-634E934202F1}.Debug|x86.ActiveCfg = Debug|x86
{A96B3752-FF0F-402B-A85A-634E934202F1}.Debug|x86.Build.0 = Debug|x86
{A96B3752-FF0F-402B-A85A-634E934202F1}.Release|Any CPU.ActiveCfg = Release|x86
{A96B3752-FF0F-402B-A85A-634E934202F1}.Release|Any CPU.Build.0 = Release|x86
{A96B3752-FF0F-402B-A85A-634E934202F1}.Release|x64.ActiveCfg = Release|x64
{A96B3752-FF0F-402B-A85A-634E934202F1}.Release|x64.Build.0 = Release|x64
{A96B3752-FF0F-402B-A85A-634E934202F1}.Release|x86.ActiveCfg = Release|x86
{A96B3752-FF0F-402B-A85A-634E934202F1}.Release|x86.Build.0 = Release|x86
{E93EB175-0FD3-4BFD-B81A-CDAF33998932}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E93EB175-0FD3-4BFD-B81A-CDAF33998932}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E93EB175-0FD3-4BFD-B81A-CDAF33998932}.Debug|x64.ActiveCfg = Debug|Any CPU
{E93EB175-0FD3-4BFD-B81A-CDAF33998932}.Debug|x64.Build.0 = Debug|Any CPU
{E93EB175-0FD3-4BFD-B81A-CDAF33998932}.Debug|x86.ActiveCfg = Debug|Any CPU
{E93EB175-0FD3-4BFD-B81A-CDAF33998932}.Debug|x86.Build.0 = Debug|Any CPU
{E93EB175-0FD3-4BFD-B81A-CDAF33998932}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E93EB175-0FD3-4BFD-B81A-CDAF33998932}.Release|Any CPU.Build.0 = Release|Any CPU
{E93EB175-0FD3-4BFD-B81A-CDAF33998932}.Release|x64.ActiveCfg = Release|Any CPU
{E93EB175-0FD3-4BFD-B81A-CDAF33998932}.Release|x64.Build.0 = Release|Any CPU
{E93EB175-0FD3-4BFD-B81A-CDAF33998932}.Release|x86.ActiveCfg = Release|Any CPU
{E93EB175-0FD3-4BFD-B81A-CDAF33998932}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
49 changes: 49 additions & 0 deletions src/NET_8/EmbeddedWindows/EmbeddedWindows/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Application
x:Class="EmbeddedWindows.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:EmbeddedWindows"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
<!-- Other merged dictionaries here -->
</ResourceDictionary.MergedDictionaries>
<!-- Other app resources here -->
<Color x:Key="Primary">#512BD4</Color>

<SolidColorBrush x:Key="PrimaryBrush" Color="{StaticResource Primary}" />
<SolidColorBrush x:Key="WhiteBrush" Color="White" />
<SolidColorBrush x:Key="BlackBrush" Color="Black" />

<x:Double x:Key="AppFontSize">14</x:Double>

<Style x:Key="MyLabel" TargetType="TextBlock">
<Setter Property="Foreground"
Value="{StaticResource PrimaryBrush}" />
</Style>

<Style x:Key="Action" TargetType="Button">
<Setter Property="FontSize"
Value="{StaticResource AppFontSize}" />
<Setter Property="Padding"
Value="14,10" />
</Style>

<Style x:Key="PrimaryAction"
TargetType="Button"
BasedOn="{StaticResource Action}">
<Setter Property="Background"
Value="{StaticResource PrimaryBrush}" />
<Setter Property="CornerRadius"
Value="8" />
<Setter Property="Foreground"
Value="{StaticResource WhiteBrush}" />
</Style>
</ResourceDictionary>
</Application.Resources>
</Application>
30 changes: 30 additions & 0 deletions src/NET_8/EmbeddedWindows/EmbeddedWindows/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace EmbeddedWindows
{
/// <summary>
/// Provides application-specific behavior to supplement the default Application class.
/// </summary>
public partial class App : Application
{
/// <summary>
/// Initializes the singleton application object. This is the first line of authored code
/// executed, and as such is the logical equivalent of main() or WinMain().
/// </summary>
public App()
{
this.InitializeComponent();
}

/// <summary>
/// Invoked when the application is launched normally by the end user. Other entry points
/// will be used such as when the application is launched to open a specific file.
/// </summary>
/// <param name="args">Details about the launch request and process.</param>
protected override void OnLaunched(LaunchActivatedEventArgs args)
{
m_window = new MainWindow();
m_window?.Activate();
}

private Window? m_window;
}
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
68 changes: 68 additions & 0 deletions src/NET_8/EmbeddedWindows/EmbeddedWindows/EmbeddedWindows.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0-windows10.0.19041.0</TargetFramework>
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
<OutputType>WinExe</OutputType>

<!-- WinUI 3 -->
<UseWinUI>true</UseWinUI>
<EnableMsixTooling>true</EnableMsixTooling>
<WindowsPackageType>None</WindowsPackageType>

<!-- Project Options -->
<Nullable>enable</Nullable>
<!--<ImplicitUsings>enable</ImplicitUsings>-->
<RootNamespace>EmbeddedWindows</RootNamespace>
<EnableDefaultXamlItems>false</EnableDefaultXamlItems>

<!-- App Options -->
<UseRidGraph>true</UseRidGraph>
<Platforms>x86;x64;arm64</Platforms>
<ApplicationManifest>app.manifest</ApplicationManifest>
<PublishProfile>win10-$(Platform).pubxml</PublishProfile>
<RuntimeIdentifiers>win10-x86;win10-x64;win10-arm64</RuntimeIdentifiers>
</PropertyGroup>

<ItemGroup>
<Content Include="Assets\SplashScreen.scale-200.png" />
<Content Include="Assets\LockScreenLogo.scale-200.png" />
<Content Include="Assets\Square150x150Logo.scale-200.png" />
<Content Include="Assets\Square44x44Logo.scale-200.png" />
<Content Include="Assets\Square44x44Logo.targetsize-24_altform-unplated.png" />
<Content Include="Assets\StoreLogo.png" />
<Content Include="Assets\Wide310x150Logo.scale-200.png" />
<!-- Custom Fonts -->
<MauiFont Include="Resources\Fonts\*" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" />
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="$(MauiVersion)" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.*" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.*" />
<Manifest Include="$(ApplicationManifest)" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\MauiLib\MauiLib.csproj" />
</ItemGroup>

<!--
Defining the "Msix" ProjectCapability here allows the Single-project MSIX Packaging
Tools extension to be activated for this project even if the Windows App SDK Nuget
package has not yet been restored.
-->
<ItemGroup Condition="'$(DisableMsixProjectCapabilityAddedByProject)'!='true' and '$(EnableMsixTooling)'=='true'">
<ProjectCapability Include="Msix" />
</ItemGroup>

<!--
Defining the "HasPackageAndPublishMenuAddedByProject" property here allows the Solution
Explorer "Package and Publish" context menu entry to be enabled for this project even if
the Windows App SDK Nuget package has not yet been restored.
-->
<PropertyGroup Condition="'$(DisableHasPackageAndPublishMenuAddedByProject)'!='true' and '$(EnableMsixTooling)'=='true'">
<HasPackageAndPublishMenu>true</HasPackageAndPublishMenu>
</PropertyGroup>
</Project>
3 changes: 3 additions & 0 deletions src/NET_8/EmbeddedWindows/EmbeddedWindows/Imports.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
global using Microsoft.UI.Xaml;

global using MauiLib;
48 changes: 48 additions & 0 deletions src/NET_8/EmbeddedWindows/EmbeddedWindows/Package.appxmanifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>

<Package
xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
IgnorableNamespaces="uap rescap">

<Identity
Name="559b8d5a-85a6-462d-8ec8-f49df3a3d69a"
Publisher="CN=User Name"
Version="1.0.0.0" />

<Properties>
<DisplayName>EmbeddedWindows</DisplayName>
<PublisherDisplayName>User Name</PublisherDisplayName>
<Logo>Assets\StoreLogo.png</Logo>
</Properties>

<Dependencies>
<TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.17763.0" MaxVersionTested="10.0.19041.0" />
<TargetDeviceFamily Name="Windows.Desktop" MinVersion="10.0.17763.0" MaxVersionTested="10.0.19041.0" />
</Dependencies>

<Resources>
<Resource Language="x-generate"/>
</Resources>

<Applications>
<Application Id="App"
Executable="$targetnametoken$.exe"
EntryPoint="$targetentrypoint$">
<uap:VisualElements
DisplayName="EmbeddedWindows"
Description="EmbeddedWindows"
BackgroundColor="transparent"
Square150x150Logo="Assets\Square150x150Logo.png"
Square44x44Logo="Assets\Square44x44Logo.png">
<uap:DefaultTile Wide310x150Logo="Assets\Wide310x150Logo.png" />
<uap:SplashScreen Image="Assets\SplashScreen.png" />
</uap:VisualElements>
</Application>
</Applications>

<Capabilities>
<rescap:Capability Name="runFullTrust" />
</Capabilities>
</Package>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<PublishProtocol>FileSystem</PublishProtocol>
<Platform>arm64</Platform>
<RuntimeIdentifier>win10-arm64</RuntimeIdentifier>
<PublishDir>bin\$(Configuration)\$(TargetFramework)\$(RuntimeIdentifier)\publish\</PublishDir>
<SelfContained>true</SelfContained>
<PublishSingleFile>False</PublishSingleFile>
<PublishReadyToRun Condition="'$(Configuration)' == 'Debug'">False</PublishReadyToRun>
<PublishReadyToRun Condition="'$(Configuration)' != 'Debug'">True</PublishReadyToRun>
<!--
See https://github.com/microsoft/CsWinRT/issues/373
<PublishTrimmed>True</PublishTrimmed>
-->
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<PublishProtocol>FileSystem</PublishProtocol>
<Platform>x64</Platform>
<RuntimeIdentifier>win10-x64</RuntimeIdentifier>
<PublishDir>bin\$(Configuration)\$(TargetFramework)\$(RuntimeIdentifier)\publish\</PublishDir>
<SelfContained>true</SelfContained>
<PublishSingleFile>False</PublishSingleFile>
<PublishReadyToRun Condition="'$(Configuration)' == 'Debug'">False</PublishReadyToRun>
<PublishReadyToRun Condition="'$(Configuration)' != 'Debug'">True</PublishReadyToRun>
<!--
See https://github.com/microsoft/CsWinRT/issues/373
<PublishTrimmed>True</PublishTrimmed>
-->
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<PublishProtocol>FileSystem</PublishProtocol>
<Platform>x86</Platform>
<RuntimeIdentifier>win10-x86</RuntimeIdentifier>
<PublishDir>bin\$(Configuration)\$(TargetFramework)\$(RuntimeIdentifier)\publish\</PublishDir>
<SelfContained>true</SelfContained>
<PublishSingleFile>False</PublishSingleFile>
<PublishReadyToRun Condition="'$(Configuration)' == 'Debug'">False</PublishReadyToRun>
<PublishReadyToRun Condition="'$(Configuration)' != 'Debug'">True</PublishReadyToRun>
<!--
See https://github.com/microsoft/CsWinRT/issues/373
<PublishTrimmed>True</PublishTrimmed>
-->
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"profiles": {
"EmbeddedWindows (Package)": {
"commandName": "MsixPackage"
},
"EmbeddedWindows (Unpackaged)": {
"commandName": "Project"
}
}
}
Binary file not shown.
Binary file not shown.
10 changes: 10 additions & 0 deletions src/NET_8/EmbeddedWindows/EmbeddedWindows/Views/MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Window
x:Class="EmbeddedWindows.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:EmbeddedWindows"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
</Window>
31 changes: 31 additions & 0 deletions src/NET_8/EmbeddedWindows/EmbeddedWindows/Views/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Microsoft.Maui.Hosting;
using Microsoft.Maui.Platform;
using Microsoft.Maui;
using Microsoft.Maui.Embedding;
using Microsoft.Extensions.DependencyInjection;

namespace EmbeddedWindows
{
/// <summary>
/// An empty window that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();

var mauiApp = MauiApp.CreateBuilder()
.UseMauiEmbedding<MyApp>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OS400");
fonts.AddFont("OpenSans-SemiBold.ttf", "OS600");
})
.Build();
// While doing .NET MAUI Embedding, this call is required so that the Application object instance gets resolved.
var _ = mauiApp.Services.GetRequiredService<IApplication>();
Content = new MauiPage().ToPlatform(new MauiContext(mauiApp.Services));
}
}
}

0 comments on commit b4ddfc2

Please sign in to comment.