ValueConverter的依赖属性

时间:2010-03-30 05:45:49

标签: c# silverlight data-binding xaml

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

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();
    }
}

1 个答案:

答案 0 :(得分:2)

2260是AG_E_PARSER_BAD_PARAMETER_VALUE

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

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

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

如果它在执行此操作后确实有效,我很困惑,因为SL4应该使用DependencyObject进行数据绑定,而不是SL3和SL2使用的FrameworkElement。