Search code examples
visual-studioxamlc++17winui-3

Runtime Exception: Cannot locate xaml Resource file


working on c++ WinUI3 application

'Cannot locate resource from 'ms-appx:///Sample.xaml'.'. Actually, by default xaml files are placed under TestWRC folder during build process under CoreApp x64 directory but when xaml cpp code is generated, file is referring root location ('ms-appx:///Sample.xaml'.') instead of ('ms-appx:///SampleWRC/Sample.xaml'.').

<?xml version="1.0" encoding="utf-8"?>
<Page
    x:Class="TestWRC.Sample"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:TestWRC"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
        <Button x:Name="myButton" Click="myButton_Click">Click Me</Button>
    </StackPanel>
</Page>

Steps to reproduce the bug

  1. create Balnk App, Packaged (WinUI 3 in Desktop) C++ with same name (CoreApp) for solution and project name. Uncheck the option to put the Project in the Solution folder.
  2. create Windows Runtime component (WinUI 3) C++ project with TestWRC as name, then add Sample.xaml file. enter image description here
  3. make navigation from CoreApp to WRC project.
  4. Build application and Run
  5. click Button which throws 'Cannot locate resource from 'ms-appx:///Sample.xaml'. ERROR

I felt it has to do with configuration/property settings of xaml file but nothing found on project settings. Am I missing anything small here?

source code sample - https://github.com/GMudide/Bugs/tree/main/WinUI%203/CPP


Solution

  • After looking into multiple Microsoft pages found it as a known issue.

    To resolve, add the below target to the end of the Windows Runtime Component's .vcxproj:

    <Target Name="GetPriIndexName">
    <PropertyGroup>
        <!-- Winmd library targets use the default root namespace of the project for the App package name -->
        <PriIndexName Condition="'$(RootNamespace)' != ''">$(RootNamespace)</PriIndexName>
        <!-- If RootNamespace is empty fall back to TargetName -->
        <PriIndexName Condition="$(PriIndexName) == ''">$(TargetName)</PriIndexName>
    </PropertyGroup>
    </Target>
    

    refer: https://learn.microsoft.com/en-us/windows/apps/windows-app-sdk/stable-channel#:~:text=Known%20issues%20for%20both%20packaged%20and%20unpackaged%20WinUI%20applications%3A