Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

The invocation of the constructor on type 'MahApps.Metro.Controls.MetroWindow' that matches the specified binding constraints threw an exception #4482

Closed
MERRiiii1 opened this issue Apr 30, 2024 · 2 comments

Comments

@MERRiiii1
Copy link

MERRiiii1 commented Apr 30, 2024

Hey,

I am currently trying to understand how custom assemblies work with Powershell and WPF, but I'm really knew to all this and can't find any detailled tutorials on this, so I stumbled on this error and I need help to solve it...

I'm really new to custom assemblies so I don't really know how to use them with Powershell and Xaml. Here's what I did :

  • Created new WPF project in VS and used Install-Package MahApps.Metro in PM Console
  • Located the .dll files that were just downloaded, and copied it to a new directory named "Themes" in the WPF project subdirectory (tried multiple net versions)
  • Created the .PS1 and modified the XAML according to the MahApps Quick Start guide (loading .dll assemblies in the script and changing the <Window ... </Window> tag to <mah:MetroWindow ... </mah:MetroWindow>.... and adding xmlns:mah="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"

The script

Add-Type -AssemblyName PresentationFramework
Add-Type -AssemblyName PresentationCore
Add-Type -AssemblyName WindowsBase
$AssemblyLocation = Join-Path -Path $PSScriptRoot -ChildPath .\Themes
foreach ($Assembly in (Dir $AssemblyLocation -Filter *.dll)) {
     [System.Reflection.Assembly]::LoadFrom($Assembly.fullName) | out-null
}
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null

$inputXML = Get-Content "$PSScriptRoot\MainWindow.xaml"
$inputXML = $inputXML -replace 'mc:Ignorable="d"', '' -replace "x:N", 'N' -replace '^<Win.*', '<Window'
[XML]$XAML = $inputXML


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

$xaml.SelectNodes("//*[@Name]") | ForEach-Object {
    try { Set-Variable -Name "var_$($_.Name)" -Value $window.FindName($_.Name) -ErrorAction "Continue"
    } catch { throw
    }
}
$window.ShowDialog() | Out-Null

The XAML :

<mah:MetroWindow 
        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:mah="http://metro.mahapps.com/winfx/xaml/controls"
        Title="MainWindow" Height="450" Width="800">

    <Grid>
    </Grid>

</mah:MetroWindow>

When launching the script I get this error :

Exception calling "Load" with "1" argument(s): "The invocation of the constructor on type 'MahApps.Metro.Controls.MetroWindow' that matches the specified binding constraints threw an exception.
+     $window = [Windows.Markup.XamlReader]::Load( $reader )
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : XamlParseException

I really don't know what to do now, I don't even know how to get details about this exception. Can someone help me ?

@MERRiiii1
Copy link
Author

I've re-downloaded the .dll files and re-copied them, and now for some reasons, my Powershell sessions just die when I launch it ; no error, no popup, nothing. Didn't change the code, nothing explains the session being killed instantly

@timunie
Copy link
Collaborator

timunie commented Apr 30, 2024

It's not really fair to open an issue for a question. Please close it and use Discussions for Q&As.

I can't answer here as I never used PS with MahApps, but maybe anyone else can if you attach a very minimal sample.

@MahApps MahApps locked and limited conversation to collaborators Apr 30, 2024
@punker76 punker76 converted this issue into discussion #4483 Apr 30, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Development

No branches or pull requests

2 participants