gpt4 book ai didi

c# - ValueConverter 的依赖属性

转载 作者:行者123 更新时间:2023-11-30 17:22:54 25 4
gpt4 key购买 nike

使用 Silverlight 4,我试图在我的 UserControl 的资源部分中初始化一个转换器,并引用我的控件中的一个对象。当我尝试运行应用程序时出现此异常,注意实际 XAML 文件中的第 16 行是转换器:PointConverter.... 行:

System.Windows.Markup.XamlParseException: 2260 An error has occurred. [Line: 16 Position: 58]
at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
at WheresMyCar.View.Map.InitializeComponent()
at WheresMyCar.View.Map..ctor()
at System.Reflection.RuntimeConstructorInfo.InternalInvoke(RuntimeConstructorInfo rtci, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)
at System.Reflection.RuntimeConstructorInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
at System.Activator.InternalCreateInstance(Type type, Boolean nonPublic, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(Type type)
at System.Windows.Navigation.PageResourceContentLoader.BeginLoad_OnUIThread(AsyncCallback userCallback, PageResourceContentLoaderAsyncResult result)
at System.Windows.Navigation.PageResourceContentLoader.<>c__DisplayClass4.<BeginLoad>b__0(Object args)
at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)
at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark) at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) at System.Delegate.DynamicInvokeImpl(Object[] args)
at System.Delegate.DynamicInvoke(Object[] args)
at System.Windows.Threading.DispatcherOperation.Invoke()
at System.Windows.Threading.Dispatcher.Dispatch(DispatcherPriority priority)
at System.Windows.Threading.Dispatcher.OnInvoke(Object context)
at System.Windows.Hosting.CallbackCookie.Invoke(Object[] args)
at System.Windows.Hosting.DelegateWrapper.InternalInvoke(Object[] args)
at System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr pHandle, Int32 nParamCount, ScriptParam[] pParams, ScriptParam& pResult)

XAML:

<UserControl.Resources>
<converter:PointConverter x:Key="pointConverter" Map="{Binding ElementName=ThingMap}" />
</UserControl.Resources>
<Grid>
<m:Map
x:Name="ThingMap" />
</Grid>

点转换器类:

public class PointConverter :
DependencyObject,
IValueConverter
{
public Microsoft.Maps.MapControl.Map Map
{
get { return (Microsoft.Maps.MapControl.Map)GetValue(MapProperty); }
set { SetValue(MapProperty, value); }
}

// Using a DependencyProperty as the backing store for Map. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MapProperty =
DependencyProperty.Register("Map", typeof(Microsoft.Maps.MapControl.Map), typeof(PointConverter), null);

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string param = (string)parameter;

Microsoft.Maps.MapControl.Location location = value as Microsoft.Maps.MapControl.Location;
if (location != null)
{
Point point = Map.LocationToViewportPoint(location);
if (string.Compare(param.ToUpper(), "X") == 0)
return point.X;
else if (string.Compare(param.ToUpper(), "Y") == 0)
return point.Y;
return point;
}

return null;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}

最佳答案

2260 是 AG_E_PARSER_BAD_PARAMETER_VALUE

这是 Silverlight 的奇迹之一:完全没有有用的错误消息。

那么...这是我在 Silverlight 2 和 3 中遇到的问题,我很惊讶你在 4 中看到它。你 100% 是在 SL4 中吗?

将您的 PointConverter 更改为从 FrameworkElement 而不是 DependencyObject 继承。我希望这样做之后它会起作用。

如果这样做之后确实有效,我会感到困惑,因为 SL4 应该使用 DependencyObject 进行数据绑定(bind),而不是 SL3 和 SL2 使用的 FrameworkElement。

关于c# - ValueConverter 的依赖属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2543088/

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