Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C# How to use multiple auxiliary xaml files in Xaml for Win UI 3

jv1597

Coder
I'm trying to get multiple Xaml files to work with my application to get my app code organized. I've tried multiple would-be solutions to no avail. I've tried a couple of XamlReader solutions I couldn't get working. Here are some of the things I've tried:

Simple Xaml code:
Code:
<Grid RowDefinitions="Auto,*">
    <Button
        Grid.Row="0"
        Click="LoadXamlButton_Click"
        Content="Load XAML" />
    <Frame
        x:Name="DynamicFrame"
        Grid.Row="2" />
</Grid>

Simple solution provided by experts on StackOverflow:
Code:
private async void LoadXamlButton_Click(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
{
    string loadedXamlText = await File.ReadAllTextAsync("AuxFile.xaml");

    if (Microsoft.UI.Xaml.Markup.XamlReader.Load(loadedXamlText) is FrameworkElement dynamicElement)
    {
        this.DynamicFrame.Content = dynamicElement;
    }
}

I would prefer a simpler approach such as the following:
Code:
<Page x:Name="AuxFile" Resources="AuxFile.xaml"></Page>

Does anyone have any ideas on how to get this working? Thanks in advance.
 
I'm trying to get multiple Xaml files to work with my application to get my app code organized. I've tried multiple would-be solutions to no avail. I've tried a couple of XamlReader solutions I couldn't get working. Here are some of the things I've tried:

Simple Xaml code:
Code:
<Grid RowDefinitions="Auto,*">
    <Button
        Grid.Row="0"
        Click="LoadXamlButton_Click"
        Content="Load XAML" />
    <Frame
        x:Name="DynamicFrame"
        Grid.Row="2" />
</Grid>

Simple solution provided by experts on StackOverflow:
Code:
private async void LoadXamlButton_Click(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
{
    string loadedXamlText = await File.ReadAllTextAsync("AuxFile.xaml");

    if (Microsoft.UI.Xaml.Markup.XamlReader.Load(loadedXamlText) is FrameworkElement dynamicElement)
    {
        this.DynamicFrame.Content = dynamicElement;
    }
}

I would prefer a simpler approach such as the following:
Code:
<Page x:Name="AuxFile" Resources="AuxFile.xaml"></Page>

Does anyone have any ideas on how to get this working? Thanks in advance.
Hey there,
So, you may need to use a ResourceDictionary like so,

Code:
<Application
   …
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="MyTheme.xaml" />
               <ResourceDictionary Source="MyTheme.xaml" />...
                <!-- Other merged dictionaries here -->
            </ResourceDictionary.MergedDictionaries>
            <!-- Other app resources here -->
        </ResourceDictionary>
    </Application.Resources>
</Application>

Taken from: How to create and customize a C# WinUI 3 desktop app
 
Last edited:
Antero,
I tried the code you posted. Didn't work. I wasn't sure how the file was to be imported, accessed, or applied. Any ideas on how to get it working? I've tried some searching online but have only come across XamlReader method info, which hasn't worked for me as of yet.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom