How to make Projection UserControl (XAML resource) in C++/WinRT (as WRC) to C#?

Volker Veidt 36 Reputation points
2022-09-03T04:35:18.93+00:00

Hello!
I'm trying to add a SwapChainPanel-based XAML UserControl WRC the code on this docs but have reference error saying;

"Error WMC0001 Unknown type 'BoxRenderer' in XML namespace 'using:SimpleMathProjection' StubWinUI3Desktop my-local-path\NetProjection\StubWinUI3Desktop\DxPage.xaml 12 "

The project is Here.

To make the code attached, I referred to this document and other pages.
The first, I've asked where the base code made but introduced here, the Microsoft Q&A.

The UserControl just contains WinUI3 SwapChainPanel named BoxRenderer. Though I intend to use C++ DirectX on there but currently no functions exist.

In short, please look in the code on the site;

build CppWinRTComponentProjectionSample.sln --> this makes NuGet package
build StubWinUI3Desktop.sln --> this makes the error above.
Detailed repro step is in Readme.md. It contains original API consuming project (but it is console app).

Anyway, thanks for reading.

Windows development | Windows App SDK
{count} votes

Accepted answer
  1. Junjie Zhu - MSFT 21,646 Reputation points
    2022-12-29T14:46:24.52+00:00

    Hi @Volker Veidt ,

    The issue is that WinUI can't find the default style defined in generic.xaml as the compiled Generic.xbf that contains it is not copied over to the app. Visual Studio takes care of this for a C++ app, but not for a C# project through a NuGet package. So, the workaround is to do that through the NuGet package yourself.

    You can follow the steps below.

    1.Download the project you shared in GitHub https://github.com/vmx17/NetProjection
    2.Update SimpleMathProjection.nuspec with:

    ...  
          <file src="..\..\_build\x64\Release\SimpleMathComponent\obj\embed\SimpleMathComponent\Themes\Generic.xbf" target="content\Generic.xbf"/>  
          <file src="SimpleMathComponent.targets" target="build\SimpleMathComponent.targets"></file>  
    </files>  
    

    274881-image.png

    3.Add SimpleMathComponent.targets with:

    <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">   
          <ItemGroup>  
                <Content Include="$(MSBuildThisFileDirectory)\..\content\Generic.xbf">  
                      <CopyToOutputDirectory>Always</CopyToOutputDirectory>  
                      <TargetPath>SimpleMathComponent\Themes\Generic.xbf</TargetPath>  
                </Content>  
          </ItemGroup>  
    </Project>  
    

    274788-image.png
    4.Change "Theme" to "Themes"

    274789-image.png


4 additional answers

Sort by: Most helpful
  1. Volker Veidt 36 Reputation points
    2023-01-05T07:45:07.307+00:00

    Thank you so much! (Oh, same beginning as Mr. or Mrs. kPark-8696.) I've just updated the code and readme on GitHub.276393-afterpushingbuttons.jpg I'll accept the answer, but I'm very confident! to hit another/many issues soon and reuse the same repository. Now I begin checking whether I can draw something in the SwapChainPanel in C++.
    These months, I was wandering this or that projects. I hope I can use C++ and C# interop freely and easily.
    Do I have to dive into the depth of compilers?

    1 person found this answer helpful.

  2. Junjie Zhu - MSFT 21,646 Reputation points
    2022-09-06T10:09:48.977+00:00

    Hello @Volker Veidt ,

    I tried to modify the code in DxPage.xmal , I put the SimpleMathComponent.dll under the directory **...\NetProjection\StubWinUI3Desktop**.
    The program load the Component and run successfully, can this workaround meet your needs?

    <Page  
        x:Class="StubWinUI3Desktop.DxPage"  
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
        xmlns:local="using:StubWinUI3Desktop"  
        xmlns:prj="using:SimpleMathComponent"  
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
        mc:Ignorable="d"  
        Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
      
        <prj:BoxRenderer />  
          
    </Page>  
    

    238110-image.png

    Thank you.
    Junjie


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  3. Volker Veidt 36 Reputation points
    2022-09-08T17:04:24.19+00:00

    I'm sorry to say, the fix by JunjieZhu-MSFT-san's answer made no errors but the SwapChainPanel in WRC did not appeared.
    No compile error but no work. So I cannot help re-opening this thread.

    I returned the repository almost same state but mod some miner change.

    And also I've realized, a GUI component included in *.nupkg is hard to debug. Aren't there easy way to integrate C++/WinRT XAML Control (WRC) into C# WinUI3 desktop project? (Is it possible? All Microsoft documents seems to avoid this point.)

    I'll continue to shape up the code.
    Currently the code is very fragile. Just rename solution name, got some runtime errors.


  4. Volker Veidt 36 Reputation points
    2022-09-23T06:16:12.517+00:00

    Hi!
    I'm still keep looking for the way to consume C++ library by C#. To make things simpler, I've reduced everything from current sample program.

    In the process, I realized that I cannot use/consume/refer XAML custom control (it is a "BgLabelControl") in Windows Runtime Component (WRC). Both of App and WRC are made in C++/WinRT. The library reference is done "by project (*.winmd)" because there is not .Net 6. In the referred directory seems to meet. Everything are there. WRC itself can be built without errors but the app can not be built.
    The error output is;
    C2039: 'CppXamlWRC': is not a member of 'winrt'
    The CppXamlWRC is the name of namespace. Now I'm trying to put some reference to App.xaml but not got any luck.

    The details are here. I've seen this error several times but those are handled by system.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.