gpt4 book ai didi

c# - 将转换器指定为 MarkupExtension 时在设计时出现 InvalidCastException

转载 作者:太空宇宙 更新时间:2023-11-03 12:46:33 25 4
gpt4 key购买 nike

我想实现值转换器来为 RenderTransform 指定点。在我只实现 IValueConverter 接口(interface)之前,它工作正常。我知道我可以实现 MarkupExtension 类,而不是每次都为我的转换器声明单独的 XAML 资源。当我尝试实现这个时,我有

InvalidCastException:无法将“System.Object”类型的对象转换为“System.Windows.Data.IValueConverter”类型。

转换器实现:

[ValueConversion(typeof(Point), typeof(Transform))]
public class PointToTransformConverter : MarkupExtension, IValueConverter
{
private PointToTransformConverter _instance = null;

public object Convert(
object value,
Type targetType,
object parameter,
CultureInfo culture)
{
var point = (Point)value;

return new TransformGroup
{
Children = new TransformCollection
{
new TranslateTransform(point.X, point.Y)
}
};
}

public object ConvertBack(
object value,
Type targetType,
object parameter,
CultureInfo culture)
{
var transform = value as TransformGroup;
if (transform?.Children.Count > 0)
{
var translateTransform = transform.Children[0] as TranslateTransform;
if (translateTransform != null)
{
return new Point(
translateTransform.X,
translateTransform.Y);
}
}

return null;
}

public override object ProvideValue(IServiceProvider serviceProvider)
{
return _instance ?? (_instance = new PointToTransformConverter());
}
}

XAML 用法:

<local:PathControl x:Class="PathToWiringTube.PathView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:PathToWiringTube"
xmlns:view="clr-namespace:PathToWiringTube.View"
xmlns:vm="clr-namespace:PathToWiringTube.VM"
xmlns:crocodile="clr-namespace:Crocodile;assembly=Crocodile"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance vm:PathVm}">
<local:PathControl.Resources>
<crocodile:PointToTransformConverter x:Key="pointToTransformConverter"/>
</local:PathControl.Resources>
<Canvas>
<ItemsControl x:Name="itemsMarkers" ItemsSource="{Binding Markers}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<view:SplineDensityMarker Width="5" RenderTransform="{Binding Position, Converter={StaticResource pointToTransformConverter}}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Canvas>
</local:PathControl>

我做错了什么?

最佳答案

能否请您尝试下面的代码并检查是否出现异常?我已经改正了。实例应该是静态的和xaml,你可以直接引用转换器而不是调用它作为静态资源。这是使用 MarkupExtension 的主要用途。下面的代码应该可以正常工作。如果有任何问题,请回复。

转换器:

[ValueConversion(typeof(Point), typeof(Transform))]
public class PointToTransformConverter : MarkupExtension, IValueConverter
{
private static PointToTransformConverter _instance = null;

public object Convert(
object value,
Type targetType,
object parameter,
CultureInfo culture)
{
var point = (Point)value;

return new TransformGroup
{
Children = new TransformCollection
{
new TranslateTransform(point.X, point.Y)
}
};
}

public object ConvertBack(
object value,
Type targetType,
object parameter,
CultureInfo culture)
{
var transform = value as TransformGroup;
if (transform?.Children.Count > 0)
{
var translateTransform = transform.Children[0] as TranslateTransform;
if (translateTransform != null)
{
return new Point(
translateTransform.X,
translateTransform.Y);
}
}

return null;
}

public override object ProvideValue(IServiceProvider serviceProvider)
{
return _instance ?? (_instance = new PointToTransformConverter());
}
}

Xaml:

<local:PathControl x:Class="PathToWiringTube.PathView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:PathToWiringTube"
xmlns:view="clr-namespace:PathToWiringTube.View"
xmlns:vm="clr-namespace:PathToWiringTube.VM"
xmlns:crocodile="clr-namespace:Crocodile;assembly=Crocodile"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance vm:PathVm}">
</local:PathControl.Resources>
<Canvas>
<ItemsControl x:Name="itemsMarkers" ItemsSource="{Binding Markers}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<view:SplineDensityMarker Width="5" RenderTransform="{Binding Position, Converter={crocodile:PointToTransformConverter}}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Canvas>
</local:PathControl>

关于c# - 将转换器指定为 MarkupExtension 时在设计时出现 InvalidCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37099482/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com