调整代码
This commit is contained in:
64
ShrlAlgoToolkit.RevitCore/Base/BaseApplication.cs
Normal file
64
ShrlAlgoToolkit.RevitCore/Base/BaseApplication.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
|
||||
using ShrlAlgoToolkit.RevitCore.Base;
|
||||
|
||||
namespace ShrlAlgoToolkit.RevitCore.Assists
|
||||
{
|
||||
public abstract class BaseApplication : IExternalApplication
|
||||
{
|
||||
public UIControlledApplication UiApplication { get; private set; }
|
||||
|
||||
public Result OnStartup(UIControlledApplication application)
|
||||
{
|
||||
UiApplication = application;
|
||||
|
||||
// 1. 注册程序集依赖解析事件 (解决第三方库找不到的问题)
|
||||
AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve;
|
||||
|
||||
// 2. 初始化全局异步外部事件处理器 (必须在主线程创建)
|
||||
ExternalEventHelper.Initialize();
|
||||
|
||||
try
|
||||
{
|
||||
OnApplicationStartup();
|
||||
return Result.Succeeded;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
TaskDialog.Show("Startup Error", ex.ToString());
|
||||
return Result.Failed;
|
||||
}
|
||||
}
|
||||
|
||||
public Result OnShutdown(UIControlledApplication application)
|
||||
{
|
||||
try
|
||||
{
|
||||
OnApplicationShutdown();
|
||||
return Result.Succeeded;
|
||||
}
|
||||
finally
|
||||
{
|
||||
AppDomain.CurrentDomain.AssemblyResolve -= OnAssemblyResolve;
|
||||
}
|
||||
}
|
||||
|
||||
// 供子类重写的方法
|
||||
public abstract void OnApplicationStartup();
|
||||
public virtual void OnApplicationShutdown() { }
|
||||
|
||||
// 依赖自动解析逻辑
|
||||
private Assembly OnAssemblyResolve(object sender, ResolveEventArgs args)
|
||||
{
|
||||
var assemblyName = new AssemblyName(args.Name).Name;
|
||||
var pluginDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
|
||||
var assemblyPath = Path.Combine(pluginDirectory, $"{assemblyName}.dll");
|
||||
|
||||
return File.Exists(assemblyPath) ? Assembly.LoadFrom(assemblyPath) : null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user