Skip to content

Windows 11 Theming Does Not Work In PowerShell WPF Window #9373

Closed

Description

@nkasco

URL(s)

https://github.com/dotnet/core/blob/main/release-notes/9.0/preview/preview4/9.0.0-preview.4.md

Description

The following code should render a Windows 11 style WPF Window in PowerShell 7 (fx dependent, using .NET 9 Preview 4)

Add-Type -AssemblyName PresentationFramework

[xml]$xaml = @"
<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Name="Window">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/PresentationFramework.Fluent;component/Resources/Fluent.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
    <StackPanel>
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
            <TextBox x:Name="MyTextBox" Width="150" />
            <Button x:Name="SubmitButton" Content="Submit" Margin="5" />
        </StackPanel>
    </StackPanel>
</Window>
"@

$reader = (New-Object System.Xml.XmlNodeReader $xaml)
$window = [Windows.Markup.XamlReader]::Load($reader)

$window.ShowDialog()

Current Result: Black window, although I can tell the elements are there because if I hover over the textbox my cursor changes. Doesn't appear to be properly loading the ResourceDictionary
image

Confirmed that my PowerShell 7 console is using .NET 9 preview 4:

> [System.Runtime.InteropServices.RuntimeInformation]::FrameworkDescription
.NET 9.0.0-preview.4.24266.19

Perhaps I am just missing something beyond adding the proper ResourceDictionary?

Activity

  1. antonfirsov commented on Jun 2, 2024

    @antonfirsov

    cc @dotnet/wpf-developers

  2. dipeshmsft commented on Jun 3, 2024

    @dipeshmsft
    Member

    Hey @antonfirsov, there is a slight issue here. As of now, the mechanism for accent color, theme switching, etc. will work when the resource dictionary is added to App.xaml, in coming preview releases we will add the support for using Win11 themes at window level explictly.

    For now, you will have to add it in your app.xaml.

  3. nkasco commented on Jun 3, 2024

    @nkasco
    Author

    Hey @antonfirsov, there is a slight issue here. As of now, the mechanism for accent color, theme switching, etc. will work when the resource dictionary is added to App.xaml, in coming preview releases we will add the support for using Win11 themes at window level explictly.

    For now, you will have to add it in your app.xaml.

    @dipeshmsft Appreciate this insights. Is there an issue, branch, or PR that can be tracked to know when it is released?

  4. nkasco commented on Jun 12, 2024

    @nkasco
    Author

    Not fixed still in .NET 9 preview 5

  5. nkasco commented on Jul 9, 2024

    @nkasco
    Author

    Not fixed still in .NET 9 preview 6

  6. antonfirsov commented on Jul 10, 2024

    @antonfirsov

    @dotnet/wpf-developers is there a tracking issue in dotnet/wpf we can link, so we can close this? Or should we transfer this one?

  7. dipeshmsft commented on Jul 10, 2024

    @dipeshmsft
    Member

    @nkasco @antonfirsov we are currently working on theme switching feature for Fluent, so we couldn't get back to this. However, we will fix this soon enough. For now, I will add it in the tracking issue and assign the label to this bug.

  8. transferred this issue fromdotnet/coreon Jul 10, 2024
  9. dipeshmsft commented on Jul 25, 2024

    @dipeshmsft
    Member

    @nkasco, can you tell me if there is a way to test this with a local set of binaries ? We have just merged the Theme switching APIs, and wanted to test this with the recent changes.

  10. nkasco commented on Jul 25, 2024

    @nkasco
    Author

    @nkasco, can you tell me if there is a way to test this with a local set of binaries ? We have just merged the Theme switching APIs, and wanted to test this with the recent changes.

    @dipeshmsft
    I have been using the fxdependent version of PowerShell: https://github.com/PowerShell/PowerShell/releases/download/v7.4.4/PowerShell-7.4.4-win-fxdependentWinDesktop.zip

    If that doesn't work, instead of Add-Type -AssemblyName PresentationFramework you could try Add-Type -Path "path\to\PresentationFramework.dll" (might also need to load in PresentationFramework.Fluent.dll if you do that)

  11. nkasco commented on Aug 16, 2024

    @nkasco
    Author

    @dipeshmsft Still not working in .NET 9 preview 7, although the resource path changed per #9580

    Updated test script:

    Add-Type -AssemblyName PresentationFramework
    
    [xml]$xaml = @"
    <Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Name="Window">
        <Window.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="pack://application:,,,/PresentationFramework.Fluent;component/Themes/Fluent.xaml" />
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Window.Resources>
        <StackPanel>
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                <TextBox x:Name="MyTextBox" Width="150" />
                <Button x:Name="SubmitButton" Content="Submit" Margin="5" />
            </StackPanel>
        </StackPanel>
    </Window>
    "@
    
    $reader = (New-Object System.Xml.XmlNodeReader $xaml)
    $window = [Windows.Markup.XamlReader]::Load($reader)
    
    $window.ShowDialog()
    
  12. dipeshmsft commented on Aug 27, 2024

    @dipeshmsft
    Member

    The changes to fix this have finally made it in .NET 9 RC1. Can you give it a try with the latest RC1 / RC2 release ?

  13. HotCakeX commented on Aug 27, 2024

    @HotCakeX
    Contributor

    The changes to fix this have finally made it in .NET 9 RC1. Can you give it a try with the latest RC1 / RC2 release ?

    Where can we download them? the latest available one i see is preview 7
    https://dotnet.microsoft.com/en-us/download/dotnet/9.0

  14. dipeshmsft commented on Aug 27, 2024

    @dipeshmsft
    Member
  15. nkasco commented on Aug 27, 2024

    @nkasco
    Author

    @dipeshmsft We are in business! I didn't do an extensive test on all object types, but definitely seems to be propagating the theming from the Window element now.

    image

    If I find anything odd I will report it back here. If I don't find anything in the next week, or unless you know of an reason otherwise, at that point we can probably close this one out.

  16. dipeshmsft commented on Aug 28, 2024

    @dipeshmsft
    Member

    If I am not wrong, I think I saw a comment stating otherwise yesterday. What was the issue earlier ?
    I also wanted to state that you can give Window.ThemeMode property a try.

  17. HotCakeX commented on Aug 28, 2024

    @HotCakeX
    Contributor

    If I am not wrong, I think I saw a comment stating otherwise yesterday. What was the issue earlier ? I also wanted to state that you can give Window.ThemeMode property a try.

    You're right, i posted a comment before but deleted it because i figured it out.

    Can we use the new Windows 11 style themes on <UserControl> elements too? because it doens't seem to work when I try it like this

        <UserControl.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="pack://application:,,,/PresentationFramework.Fluent;component/Themes/Fluent.xaml" />
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </UserControl.Resources>

    in

    Add-Type -Path 'C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App\9.0.0-rc.2.24425.1\PresentationFramework.dll'
    
    [xml]$xaml = @'
    <UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <UserControl.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="pack://application:,,,/PresentationFramework.Fluent;component/Themes/Fluent.xaml" />
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </UserControl.Resources>
        <StackPanel>
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                <TextBox x:Name="MyTextBox" Width="150" />
                <Button x:Name="SubmitButton" Content="Submit" Margin="5" />
            </StackPanel>
        </StackPanel>
    </UserControl>
    '@
    
    $reader = (New-Object System.Xml.XmlNodeReader $xaml)
    $userControl = [Windows.Markup.XamlReader]::Load($reader)
    
    $window = New-Object System.Windows.Window
    $window.Content = $userControl
    $window.Width = 300
    $window.Height = 200
    $window.WindowStartupLocation = 'CenterScreen'
    $window.Title = 'UserControl in Window'
    
    $window.ShowDialog()

    This is what I see

    image


  18. dipeshmsft commented on Aug 28, 2024

    @dipeshmsft
    Member

    @HotCakeX , in this case you can make it work by setting ThemeMode on window rather than including the resource dictionary

  19. dipeshmsft commented on Sep 4, 2024

    @dipeshmsft
    Member

    @HotCakeX @nkasco thanks for bringing this issue. It took us some time to get to it but since this is fixed now, I am closing this issue now. If you have any more issues, feel free to open a new issue.

  20. HotCakeX commented on Sep 4, 2024

    @HotCakeX
    Contributor

    @dipeshmsft Thank u very much!

  21. locked and limited conversation to collaborators on Oct 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions