Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TitleBar non-responsive in Powershell when UseModernWindowStyle is set to "True" #138

Closed
IvanDrag0 opened this issue Jul 13, 2020 · 6 comments
Labels
bug Something isn't working

Comments

@IvanDrag0
Copy link

I have the following WPF GUI I've made for Powershell using the ModernWpf library:

<Window x:Name="Window" x:Class="WpfApp1.MainWindow" 
    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:local="clr-namespace:WpfApp1" 
    xmlns:ui="http://schemas.modernwpf.com/2019" Style="{DynamicResource WindowStyle}" WindowStartupLocation="CenterScreen" Title="WpfApp1" mc:Ignorable="d" Height="450" Width="800">

    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/ModernWpf;component/ThemeResources/Light.xaml" />
                <ResourceDictionary Source="pack://application:,,,/ModernWpf;component/ControlsResources.xaml" />
            </ResourceDictionary.MergedDictionaries>

            <Style x:Key="WindowStyle" TargetType="Window">
                <Setter Property="Foreground" Value="{DynamicResource SystemControlPageTextBaseHighBrush}" />
                <Setter Property="Background" Value="{DynamicResource SystemControlPageBackgroundAltHighBrush}" />
                <Setter Property="FontFamily" Value="{DynamicResource ContentControlThemeFontFamily}" />
                <Setter Property="FontSize" Value="{DynamicResource ControlContentThemeFontSize}" />
                <Setter Property="UseLayoutRounding" Value="True" />
                <Setter Property="ui:ThemeManager.IsThemeAware" Value="True" />
                <Setter Property="ui:TitleBar.ExtendViewIntoTitleBar" Value="True" />
                <Setter Property="ui:WindowHelper.UseModernWindowStyle" Value="True" />
            </Style>
        </ResourceDictionary>
    </Window.Resources>

    <Grid>
        <ui:NavigationView x:Name="NavView" Header="Navigation" Height="Auto" PaneDisplayMode="LeftCompact" IsTabStop="False" OpenPaneLength="200" PaneTitle="Menu">

            <ui:NavigationView.AutoSuggestBox>
                <ui:AutoSuggestBox x:Name="NavViewSearchBox" QueryIcon="Find" />
            </ui:NavigationView.AutoSuggestBox>

            <ui:NavigationView.MenuItems>
                <ui:NavigationViewItem Content="Home" Icon="Home" ToolTipService.ToolTip="Home" Tag="SamplePage1" />
                <ui:NavigationViewItem Content="Account" Icon="Contact" ToolTipService.ToolTip="Account" Tag="SamplePage2">
                    <ui:NavigationViewItem.MenuItems>
                        <ui:NavigationViewItem Content="Mail" Icon="Mail" ToolTipService.ToolTip="Mail" Tag="SamplePage3" />
                        <ui:NavigationViewItem Content="Calendar" Icon="Calendar" ToolTipService.ToolTip="Calendar" Tag="SamplePage4" />
                    </ui:NavigationViewItem.MenuItems>
                </ui:NavigationViewItem>
                <ui:NavigationViewItem Content="Document options" Icon="Page2" ToolTipService.ToolTip="Document options" SelectsOnInvoked="False">
                    <ui:NavigationViewItem.MenuItems>
                        <ui:NavigationViewItem Content="Create new" Icon="NewFolder" ToolTipService.ToolTip="Create new" Tag="SamplePage5" />
                        <ui:NavigationViewItem Content="Upload file" Icon="OpenLocal" ToolTipService.ToolTip="Upload file" Tag="SamplePage6" />
                    </ui:NavigationViewItem.MenuItems>
                </ui:NavigationViewItem>
            </ui:NavigationView.MenuItems>
            <ui:NavigationView.Content>
                <ScrollViewer>
                    <ui:Frame x:Name="ContentFrame" Padding="10,10,10,10" IsTabStop="True" />
                </ScrollViewer>
            </ui:NavigationView.Content>
        </ui:NavigationView>
    </Grid>
</Window>

When I set the UseModernWindowStyle line to "True", the GUI looks and behaves as expected, however, the TitleBar is not responding to movement and/or maximizing the window by double clicking on it. The minimize, maximize, and close buttons work as expected. Also, the icon in the top left (next to the title) disappears.

image

When I set the line to "False", the window looks and behaves as expected (with the icon on the top left showing up correctly), however, there's a gap between the TitleBar and the NavigationView back button.

image

I tried this on the Powershell example in the repo and I'm getting the same results (so I'm not sure if it's an issue with the code or with my computer).

@Kinnara
Copy link
Owner

