Skip to content

PrintPreview window displays empty value when RequestedTheme is set to dark in WinUI Application #6086

@SweathaBharathi

Description

@SweathaBharathi

Describe the bug

In my WinUI application, I implemented printing functionality and set the RequestedTheme to Dark. However, the PrintPreview Window does not display any text when using the Dark theme. When the theme is set to Light, the text appears properly.

Source Code for Printing:


public sealed partial class MainWindow : Window
{
    private PrintDocument _printDoc;
    private IPrintDocumentSource _printSource;
    private List<UIElement> _pages = new List<UIElement>();
    public MainWindow()
    {
        InitializeComponent();


        _printDoc = new PrintDocument();
        _printSource = _printDoc.DocumentSource;

        _printDoc.Paginate += OnPaginate;
        _printDoc.GetPreviewPage += OnGetPreviewPage;
        _printDoc.AddPages += OnAddPages;
    }

    private async void OnPrintClick(object sender, RoutedEventArgs e)
    {
        var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
        var printManager = PrintManagerInterop.GetForWindow(hWnd);
        printManager.PrintTaskRequested += (s, args) =>
        {
            args.Request.CreatePrintTask("Print Test", t => t.SetSource(_printSource));
        };
        await PrintManagerInterop.ShowPrintUIForWindowAsync(hWnd);
    }


    private void OnPaginate(object sender, PaginateEventArgs e)
    {
        _pages.Clear();
        var page = new StackPanel { Margin = new Thickness(20) };
        page.Children.Add(new TextBlock { Text = "Hello, this is a print test!", FontSize = 24 });
        _pages.Add(page);
        _printDoc.SetPreviewPage(1, page);
    }

    private void OnGetPreviewPage(object sender, GetPreviewPageEventArgs e)
    {
        _printDoc.SetPreviewPage(e.PageNumber, _pages[e.PageNumber - 1]);
    }

    private void OnAddPages(object sender, AddPagesEventArgs e)
    {
        foreach (var p in _pages) _printDoc.AddPage(p);
        _printDoc.AddPagesComplete();
    }
}

Code for Dark Theme:

public partial class App : Application
{
    private Window? _window;

    /// <summary>
    /// Initializes the singleton application object.  This is the first line of authored code
    /// executed, and as such is the logical equivalent of main() or WinMain().
    /// </summary>
    public App()
    {
        this.RequestedTheme = ApplicationTheme.Dark;
        InitializeComponent();
    }
}

Why is this important?

My user needs to display the content in print preview window when the dark theme is set in a WinUI application

Steps to reproduce the bug

Step 1: Run the sample
Step 2: Click Print Test button

Actual behavior

PrintPreview window displays empty value when RequestedTheme is set to dark.

Expected behavior

PrintPreview window should displays actual content when RequestedTheme is set to dark.

Screenshots

Image Reference for Dark Theme:

Image

Image Reference for Light Theme:

Image

NuGet package version

None

Windows version

No response

Additional context

I have attached the sample for your reference:

PrintingSample.zip

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions