CommunityToolkit / Microsoft.Toolkit.Win32

ARCHIVE - This repository contained XAML Islands wrapper controls and tooling for XAML Islands with WinUI 2, see readme for more info about XAML Islands with WinUI 3 and the WindowsAppSDK.

Home Page:https://aka.ms/windowsappsdk

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Xaml Islands launches fine from the WPF project, but fails to launch as a WAP project.

TamBuiWork opened this issue · comments

Describe the bug

The issue is I have a simple minimal example project that showcases a WPF app utilizing a UWP control, using XAML islands. It works. This is what it looks like when I run it:

image

And it still works when I run directly from the WPF project (F5 - Start Debug). However, if I try to package this WPF project now inside a WAP project, and run it from the WAP project, it shows this error:

Cannot create control of type XamlIslandsTest.UwpPart.MyUserControl1

image

The strange thing is that this used to work fine just a few months ago with the same code. The only things that could have changed in the past few months was my Visual Studio version, or my OS version may have upgraded to 20H2 (19042).

Steps to Reproduce

Steps to reproduce the behavior:

  1. Download my sample solution (contains a WPF proj, UWP proj, and WAP proj).
  2. Open the sln in Visual Studio 2019 (I'm using 16.11.6)
  3. Set 'XamlIslandsTest.DesktopApp' as the startup project. This is a WPF project that uses XAML islands.
  4. Set your platform/configuration as Debug/x64
  5. Launch the app (F5) and you will see that the XAML Islands WPF app runs just fine.
  6. Close the app
  7. Set 'XamlIslandsTest.WAP' as the startup project. This is an app package project (WAP) for the WPF project.
  8. Launch the app (F5) and you will see the exception.

Expected behavior

I expect the app packaged project to run the XAML Islands app, just as the XAML Islands app is able to run as a standalone WPF app.

Environment

NuGet Package(s): 
- WPF Project (XamlIslandsTest.DesktopApp): 
  - Microsoft.Toolkit.Wpf.UI.XamlHost (version: 6.1.2)
  - Microsoft.VCRTForwarders.140 (version: 1.0.6)
- UWP Project (XamlIslandsTest.UwpPart)
  - Microsoft.NETCore.UniversalWindowsPlatform (version: 6.2.12)
  - Microsoft.Toolkit.Win32.UI.XamlApplication (version: 6.1.3)
- WAP Project (MyAppPackage)
  - Microsoft.Windows.SDK.Buildtools (version: 10.0.19041.8)

Project .NET Version:
- [ ] .NET Framework (version: )
- [ ] .NET Core 3
- [ ] .NET Core 3.1 Preview (version: )
- [ x ] .NET Core 3.1

Windows 10 Build Number:
- [ ] Fall Creators Update (16299)
- [ ] April 2018 Update (17134)
- [ ] October 2018 Update (17763)
- [ ] May 2019 Update (18362)
- [ ] Insider Build (build number: )
- [ x ] Version 20H2 (19042)

App min and target version:
- [ ] Fall Creators Update (16299)
- [ ] April 2018 Update (17134)
- [ ] October 2018 Update (17763)
- [ x ] May 2019 Update (18362)
- [ ] Insider Build (xxxxx)

Device form factor:
- [ ] Desktop
- [ ] Xbox
- [ ] Surface Hub
- [ ] IoT
- [ x ] Laptop (Lenovo Thinkpad T490

Visual Studio 
- [ ] 2017 (version: )
- [ x ] 2019 (version: Professional, Version 16.11.6) 
- [ ] 2019 Preview (version: )

Hello TamBuiWork, thank you for opening an issue with us!

I have automatically added a "needs triage" label to help get things started. Our team will analyze and investigate the issue, and escalate it to the relevant team if possible. Other community members may also look into the issue and provide feedback 🙌

It works well if you add reference to XamlIslandsTest.UwpPart in WAP project.

image

Tested on Windows 11 22000.434, VS 2019 16.9.8

@michalleptuch Thank you for your help validating. However, when I try to add a reference to XamlIslandsTest.UwpPart as you described, the WAP project fails to build for me with this error:

AppxManifest.xml : error APPX0501: Validation error. error 80080204: App manifest validation error: Line 48, Column 27, Reason: The field "[local-name()='Extensions']/[local-name()='Extension']/[local-name()='InProcessServer']/[local-name()='ActivatableClass']" with value "Microsoft.Toolkit.Win32.UI.XamlHost.XamlApplication" must only be declared once. A duplicate exists on Line 42, Column 27.

Navigating to the error in AppManifest.xml shows the following snippet where the duplicate entry can be seen:

 <Extensions>
   <Extension Category="windows.activatableClass.inProcessServer">
     <InProcessServer>
       <Path>Microsoft.Toolkit.Win32.UI.XamlHost.dll</Path>
       <ActivatableClass ActivatableClassId="Microsoft.Toolkit.Win32.UI.XamlHost.XamlApplication" ThreadingModel="both" />
     </InProcessServer>
   </Extension>
   <Extension Category="windows.activatableClass.inProcessServer">
     <InProcessServer>
       <Path>XamlIslandsTest.DesktopApp\Microsoft.Toolkit.Win32.UI.XamlHost.dll</Path>
       <ActivatableClass ActivatableClassId="Microsoft.Toolkit.Win32.UI.XamlHost.XamlApplication" ThreadingModel="both" />
     </InProcessServer>
   </Extension>
 </Extensions>

When I try to manually remove either duplicate entry, it always comes back after I build.

Did you encounter any similar error? And if so, how did you overcome this error?

It's known issue: #328
You need to use older version of Visual Studio 2019

@michalleptuch Thank you! I wasn't aware of that, this confirms my initial suspicion then. I appreciate your prompt response.

This issue has been marked as "needs attention 👋" due to no activity for 15 days. Please triage the issue so the fix can be established.

This issue has been marked as "needs attention 👋" due to no activity for 15 days. Please triage the issue so the fix can be established.

This issue has been marked as "needs attention 👋" due to no activity for 15 days. Please triage the issue so the fix can be established.

Using the information from the bug ticket that @michalleptuch provided, I did the following and got it to work:

  1. Append the following at the end of the .wapproj file, right before the close of the </project>:
  <Target Name="_RemoveWinMDFromAppxManifest" BeforeTargets="_GenerateCurrentProjectAppxManifest">
    <ItemGroup>
      <_AppxWinmdFilesToHarvest Remove="@(_AppxWinmdFilesToHarvest)" />
    </ItemGroup>
  </Target>
  <Target Name="_StompSourceProjectForWapProject" BeforeTargets="_ConvertItems">
    <ItemGroup>
      <_TemporaryFilteredWapProjOutput Include="@(_FilteredNonWapProjProjectOutput)" />
      <_FilteredNonWapProjProjectOutput Remove="@(_TemporaryFilteredWapProjOutput)" />
      <_FilteredNonWapProjProjectOutput Include="@(_TemporaryFilteredWapProjOutput)">
        <SourceProject>
        </SourceProject>
      </_FilteredNonWapProjProjectOutput>
      <_DetailedPriXml Remove="@(_DetailedPriXml)" />
    </ItemGroup>
  </Target>
  1. Make sure the WAP project includes a project reference to the UWP project.

Once I did the above two, I got the app to launch from the WAP project.

That's great but did you try to create Release package for MS Store?

@michalleptuch I did not try that, because for my purposes, my app does not need to be released for MS Store.