Kinnara commented Jul 14, 2020

This is indeed a problem. I'll look into it.

@Kinnara Kinnara added the bug Something isn't working label Jul 18, 2020
@Kinnara
Copy link
Owner

Kinnara commented Jul 19, 2020

Should be fixed in the latest source. Until a new version is released, you can work around this issue by setting WindowChrome explicitly:

<Window
    ...
    ui:WindowHelper.UseModernWindowStyle="True">
    <Window.Resources>
        ...
    </Window.Resources>
    <WindowChrome.WindowChrome>
        <WindowChrome
            CaptionHeight="32"
            CornerRadius="0"
            GlassFrameThickness="0,1,0,0"
            UseAeroCaptionButtons="False" />
    </WindowChrome.WindowChrome>
    ...
</Window>

@futuremotiondev
Copy link

@IvanDrag0 Sorry to hijack this thread - but I've been trying to get ModernWpf to work properly in PowerShell all day. Is there any way you can post your full source code for the example application you show above? I managed to cobble together a form:
zCrgLh5

But the styling doesn't at all match ModernWpf's design.

Here's the ps1:

Set-Location $PSScriptRoot
[IO.Directory]::SetCurrentDirectory($PSScriptRoot)

Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName PresentationCore,PresentationFramework

[Reflection.Assembly]::LoadFrom((Join-Path $PSScriptRoot 'assembly\ModernWpf.dll')) | Out-Null
[Reflection.Assembly]::LoadFrom((Join-Path $PSScriptRoot 'assembly\ModernWpf.Controls.dll')) | Out-Null
[Reflection.Assembly]::LoadFrom((Join-Path $PSScriptRoot 'assembly\System.ValueTuple.dll')) | Out-Null

$xaml = Get-Content "MainWindow.xaml" -Raw
$window = [Windows.Markup.XamlReader]::Parse($xaml)
$window.ShowDialog()

And here's the XAML:

<Window x:Name="MainWindow1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:ui="http://schemas.modernwpf.com/2019"
        xmlns:local="clr-namespace:MainApplication"
        ui:WindowHelper.UseModernWindowStyle="True"
        Title="MainWindow" 
        Height="434"
        Width="800"
        ui:TitleBar.ExtendViewIntoTitleBar="True">

    <Canvas>
        <Label Content="Modern WPF Dialog Test" Canvas.Left="28" Canvas.Top="26" HorizontalAlignment="Left" FontSize="36"/>
        <Button Content="Yes" Canvas.Left="647" Canvas.Top="326" Width="118" HorizontalAlignment="Center" Height="45" VerticalAlignment="Top" Foreground="White" Background="#FF0947C3" FontSize="18"/>
        <Button Content="No" Canvas.Left="515" Canvas.Top="326" Width="118" Height="45" FontSize="18" HorizontalAlignment="Center" VerticalAlignment="Top"/>
        <ComboBox Canvas.Left="34" Canvas.Top="120" Width="250" VerticalAlignment="Center"/>
        <ComboBox Canvas.Left="312" Canvas.Top="120" Width="306" VerticalAlignment="Center"/>
        <DatePicker Canvas.Left="643" Canvas.Top="120"/>
    </Canvas>
</Window>

I couldn't get your XAML to work. This is what happens when I try your XAML:

QKSnSYm

Any help at all would be amazing!

Thanks.

@IvanDrag0
Copy link
Author

IvanDrag0 commented Apr 6, 2022

@visusys My XAML doesn't work for you because you can't import the XAML directly into PowerShell. There's some "cleanup" that you have to do. Take a look at this example and this one. Also, you don't need to use the [Windows.Markup.XamlReader]::Parse method. Powershell is very good at identifying the correct type and parsing it as needed. You can just do [xml]$GUI = Get-Content "MainWindow.xaml" -Raw.

The reason your XAML is not working is probably because you're missing the resource dictionaries (the <Window.Resources> section in my XAML). Normally it's located in the App.xaml file, but in PowerShell it's easier to make it into one big file and rename <Application.Resources> to <Window.Resources>.

Hope this helps!

@futuremotiondev
Copy link

@IvanDrag0 Thanks SO much for the guidance and the links. I've gone through both of them thoroughly and unfortunately I still think I'm missing something crucial. I feel like I've tried everything.

This is what I have sofar:

https://gist.github.com/visusys/64570f64e5bfccb75ddb34ad82387d16

But I'm getting a lot of errors:

Cannot convert value "System.Xml.XmlDocument" to type "System.Xml.XmlDocument". Error: "The specified node cannot be inserted as the valid child of this node, because the specified node is the wrong type."

