c# – 使用StaticResource作为MarkupExtension的参数会导致WPF中出现NullReferenceException
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 使用StaticResource作为MarkupExtension的参数会导致WPF中出现NullReferenceException,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含7505字,纯文字阅读大概需要11分钟。
内容图文
![c# – 使用StaticResource作为MarkupExtension的参数会导致WPF中出现NullReferenceException](/upload/InfoBanner/zyjiaocheng/785/ef1e63f08310432f80528be6fe79140f.jpg)
即使在将问题简化为最小代码之后,我仍然无法理解什么会导致NullReferenceException.
创建新的WPF应用程序“MarkupExtParam”.替换代码:
MainWindow.xaml:
<Window x:Class="MarkupExtParam.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:MarkupExtParam"
Content="{Binding}">
<Control.DataContext>
<my:Foo/>
</Control.DataContext>
<Control.Resources>
<my:Foo x:Key="foo"/>
<DataTemplate DataType="{x:Type my:Foo}">
<TextBlock Text="{my:Ext {StaticResource foo}}"/>
</DataTemplate>
</Control.Resources>
</Window>
MainWindow.xaml.cs:
using System;
using System.Windows.Markup;
namespace MarkupExtParam
{
public partial class MainWindow
{
public MainWindow () { InitializeComponent(); }
}
[MarkupExtensionReturnType (typeof(string))]
public class ExtExtension : MarkupExtension
{
[ConstructorArgument ("foo")]
public Foo Foo { get; set; }
public ExtExtension (Foo foo) { Foo = foo; }
public override object ProvideValue (IServiceProvider provider)
{
return Foo.ToString();
}
}
public class Foo { }
}
当我运行该应用程序时,抛出以下异常:
System.NullReferenceException was unhandled
Message=Object reference not set to an instance of an object.
Source=System.Xaml
StackTrace:
at MS.Internal.Xaml.Runtime.ClrObjectRuntime.GetConverterInstance[TConverterBase](XamlValueConverter`1 converter)
at MS.Internal.Xaml.Runtime.ClrObjectRuntime.CreateObjectWithTypeConverter(ServiceProviderContext serviceContext, XamlValueConverter`1 ts, Object value)
at MS.Internal.Xaml.Runtime.ClrObjectRuntime.CreateFromValue(ServiceProviderContext serviceContext, XamlValueConverter`1 ts, Object value, XamlMember property)
at System.Xaml.XamlObjectWriter.Logic_CreateFromValue(ObjectWriterContext ctx, XamlValueConverter`1 typeConverter, Object value, XamlMember property, String targetName, IAddLineInfo lineInfo)
at System.Xaml.XamlObjectWriter.Logic_ConvertPositionalParamsToArgs(ObjectWriterContext ctx)
at System.Xaml.XamlObjectWriter.WriteEndMember()
at System.Xaml.XamlWriter.WriteNode(XamlReader reader)
at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlReader templateReader, XamlObjectWriter currentWriter)
at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlObjectWriter objectWriter)
at System.Windows.FrameworkTemplate.LoadOptimizedTemplateContent(DependencyObject container, IComponentConnector componentConnector, IStyleConnector styleConnector, List`1 affectedChildren, UncommonField`1 templatedNonFeChildrenField)
at System.Windows.FrameworkTemplate.LoadContent(DependencyObject container, List`1 affectedChildren)
at System.Windows.StyleHelper.ApplyTemplateContent(UncommonField`1 dataField, DependencyObject container, FrameworkElementFactory templateRoot, Int32 lastChildIndex, HybridDictionary childIndexFromChildID, FrameworkTemplate frameworkTemplate)
at System.Windows.FrameworkTemplate.ApplyTemplateContent(UncommonField`1 templateDataField, FrameworkElement container)
at System.Windows.FrameworkElement.ApplyTemplate()
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at System.Windows.ContextLayoutManager.UpdateLayout()
at System.Windows.UIElement.UpdateLayout()
at System.Windows.Interop.HwndSource.SetLayoutSize()
at System.Windows.Interop.HwndSource.set_RootVisualInternal(Visual value)
at System.Windows.Interop.HwndSource.set_RootVisual(Visual value)
at System.Windows.Window.SetRootVisual()
at System.Windows.Window.SetRootVisualAndUpdateSTC()
at System.Windows.Window.SetupInitialState(Double requestedTop, Double requestedLeft, Double requestedWidth, Double requestedHeight)
at System.Windows.Window.CreateSourceWindow(Boolean duringShow)
at System.Windows.Window.CreateSourceWindowDuringShow()
at System.Windows.Window.SafeCreateWindowDuringShow()
at System.Windows.Window.ShowHelper(Object booleanBox)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.DispatcherOperation.InvokeImpl()
at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Windows.Threading.DispatcherOperation.Invoke()
at System.Windows.Threading.Dispatcher.ProcessQueue()
at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.Run()
at System.Windows.Application.RunDispatcher(Object ignore)
at System.Windows.Application.RunInternal(Window window)
at System.Windows.Application.Run(Window window)
at System.Windows.Application.Run()
at MarkupExtParam.App.Main() in d:\Docs\Projects\_Bugs\SubclassingBinding\obj\Debug\App.g.cs:line 0
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
如果我不使用DataTemplate并将< TextBlock Text =“{my:Ext {StaticResource foo}}”/>外面,一切正常.
我已经尝试使用.NET Refelector生成的PDB进行调试(公共源代码似乎不包含任何内部类),但是没有成功,因为Reflector无法为大量优化的程序集生成正确的PDB,因此断点不起作用,大多数变量我知道XAML解析器试图将StaticResourceHolder(StaticResourceExtension的子类)分配给Foo并失败(虽然无法理解它为什么会发生).
问题:如何解决问题?也许我只是做错了什么?
解决方法:
通过将以下属性添加到Foo类来解决问题:
[TypeConverter(typeof(TypeConverter))]
在这种情况下,显然指定类型转换器是必需的.如果您需要为类提供不同类型的转换器,请不要忘记调用基本方法(Convert *,CanConvert *).将TypeConverter属性应用于构造函数参数的方法似乎不存在.
内容总结
以上是互联网集市为您收集整理的c# – 使用StaticResource作为MarkupExtension的参数会导致WPF中出现NullReferenceException全部内容,希望文章能够帮你解决c# – 使用StaticResource作为MarkupExtension的参数会导致WPF中出现NullReferenceException所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。