0

In a release build, XamlReader.Load fails trying to read the 3rd party control from Windows Community Toolkit shown in the example. It also fails whenever it tries to read some of my custom controls, but not others. None of them fail to read in debug builds.

I have a minimal test project repro here. Runs fine in debug, fails in release.

I have no idea how this could be happening. If it's a UWP restriction why would it work in debug builds?

MainPage.xaml.cs

public MainPage()
{
    this.Loaded += MainPage_Loaded;
    this.InitializeComponent();
}

private async void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    var xamlStr = File.ReadAllText("TestView.xaml");
    try
    {
        var runtimeXaml = XamlReader.Load(xamlStr) as UserControl;
        this.Content = runtimeXaml;
    }
    catch (Exception ex)
    {
        var messageDialog = new MessageDialog(ex.Message);
        await messageDialog.ShowAsync();
    }
}

TestView.xaml

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:media="using:Microsoft.Toolkit.Uwp.UI.Media"
    mc:Ignorable="d">

    <Grid Width="400" Height="400">
        <Grid.Background>
            <media:AcrylicBrush TintColor="Orange" BlurAmount="4" />
        </Grid.Background>
        <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center">
            Runtime XAML Loaded Successfully!</TextBlock>
    </Grid>
</UserControl>
Sean O'Neil
  • 1,222
  • 12
  • 22
  • 1
    It sounds like optimization for release build when part of libraries you use can be ignored and not copied over to build (if MSBuild thinks that they are not in use - it can do that, sometimes resolving in errors, because they are in use but indirectly). You could try to use some component or anything from the libraries directly in the code (somethig dumb, like just creating new object, but not using it) just to show MSBuild that it is in use – Nikita Chayka Dec 15 '20 at 16:47
  • That makes sense I'll give it a shot. – Sean O'Neil Dec 15 '20 at 16:51
  • So yeah that's exactly what's happening. Unfortunately just creating an object in code doesn't work, it has to be in a compile-time XAML somewhere. This is actually a serious problem for me. Is there any way to tell the compiler to not do this? – Sean O'Neil Dec 15 '20 at 17:22
  • You have to use that directly, or copy needed files after build manually, that's the only options i think of. You could find discussion here - https://stackoverflow.com/questions/15816769/dependent-dll-is-not-getting-copied-to-the-build-output-folder-in-visual-studio – Nikita Chayka Dec 15 '20 at 17:33

0 Answers0