XAFApplication 模型 属性 意外未设置。尝试为 .net core6 (XAF) 设置自定义模板

XAFApplication model property unexpectedly not set. Trying to set custom template for .net core6 (XAF)

我需要为我的 .net 核心 xaf 项目设置模板表单。如上所述here

所以我引用了一个包含我的模板表单的框架 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 为空

Framework example 中的工作代码我认为我应该安全地将 winApplication.CreateCustomTemplate 的框架代码复制到我的 xaf .net 核心应用程序中。

然而,空引用异常表明在 .net 核心中是不同的

[更新] 我将框架示例转换为 .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。我问过 here.

[更新] 我觉得区别可能就在这里

事实证明,在良好的调用堆栈中,以下内容是正确的

if (RunSetupInNewThread && (SplashScreen == null || SplashScreen is DXSplashScreen))

通过删除我的 SplashScreen.cs,我能够让 winApplication.Setup 走上幸福的道路。

它是 What is a NullReferenceException, and how do I fix it? 的副本,可以轻松调试,因为您的自定义代码中会出现 NullReferenceException (MyApp.Win.Program.<>c.b__0_0(Object sender , CreateCustomTemplateEventArgs e) 在 D:\dev\MyApp\MyApp.Win\Program.cs:line 93).

正确的解决方案是遵循 Create a Custom Ribbon Template 上针对此特定场景的 DevExpress 文档中的最佳实践,并添加以下检查 e.Application.Model != null。这并非特定于 .NET 5、6 或 .NET Framework。