and:

Code_RBZ9AnDKJD

I'm trying to follow this guide.

  • I've tried removing the x:Class="WpfApp1.MainWindow" attribute.
  • I've tried manually removing all x: namespace prefixes as mentioned here
  • I've tried completely removing the $inputXML = $inputXML -replace 'mc:Ignorable="d"','' -replace "x:N",'N' -replace '^<Win.*', '<Window' line that I originally plucked from here because it was throwing errors.

And a whole bunch of other various tweaks to no avail.

Would it all be possible for you to drop a working example .ps1 in a gist and link me? I've been trying to get this working for two days straight now and I'm totally burnt out and frustrated.

Thanks again for the help!

@IvanDrag0
Copy link
Author

@visusys

Here's the XAML I used:

<Window x:Name="Window" x:Class="WpfApp1.MainWindow" 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:local="clr-namespace:WpfApp1" xmlns:ui="http://schemas.modernwpf.com/2019" Style="{DynamicResource WindowStyle}" WindowStartupLocation="CenterScreen" Title="WpfApp1" mc:Ignorable="d" Height="450" Width="800">

    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/ModernWpf;component/ThemeResources/Light.xaml" />
                <ResourceDictionary Source="pack://application:,,,/ModernWpf;component/ControlsResources.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>

    <Grid>
        <ui:NavigationView x:Name="NavView" Header="Navigation" Height="Auto" PaneDisplayMode="LeftCompact" IsTabStop="False" OpenPaneLength="200" PaneTitle="Menu">

            <ui:NavigationView.AutoSuggestBox>
                <ui:AutoSuggestBox x:Name="NavViewSearchBox" QueryIcon="Find" />
            </ui:NavigationView.AutoSuggestBox>

            <ui:NavigationView.MenuItems>
                <ui:NavigationViewItem Content="Home" Icon="Home" ToolTipService.ToolTip="Home" Tag="SamplePage1" />
                <ui:NavigationViewItem Content="Account" Icon="Contact" ToolTipService.ToolTip="Account" Tag="SamplePage2">
                    <ui:NavigationViewItem.MenuItems>
                        <ui:NavigationViewItem Content="Mail" Icon="Mail" ToolTipService.ToolTip="Mail" Tag="SamplePage3" />
                        <ui:NavigationViewItem Content="Calendar" Icon="Calendar" ToolTipService.ToolTip="Calendar" Tag="SamplePage4" />
                    </ui:NavigationViewItem.MenuItems>
                </ui:NavigationViewItem>
                <ui:NavigationViewItem Content="Document options" Icon="Page2" ToolTipService.ToolTip="Document options" SelectsOnInvoked="False">
                    <ui:NavigationViewItem.MenuItems>
                        <ui:NavigationViewItem Content="Create new" Icon="NewFolder" ToolTipService.ToolTip="Create new" Tag="SamplePage5" />
                        <ui:NavigationViewItem Content="Upload file" Icon="OpenLocal" ToolTipService.ToolTip="Upload file" Tag="SamplePage6" />
                    </ui:NavigationViewItem.MenuItems>
                </ui:NavigationViewItem>
            </ui:NavigationView.MenuItems>
            <ui:NavigationView.Content>
                <ScrollViewer>
                    <ui:Frame x:Name="ContentFrame" Padding="10,10,10,10" IsTabStop="True" />
                </ScrollViewer>
            </ui:NavigationView.Content>
        </ui:NavigationView>
    </Grid>
</Window>

Here's the Powershell code:

# Load assemblies
Add-Type -AssemblyName PresentationCore, PresentationFramework
Get-ChildItem -Path "$PSScriptRoot\lib\net462" -Filter '*.dll' -Recurse | ForEach-Object { Add-Type -Path $_.FullName }

# Load GUI XAML
[xml]$XAML = Get-Content "$PSScriptRoot\GUI.xml" -Raw

# Declare which XML properties to remove so Powershell can properly process the XAML
$UnwantedXMLProperties = @('mc:Ignorable', 'x:Class')

# Clean the XAML file (this is a better/safer way of doing it than the examples)
foreach ($Property in $UnwantedXMLProperties) {
    $PropToRemove = $xaml.Window.Attributes | Where-Object { $_.Name -eq $Property}
    $xaml.Window.Attributes.Remove($PropToRemove)
}

# Initialize the XML reader
$reader = (New-Object System.Xml.XmlNodeReader $XAML)
$Form = [Windows.Markup.XamlReader]::Load($reader)

# Show the GUI
$Form.ShowDialog() | Out-Null

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants