What am I missing? I was trying to implement converters to my XAML-based PowerShell script but with no luck. I've picked up pieces of information from sites like StackOverflow. but couldn't find one successful implementation of a converter in powershell XAML-based GUI script.
in the code I am testing the converter, and it works (you can see 2 examples for conversion) so that means that powershell itself accepted the new converter type, buy this converter cannot be implemented in my xaml code.
$src = @'
using System;
using System.Windows;
using System.Windows.Data;
using System.Globalization;
namespace MyProject
{
public class DemoConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
{
return "kuku";
}
else
{
return "bobo";
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}
}
}
'@
Add-Type -AssemblyName PresentationFramework
Add-Type -TypeDefinition $src -ReferencedAssemblies PresentationFramework
#Checking that the new type works and convert is done...
$c = new-object MyProject.DemoConverter
$c.Convert("gg", $null, $null, $null)
$c.Convert(55, $null, $null, $null)
#Now declaring and loading the xaml
[xml]$XAML = @'
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cnv="clr-namespace:MyProject" >
<Window.Resources>
<cnv:DemoConverter x:Key="TestConverter" />
</Window.Resources>
<Grid>
<TextBox x:Name="txtTestValue" Text="I'm here to show that xaml loading works!" />
</Grid>
</Window>
'@
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$Window=[Windows.Markup.XamlReader]::Load( $reader )
$Window.ShowDialog() | out-null
I keep getting this error:
Exception calling "Load" with "1" argument(s): "Cannot create unknown type '{clr-namespace:MyProject}DemoConverter'."
If I remove the line: <cnv:DemoConverter x:Key="TestConverter" />
It will not give the above error and the window will show (but of course, convertion in xaml will not be available), so I guess I'm doing something wrong with namespace and/or assembly deceleration that XAML doesn't like.
Note that on my xaml I'm not yet using the converter. I just want to overcome the above error before trying to use the converter.
Thank you so much in advance!
I signed up for Stackoverflow just so I could participate in this question.
I searched for HOURS for a solution to this before I ran across this question here, and then my hopes were dashed to see it's been sitting out here almost a year with no answer.
I had found this related question earlier, and it gives the basic solution we're trying to implement with this question, but doesn't give any details on making it actually work. How to use IValueConverter from powershell?
Luckily, after several more hours, and piecing together a lot of other info, I finally solved it!
There are 3 pieces to it.
SO, see the updated code I posted which has the following changes. As the original poster indicated, this example doesn't show the converter in action, but the script will at least run and display the form without error, indicating the the form itself accepted the converter definition and usage in the TextBox.
Code
$src = @'
using System;
using System.Windows;
using System.Windows.Data;
using System.Globalization;
namespace MyConverter
{
public class DemoConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
{
return "kuku";
}
else
{
return "bobo";
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}
}
}
'@
Add-Type -AssemblyName PresentationFramework
Add-Type -TypeDefinition $src -ReferencedAssemblies PresentationFramework
#Checking that the new type works and convert is done...
$c = new-object MyConverter.DemoConverter
$AssemblyName = $c.gettype().Assembly.FullName.Split(',')[0]
#$c.Convert("gg", $null, $null, $null)
#$c.Convert(55, $null, $null, $null)
#Now declaring and loading the xaml
$inputXML = @'
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Converter="clr-namespace:MyConverter;assembly=myassembly"
xmlns:cnv="clr-namespace:MyProject" >
<Window.Resources>
<Converter:DemoConverter x:Key="TestConverter" />
</Window.Resources>
<Grid>
<TextBox x:Name="txtTestValue" Text="I'm here to show that xaml loading works!" />
<TextBox x:Name="txtTestValue2" Text="{Binding Path=Whatever, Converter={StaticResource TestConverter}}" />
</Grid>
</Window>
'@
[xml]$XAML = $inputXML -replace 'myassembly', $AssemblyName
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$Window=[Windows.Markup.XamlReader]::Load( $reader )
$Window.ShowDialog() | out-null
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.