【发布时间】:2019-06-29 09:55:38
【问题描述】:
我错过了什么?我试图为我的基于 XAML 的 PowerShell 脚本实现转换器,但没有运气。 我从 StackOverflow 等网站收集了一些信息。但在基于 powershell XAML 的 GUI 脚本中找不到一个成功的转换器实现。
在我正在测试转换器的代码中,它可以工作(您可以看到 2 个转换示例),这意味着 powershell 本身接受了新的转换器类型,购买此转换器无法在我的 xaml 代码中实现。
$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
我不断收到此错误:
使用“1”参数调用“Load”的异常:“无法创建未知类型'{clr-namespace:MyProject}DemoConverter'。”
如果我删除该行:<cnv:DemoConverter x:Key="TestConverter" />
它不会给出上述错误并且窗口将显示(但当然,xaml 中的转换将不可用),所以我想我在命名空间和/或 XAML 不喜欢的程序集减速方面做错了。
请注意,在我的 xaml 上,我还没有使用转换器。我只是想在尝试使用转换器之前克服上述错误。
非常感谢您!
【问题讨论】:
标签: wpf powershell xaml ivalueconverter