microsoft / microsoft/WindowsAppSDK

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

Open
#6,086 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

area-External bug needs-triage
Dominant language
C++
Stars
4.7k
Forks
471
Avg merge
3d 13h
Merged PRs (30d)
28

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the attached PrintingSample.zip and the PrintDocument pagination, preview-page, and add-pages handlers shown in the issue. Reproduce the dark and light RequestedTheme cases, then trace the WinUI print-preview behavior; done means the preview displays the page text when RequestedTheme is Dark.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
desktop
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.