Setting Caliburn IWindowmanager's Owner Property to Excel with handle HWND

陌路散爱 提交于 2019-12-11 07:13:51

问题


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:

  1. Add information to the created window so we can identify it later, we can do it like this:
    1. var settings = new ExpandoObject();
    2. settings.Tag = "THE_ONE"
    3. windowManager.ShowDialog(viewModel, settings: settings);
  2. 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");
  3. Now we wrap that with a WindowInteropHelper like this: var interopHelper = new WindowInteropHelper(ourWindow);
  4. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!