Skip to content

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

@MERRiiii1

Description

@MERRiiii1

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 ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions