更新
This commit is contained in:
@@ -1,10 +1,14 @@
|
||||
using System.IO;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Windows;
|
||||
using System.Windows.Interop;
|
||||
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
|
||||
using Microsoft.Win32;
|
||||
|
||||
using ShrlAlgoToolkit.Core.Assists;
|
||||
|
||||
namespace ShrlAlgoToolkit.RevitAddins.Assists;
|
||||
@@ -158,7 +162,7 @@ public static class WinDialogHelper
|
||||
public static void ShowModeless<T>(ObservableObject viewModel)
|
||||
where T : Window, new()
|
||||
{
|
||||
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomainOnAssemblyResolve;
|
||||
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
|
||||
var view = SingletonViewAssist<T>.GetInstance(out var isNewCreate);
|
||||
if (isNewCreate)
|
||||
{
|
||||
@@ -166,7 +170,7 @@ public static class WinDialogHelper
|
||||
view.ShowAhead();
|
||||
}
|
||||
view.Activate();
|
||||
AppDomain.CurrentDomain.AssemblyResolve -= CurrentDomainOnAssemblyResolve;
|
||||
AppDomain.CurrentDomain.AssemblyResolve -= CurrentDomain_AssemblyResolve;
|
||||
|
||||
//AssemblyLoaderHelpers loaderUtil = new(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
|
||||
//loaderUtil.HookAssemblyResolve();
|
||||
@@ -189,37 +193,44 @@ public static class WinDialogHelper
|
||||
// loaderUtil.UnhookAssemblyResolve();
|
||||
//}
|
||||
}
|
||||
private static Assembly CurrentDomainOnAssemblyResolve(object sender, ResolveEventArgs args)
|
||||
{
|
||||
if (!args.Name.Contains("Xaml.Behaviors"))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
//private static Assembly CurrentDomainOnAssemblyResolve(object sender, ResolveEventArgs args)
|
||||
//{
|
||||
// if (!args.Name.Contains("Xaml.Behaviors"))
|
||||
// {
|
||||
// return null;
|
||||
// }
|
||||
|
||||
var assemblyFile = Path.Combine(
|
||||
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
|
||||
"Microsoft.Xaml.Behaviors.dll"
|
||||
);
|
||||
return File.Exists(assemblyFile) ? Assembly.LoadFrom(assemblyFile) : null;
|
||||
}
|
||||
// var assemblyFile = Path.Combine(
|
||||
// Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
|
||||
// "Microsoft.Xaml.Behaviors.dll"
|
||||
// );
|
||||
// return File.Exists(assemblyFile) ? Assembly.LoadFrom(assemblyFile) : null;
|
||||
//}
|
||||
private static Dictionary<Type, Window> _windows = [];
|
||||
public static void ShowOrActivate<TWindow, TViewModel>(params object[] viewModelParams)
|
||||
where TWindow : Window, new()
|
||||
where TViewModel : class
|
||||
where TWindow : Window, new()
|
||||
where TViewModel : class
|
||||
{
|
||||
var windowType = typeof(TWindow);
|
||||
if (!_windows.ContainsKey(windowType) || _windows[windowType] == null)
|
||||
{
|
||||
//CloseAllWindowsExcept(windowType);
|
||||
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
|
||||
_windows[windowType] = new TWindow();
|
||||
_windows[windowType].Closed += WindowClosed;
|
||||
_windows[windowType].Closed += (sender, args) => _windows[windowType] = null;
|
||||
if (_windows[windowType].DataContext == null || !(_windows[windowType].DataContext is TViewModel))
|
||||
{
|
||||
_windows[windowType].DataContext = viewModelParams.Length == 0
|
||||
? Activator.CreateInstance(typeof(TViewModel))
|
||||
: Activator.CreateInstance(typeof(TViewModel), viewModelParams);
|
||||
if (viewModelParams.Length == 0)
|
||||
{
|
||||
_windows[windowType].DataContext = Activator.CreateInstance(typeof(TViewModel));
|
||||
}
|
||||
else
|
||||
{
|
||||
_windows[windowType].DataContext = Activator.CreateInstance(typeof(TViewModel), viewModelParams);
|
||||
}
|
||||
}
|
||||
_ = new WindowInteropHelper(_windows[windowType]) { Owner = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle };
|
||||
_ = new WindowInteropHelper(_windows[windowType]) { Owner = Process.GetCurrentProcess().MainWindowHandle };
|
||||
_windows[windowType].Show();
|
||||
}
|
||||
else
|
||||
@@ -227,4 +238,46 @@ public static class WinDialogHelper
|
||||
_windows[windowType].Activate();
|
||||
}
|
||||
}
|
||||
private static void WindowClosed(object sender, EventArgs e)
|
||||
{
|
||||
AppDomain.CurrentDomain.AssemblyResolve -= CurrentDomain_AssemblyResolve;
|
||||
}
|
||||
|
||||
|
||||
private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
|
||||
{
|
||||
StackFrame[] frames = new StackTrace().GetFrames();
|
||||
if (frames == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
string name = new AssemblyName(args.Name).Name;
|
||||
List<string> list = new List<string>();
|
||||
for (int i = 0; i < frames.Length; i++)
|
||||
{
|
||||
MethodBase method = frames[i].GetMethod();
|
||||
if ((object)method.DeclaringType == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
string directoryName = Path.GetDirectoryName(method.DeclaringType.Assembly.Location);
|
||||
if (list.Contains(directoryName))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
list.Add(directoryName);
|
||||
foreach (string item in Directory.EnumerateFiles(directoryName, "*.dll"))
|
||||
{
|
||||
if (name == Path.GetFileNameWithoutExtension(item))
|
||||
{
|
||||
return Assembly.LoadFile(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user