Problem with abstract Page in Release build

Viewed 167

The situation

I have made an abstract Page for my UWP app: TestPage. This page contains some abstract items, in this simplified case just a string.

MVCE can be found here

C# code:

public abstract partial class TestPage : Page
{
    public abstract string AbstractName { get; }

    public TestPage()
    {
        InitializeComponent();
    }
}

XAML:

<Page
    x:Class="UWP_Test.TestPage"
    ...
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid>
        <TextBlock HorizontalAlignment="Center" 
                   VerticalAlignment="Center" 
                   Style="{StaticResource TitleTextBlockStyle}" 
                   Text="{x:Bind AbstractName}"/>
    </Grid>
</Page>

I then have made two derived pages, or rather classes:

public sealed class TestPage1 : TestPage
{
    public override string AbstractName => "TestPage1";
}
public sealed class TestPage2 : TestPage
{
    public override string AbstractName => "TestPage2";
}

The problem

If I now try to navigate a Frame to either TestPage1 or TestPage2, this will work in a DEBUG build, but fail in a RELEASE build (System.AccessViolationException: 'Attempted to read or write protected memory.'). This is regardless of whether optimize code or .net native toolchain are enabled or disabled.

For my actual UWP app I would like to be able to use aforementioned principle to quickly make very similar pages that differ in a few aspects such as ItemTemplates, Viewmodels, etc.

I'd appreciate any leads that could either make this work in a release build or how to solve this (without creating a separate XAML file for each derived page, I already use this somewhere else and that is working in release).

1 Answers

Apparently, the way I want it to work does not seem to supported in a Release build, seemingly due to the way the .xaml and .cs files are compiled for a Release or Debug build.

For now I'm going to use multiple .xaml files, but I will keep trying to find a way to make this work and prevent a lot of exactly the same .xaml code (after all using multiple copies of the same code is asking for problems somewhere down the line).

I will update this answer accordingly when I do find an answer. (Thanks to Hamed and Mikah for the help so far)

Related