问题
I have an Excel Vsto addin application in which I host WPF application build using Calibrun Micro Autofac.I have a dialog popping up the excel and I want that Pop up window's Owner to be set to this excel window.Only way I see doing this is using WindowInteropHelper Class which needs Window instance.
And I am using settings like this :
dynamic settings = new ExpandoObject();
And I show window like this :
windowManager.ShowDialog(viewModel, settings: settings);
So What should I do to set the settings.Owner Property to this excel window(Whose Handle is known) so that the Pop up window is always on top of excel window??
回答1:
It looks like you are hosting a WPF application (add-in) inside Excel which is an Office application and Caliburn.Micro has a constructor in BootstrapperBase
class exactly for this situation, it looks like this: BootstrapperBase(useApplication = true)
, so you should derive your bootstrapper from BootstrapperBase
and pass in false
to the base constructor. something like this:
class MyBootstrapper : BootstrapperBase {
MyBootstrapper()
: base(false)
{
}
}
Then Caliburn.Micro will set the owner property correctly for you, you don't have to worry about it. Now if you knew about this but it didn't work for then comment on this and i will give you a solution specific to your situation.
Edit: To set the owner of the created window we need to set the Owner
property (which is of type Window
) but the problem is that you are working with a native win32 window so you only have a handle and WPF windows don't accepts handles as Owners, and the second problem is that we don't have a reference to the created window so we can wrap it inside a WindowInteropHelper
, in order to solve this i suggest the following:
- Add information to the created window so we can identify it later, we can do it like this:
var settings = new ExpandoObject();
settings.Tag = "THE_ONE"
windowManager.ShowDialog(viewModel, settings: settings);
- After doing so we need to get a reference to that window, so we can
do something like this:
var ourWindow = Application.Current.Windows.FirstOrDefault(w => w.Tag == "THE_ONE");
- Now we wrap that with a
WindowInteropHelper
like this:var interopHelper = new WindowInteropHelper(ourWindow);
- Now we can set owner to the native window handle like this:
interopHelper.Owner = (IntPtr) // PUT_YOUR_NATIVE_WINDOW_HANDLE_HERE;
That's all i can help you with, i hope it works.
来源:https://stackoverflow.com/questions/17519903/setting-caliburn-iwindowmanagers-owner-property-to-excel-with-handle-hwnd