Reputation: 18160
I need to set up a template form for my .net core xaf project. As described here
So I am referencing a framework 4.8 project containing my template form. With the following code in my .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);
}
The error stack is
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
[Update]
Application is not null
Application.Model is null
From working code in a Framework example I thought I should be safe copying the framework code for winApplication.CreateCustomTemplate into my xaf .net core application.
However the null reference exception indicates that is different in .net core
[Update] I converted the framework example to .net6 and it works as it should. Now to find out why my project fails
In the working project winApplication.Setup() does not cause the CreateCustomTemplate event to fire. The good call stack causing the event to fire is
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#
In the bad project winApplication.Setup does cause the event to fire. The bad call stack is
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#
So it appears that in the bad program winApplication.Setup is causing the event to fire whereas in the good program winApplication.Start is causing the event to fire.
Now I need to try stepping into winApplication.Setup but I seem to be missing XafApplication.cs in the pdb files. I have asked about it here.
[Update] I think the difference may be here
It turns out that in the good call stack the following is true
if (RunSetupInNewThread && (SplashScreen == null || SplashScreen is DXSplashScreen))
Upvotes: 0
Views: 445
Reputation: 538
It's a duplicate of What is a NullReferenceException, and how do I fix it? and can be debugged easily as NullReferenceException occurs in your custom code (MyApp.Win.Program.<>c.b__0_0(Object sender, CreateCustomTemplateEventArgs e) in D:\dev\MyApp\MyApp.Win\Program.cs:line 93).
The correct solution is to follow the best practices from the DevExpress documentation for this particular scenario at Create a Custom Ribbon Template and add the following check e.Application.Model != null
. This is NOT specific to .NET 5, 6 or .NET Framework.
Upvotes: 1
Reputation: 18160
I was able to get winApplication.Setup to follow the happy path by deleting my SplashScreen.cs.
Upvotes: -1