我需要为我的.net核心xaf项目设置一个模板表单。如所述,这里
因此,我引用了一个框架4.8项目,其中包含了我的模板表单。在我的.net6 program.cs中使用以下代码
try
{
winApplication.CreateCustomTemplate += delegate (object sender, CreateCustomTemplateEventArgs e)
{
// fails here
bool isRibbon = ((IModelOptionsWin)e.Application.Model.Options).FormStyle == RibbonFormStyle.Ribbon;
if (isRibbon && e.Context == TemplateContext.View)
{
e.Template = new DetailRibbonForm2(); // the form is in the framework project
}
};
winApplication.Setup();
winApplication.Start();
}
catch (Exception e)
{
MessageBox.Show($"Error. inner: {e.InnerException} e:{e.ToString()}");
winApplication.StopSplash();
winApplication.HandleException(e);
}
错误堆栈是
System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=MyApp.Win
StackTrace:
at MyApp.Win.Program.<>c.<Main>b__0_0(Object sender, CreateCustomTemplateEventArgs e) in D:\dev\MyApp\MyApp.Win\Program.cs:line 93
at DevExpress.ExpressApp.XafApplication.RaiseCreateCustomTemplate(CreateCustomTemplateEventArgs args)
at DevExpress.ExpressApp.Win.WinApplication.PreCreateCustomTemplate(TemplateContext context)
at DevExpress.ExpressApp.Win.WinApplication.InitializeFrameTemplateCache()
at DevExpress.ExpressApp.Win.WinApplication.SetupCore(String applicationName, IList`1 objectSpaceProviders, ApplicationModulesManager modulesManager, ISecurityStrategyBase security)
at DevExpress.ExpressApp.XafApplication.Setup(String applicationName, IList`1 objectSpaceProviders, ApplicationModulesManager modulesManager, ISecurityStrategyBase security)
at DevExpress.ExpressApp.XafApplication.Setup(String applicationName, IList`1 objectSpaceProviders, String[] moduleAssemblies, ISecurityStrategyBase security)
at DevExpress.ExpressApp.XafApplication.Setup(String applicationName, String connectionString, String[] moduleAssemblies, ISecurityStrategyBase security)
at DevExpress.ExpressApp.XafApplication.Setup()
at MyApp.Win.Program.Main() in D:\dev\MyApp\MyApp.Win\Program.cs:line 101
This exception was originally thrown at this call stack:
MyApp.Win.Program.Main.AnonymousMethod__0_0(object, DevExpress.ExpressApp.CreateCustomTemplateEventArgs) in Program.cs
DevExpress.ExpressApp.XafApplication.RaiseCreateCustomTemplate(DevExpress.ExpressApp.CreateCustomTemplateEventArgs)
DevExpress.ExpressApp.Win.WinApplication.PreCreateCustomTemplate(DevExpress.ExpressApp.TemplateContext)
DevExpress.ExpressApp.Win.WinApplication.InitializeFrameTemplateCache()
DevExpress.ExpressApp.Win.WinApplication.SetupCore(string, System.Collections.Generic.IList<DevExpress.ExpressApp.IObjectSpaceProvider>, DevExpress.ExpressApp.ApplicationModulesManager, DevExpress.ExpressApp.Security.ISecurityStrategyBase)
DevExpress.ExpressApp.XafApplication.Setup(string, System.Collections.Generic.IList<DevExpress.ExpressApp.IObjectSpaceProvider>, DevExpress.ExpressApp.ApplicationModulesManager, DevExpress.ExpressApp.Security.ISecurityStrategyBase)
DevExpress.ExpressApp.XafApplication.Setup(string, System.Collections.Generic.IList<DevExpress.ExpressApp.IObjectSpaceProvider>, string[], DevExpress.ExpressApp.Security.ISecurityStrategyBase)
DevExpress.ExpressApp.XafApplication.Setup(string, string, string[], DevExpress.ExpressApp.Security.ISecurityStrategyBase)
DevExpress.ExpressApp.XafApplication.Setup()
MyApp.Win.Program.Main() in Program.cs
更新
应用程序不为空。
Application.Model为空
从框架实例中的工作代码来看,我认为应该安全地将winApplication.CreateCustomTemplate的框架代码复制到xaf .net核心应用程序中。
然而,空引用异常表明在.net内核中是不同的。
Update I将框架示例转换为.net6,并按其应有的方式工作。现在来找出我的项目失败的原因
在工作项目中,winApplication.Setup()不会导致触发CreateCustomTemplate事件。导致事件触发的好调用堆栈是
CustomizeRibbonTemplateExample.Win.dll!CustomizeRibbonTemplateExample.Win.Program.Main.AnonymousMethod__0_0(object sender, DevExpress.ExpressApp.CreateCustomTemplateEventArgs e) Line 38 C#
DevExpress.ExpressApp.v21.2.dll!DevExpress.ExpressApp.XafApplication.RaiseCreateCustomTemplate(DevExpress.ExpressApp.CreateCustomTemplateEventArgs args) Unknown
DevExpress.ExpressApp.v21.2.dll!DevExpress.ExpressApp.XafApplication.OnCreateCustomTemplate(string name) Unknown
DevExpress.ExpressApp.v21.2.dll!DevExpress.ExpressApp.XafApplication.CreateTemplate(string templateContextName) Unknown
DevExpress.ExpressApp.v21.2.dll!DevExpress.ExpressApp.Frame.CreateTemplate() Unknown
DevExpress.ExpressApp.Win.v21.2.dll!DevExpress.ExpressApp.Win.WinWindow.WinWindow(DevExpress.ExpressApp.XafApplication application, DevExpress.ExpressApp.TemplateContext context, System.Collections.Generic.ICollection<DevExpress.ExpressApp.Controller> controllers, bool isMain, bool activateControllersImmediately) Unknown
DevExpress.ExpressApp.Win.v21.2.dll!DevExpress.ExpressApp.Win.WinApplication.CreateWindowCore(DevExpress.ExpressApp.TemplateContext context, System.Collections.Generic.ICollection<DevExpress.ExpressApp.Controller> controllers, bool isMain, bool activateControllersImmediately) Unknown
DevExpress.ExpressApp.v21.2.dll!DevExpress.ExpressApp.XafApplication.CreateWindow(DevExpress.ExpressApp.TemplateContext context, System.Collections.Generic.ICollection<DevExpress.ExpressApp.Controller> controllers, bool createAllControllers, bool isMain, DevExpress.ExpressApp.View view) Unknown
DevExpress.ExpressApp.Win.v21.2.dll!DevExpress.ExpressApp.Win.WinShowViewStrategyBase.CreateWindow(DevExpress.ExpressApp.ShowViewParameters parameters, DevExpress.ExpressApp.ShowViewSource showViewSource, bool isMain) Unknown
DevExpress.ExpressApp.Win.v21.2.dll!DevExpress.ExpressApp.Win.WinShowViewStrategyBase.CreateExplorerWindow(DevExpress.ExpressApp.ShowViewParameters parameters) Unknown
DevExpress.ExpressApp.Win.v21.2.dll!DevExpress.ExpressApp.Win.WinShowViewStrategyBase.ShowStartupWindowCore() Unknown
DevExpress.ExpressApp.Win.v21.2.dll!DevExpress.ExpressApp.Win.MdiShowViewStrategy.ShowStartupWindowCore() Unknown
DevExpress.ExpressApp.Win.v21.2.dll!DevExpress.ExpressApp.Win.WinShowViewStrategyBase.ShowStartupWindow() Unknown
DevExpress.ExpressApp.Win.v21.2.dll!DevExpress.ExpressApp.Win.WinApplication.ShowStartupWindow() Unknown
DevExpress.ExpressApp.Win.v21.2.dll!DevExpress.ExpressApp.Win.WinApplication.DoStartupLogic(DevExpress.XtraSplashScreen.IOverlaySplashScreenHandle overlayFormHandle) Unknown
DevExpress.ExpressApp.Win.v21.2.dll!DevExpress.ExpressApp.Win.WinApplication.Start() Unknown
CustomizeRibbonTemplateExample.Win.dll!CustomizeRibbonTemplateExample.Win.Program.Main() Line 46 C#
在糟糕的项目中,winApplication.Setup确实会导致事件触发。坏调用堆栈是
MyApp.Win.dll!MyApp.Win.Program.Main.AnonymousMethod__0_0(object sender, DevExpress.ExpressApp.CreateCustomTemplateEventArgs e) Line 92 C#
DevExpress.ExpressApp.v21.2.dll!DevExpress.ExpressApp.XafApplication.RaiseCreateCustomTemplate(DevExpress.ExpressApp.CreateCustomTemplateEventArgs args) Unknown
DevExpress.ExpressApp.Win.v21.2.dll!DevExpress.ExpressApp.Win.WinApplication.PreCreateCustomTemplate(DevExpress.ExpressApp.TemplateContext context) Unknown
DevExpress.ExpressApp.Win.v21.2.dll!DevExpress.ExpressApp.Win.WinApplication.InitializeFrameTemplateCache() Unknown
DevExpress.ExpressApp.Win.v21.2.dll!DevExpress.ExpressApp.Win.WinApplication.SetupCore(string applicationName, System.Collections.Generic.IList<DevExpress.ExpressApp.IObjectSpaceProvider> objectSpaceProviders, DevExpress.ExpressApp.ApplicationModulesManager modulesManager, DevExpress.ExpressApp.Security.ISecurityStrategyBase security) Unknown
DevExpress.ExpressApp.v21.2.dll!DevExpress.ExpressApp.XafApplication.Setup(string applicationName, System.Collections.Generic.IList<DevExpress.ExpressApp.IObjectSpaceProvider> objectSpaceProviders, DevExpress.ExpressApp.ApplicationModulesManager modulesManager, DevExpress.ExpressApp.Security.ISecurityStrategyBase security) Unknown
DevExpress.ExpressApp.v21.2.dll!DevExpress.ExpressApp.XafApplication.Setup(string applicationName, System.Collections.Generic.IList<DevExpress.ExpressApp.IObjectSpaceProvider> objectSpaceProviders, string[] moduleAssemblies, DevExpress.ExpressApp.Security.ISecurityStrategyBase security) Unknown
DevExpress.ExpressApp.v21.2.dll!DevExpress.ExpressApp.XafApplication.Setup(string applicationName, string connectionString, string[] moduleAssemblies, DevExpress.ExpressApp.Security.ISecurityStrategyBase security) Unknown
DevExpress.ExpressApp.v21.2.dll!DevExpress.ExpressApp.XafApplication.Setup() Unknown
MyApp.Win.dll!MyApp.Win.Program.Main() Line 101 C#
因此,在坏程序中,winApplication.Setup会导致事件触发,而在好程序中,winApplication.Start会导致事件触发。
现在我需要尝试进入winApplication.Setup,但是我似乎在pdb文件中缺少了XafApplication.cs。我已经问过了,这里。
最新消息我想区别可能就在这里
结果表明,在好调用堆栈中,以下内容是正确的
if (RunSetupInNewThread && (SplashScreen == null || SplashScreen is DXSplashScreen))
发布于 2022-03-16 22:07:19
它是什么是NullReferenceException,我如何修复它?的副本,可以很容易地调试,因为NullReferenceException出现在您的自定义代码( D:\dev\MyApp\MyApp.Win\Program.cs:line 93中的MyApp.Win.Program.<>c.b___0(对象发送方,CreateCustomTemplateEventArgs e)中)。
正确的解决方案是在DevExpress上遵循创建自定义条带模板文档中针对这个特定场景的最佳实践,并添加以下检查e.Application.Model != null
。这并不是.NET 5、6或.NET框架所特有的。
https://stackoverflow.com/questions/71478069
复制相似问题