Write a GUI in Powershell – Part 5

5a. Create an other C# class

using System;
using System.ComponentModel;
using System.Globalization;
using System.Windows.Data;
using System.Management.Automation;
public class MultiValueConverterWrap : IMultiValueConverter
{
public Func<object[], Type, object, CultureInfo, PSObject> ConvertPS;
public Func<object, Type[], object, CultureInfo, PSObject> ConvertBackPS;
public int debugLevel = 0;

public object Convert(object[] value, Type targetType, object parameter, CultureInfo culture)
{
try
{
DebugPrint("ConverterWrap: Convert: ");
PSObject p = ConvertPS(value, targetType, parameter, culture);
if (p == null)
{
DebugPrint("ConverterWrap: ConvertBack: null");
return null;
}
else
{
//Object o = p.ImmediateBaseObject;
Object o = p.BaseObject;
//Type t = o.GetType;
return o;
}
}
catch (Exception e)
{
DebugPrint(e.Message);
return System.Windows.DependencyProperty.UnsetValue;
}
}

public object[] ConvertBack(object value, Type[] targetType, object parameter, System.Globalization.CultureInfo culture)
{
try
{
DebugPrint("ConverterWrap: ConvertBack: ");
PSObject p = ConvertBackPS(value, targetType, parameter, culture);
DebugPrint("ConverterWrap: ConvertBack: " + p + "\t" + p.GetType());
//Object o = p.ImmediateBaseObject;
Object[] o = (Object[])p.BaseObject;
return o;
}
catch (Exception e)
{
DebugPrint(e.Message);
return null; //System.Windows.DependencyProperty.UnsetValue;
}
}

protected void DebugPrint(String s)
{
if (debugLevel == 1)
{
Console.WriteLine(s);
}
}
}

5b. add to Powershell
$csharp3=Get-Content (Join-Path $path "MultiValueConverterWrap.cs") -Raw
Add-Type -TypeDefinition $csharp3 -ReferencedAssemblies @("PresentationFramework";"WindowsBase")

Advertisements
This entry was posted in C#, Powershell, XAML. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s