WPF window 无法从 Revit ExternalCommand 初始化
WPF window fails to initialize from Revit ExternalCommand
我正在制作一个 Revit 插件,它将打开 WPF window 以与用户交互。我遵循了 SDK 中的无模式对话框示例。我按照 MVVM 模式制作了我的程序。但是,调试程序一直在 ExternalCommand 步骤抛出异常:"object reference not set to an instance of an object" ExternalCommand class 如下:
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
class RevitCommand : IExternalCommand
{
public virtual Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
try
{
ThisApplication.thisApp.ShowWindow(commandData.Application);
return Result.Succeeded;
}
catch (Exception ex)
{
message = ex.Message;
return Result.Failed;
}
}
}
而外部应用程序 class 是:
public class ThisApplication : IExternalApplication
{
//Class instance
internal static ThisApplication thisApp;
//Modeless instance
private MainWindow m_MainWindow;
public Result OnShutdown(UIControlledApplication application)
{
if (m_MainWindow != null && m_MainWindow.IsVisible)
{
m_MainWindow.Dispose();
}
return Result.Succeeded;
}
public Result OnStartup(UIControlledApplication application)
{
m_MainWindow = null; // no dialog needed yet; the command will bring it
thisApp = this; // static access to this application instance
return Result.Succeeded;
}
public void ShowWindow(UIApplication uiapp)
{
// If we do not have a dialog yet, create and show it
if (m_MainWindow == null )
{
RequestHandler handler = new RequestHandler();
ExternalEvent exEvent = ExternalEvent.Create(handler);
MyViewModel vmod = new MyViewModel(exEvent,handler);
m_MainWindow = new MainWindow();
m_MainWindow.DataContext = vmod;
m_MainWindow.Show();
}
}
}
我怀疑是因为 thisApp
为空而引发了异常,但 SDK 中的示例工作正常。唯一的区别是他们使用 WinForm 而不是 WPF,并且将 ExternalEvent 传递到视图而不是视图模型。
事实证明我的怀疑是正确的,thisApp
为 null 确实使 Revit 抛出异常,为了修复它我启动了一个新的 thisApplication
class 实例然后调用 ShowWindow
方法。它达到了我的预期。
public virtual Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
try
{
new ThisApplication().ShowWindow(commandData.Application);
return Result.Succeeded;
}
catch (Exception ex)
{
message = ex.Message;
return Result.Failed;
}
}
谢谢@Dai 的建议
我正在制作一个 Revit 插件,它将打开 WPF window 以与用户交互。我遵循了 SDK 中的无模式对话框示例。我按照 MVVM 模式制作了我的程序。但是,调试程序一直在 ExternalCommand 步骤抛出异常:"object reference not set to an instance of an object" ExternalCommand class 如下:
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
class RevitCommand : IExternalCommand
{
public virtual Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
try
{
ThisApplication.thisApp.ShowWindow(commandData.Application);
return Result.Succeeded;
}
catch (Exception ex)
{
message = ex.Message;
return Result.Failed;
}
}
}
而外部应用程序 class 是:
public class ThisApplication : IExternalApplication
{
//Class instance
internal static ThisApplication thisApp;
//Modeless instance
private MainWindow m_MainWindow;
public Result OnShutdown(UIControlledApplication application)
{
if (m_MainWindow != null && m_MainWindow.IsVisible)
{
m_MainWindow.Dispose();
}
return Result.Succeeded;
}
public Result OnStartup(UIControlledApplication application)
{
m_MainWindow = null; // no dialog needed yet; the command will bring it
thisApp = this; // static access to this application instance
return Result.Succeeded;
}
public void ShowWindow(UIApplication uiapp)
{
// If we do not have a dialog yet, create and show it
if (m_MainWindow == null )
{
RequestHandler handler = new RequestHandler();
ExternalEvent exEvent = ExternalEvent.Create(handler);
MyViewModel vmod = new MyViewModel(exEvent,handler);
m_MainWindow = new MainWindow();
m_MainWindow.DataContext = vmod;
m_MainWindow.Show();
}
}
}
我怀疑是因为 thisApp
为空而引发了异常,但 SDK 中的示例工作正常。唯一的区别是他们使用 WinForm 而不是 WPF,并且将 ExternalEvent 传递到视图而不是视图模型。
事实证明我的怀疑是正确的,thisApp
为 null 确实使 Revit 抛出异常,为了修复它我启动了一个新的 thisApplication
class 实例然后调用 ShowWindow
方法。它达到了我的预期。
public virtual Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
try
{
new ThisApplication().ShowWindow(commandData.Application);
return Result.Succeeded;
}
catch (Exception ex)
{
message = ex.Message;
return Result.Failed;
}
}
谢谢@Dai 的建议