Dependency Property on ValueConverter
Using Silverlight 4, I'm trying to initialize a converter in the Resources section of my UserControl with a reference to one of the objects in my control. When I try to run the application I get this exception, note Line 16 in the actual XAML file is the co开发者_如何学JAVAnverter: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>
Point Converter Class:
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 is AG_E_PARSER_BAD_PARAMETER_VALUE
This is one of the wonders of Silverlight: the complete lack of useful error messages.
So... this is something I've hit in Silverlight 2 and 3, and I'm quite surprised you're seeing it in 4. Are you 100% you're in SL4?
Change your PointConverter to inherit from FrameworkElement instead of DependencyObject. I expect it would work after doing that.
If it does work after doing that, I'm confused since SL4 is supposed to use DependencyObject for data bindings instead of FrameworkElement which SL3 and SL2 used.
精彩评论