WinUI3: How to load XAML file dynamically?

Minh Van 171 Reputation points
2022-12-14T04:34:23.577+00:00

Hi,

I have a requirement to load XAML file dynamically. I have some questions related to this:

  1. Can we load nested window inside a parent window? If not, is there any solution (page, user control)?
  2. Can or how the Frame control in NavigationView load the xaml file/strings? If not, is there any solution to load it?
  3. How can we interact with controls inside the xaml file if it is loaded successfully? There is no code behind for this file.

Thanks.

Windows development Windows App SDK
{count} votes

2 answers

Sort by: Most helpful
  1. Junjie Zhu - MSFT 21,646 Reputation points
    2022-12-14T10:20:23.823+00:00

    Hello @Minh Van ,

    Thanks for the information.

    Can we load nested window inside a parent window? If not, is there any solution (page, user control)?

    No, it is not supported in WinUI3. You can use Frame or NavigationView to show other Page.

    Can or how the Frame control in NavigationView load the xaml file/strings? If not, is there any solution to load it?
    How can we interact with controls inside the xaml file if it is loaded successfully? There is no code behind for this file.

    I understand what you want to achieve, but in WinUI3 you need to use WinRT component to achieve your needs.
    You need to add Page in the Windows Runtime Component (WinUI3) project, and then reference it in the main project.
    You can refer to the documents below.

    Walkthrough—Create a C# component with WinUI 3 controls, and consume it from a C++/WinRT app that uses the Windows App SDK

    Thank you.


    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.

    0 comments No comments

  2. Castorix31 90,681 Reputation points
    2022-12-14T10:31:22.267+00:00

    You can see the NavigationView samples in WinUI 3 Gallery
    which load pages from .xaml files (SamplePage1.xaml, SamplePage2, etc...) by using Tags
    (details in NavigationViewPage.xaml.cs, functions like NavigationView_SelectionChanged)


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.