开发者

Window.Show() display new window and then disappear behind main window

I am developing a Prism application where I need to publish view-models in a new window. To achieve that I created a service dedicated to the publication of these view-models. I call it like this:

windowService.Publish(myViewModel);

And the body of the Publish method is like this:

public void Publish(FloatingViewModel viewModel)
{
    var floatingWindow = ServiceLocator.Current.GetInstance<FloatingWindow>();

    floatingWindow.DataContext = viewModel;
    floatingWindow.Show();
}

After the Show开发者_JS百科() method execution, the new window quickly shows up above my main window and then disappears behind my main window like if it took back the focus.

Does anybody knows where this behavior could come from?


I finally found the problem and it was actually way deeper than I thought. This answer will not be relevant of the described problem as the root cause was totally different.

The code that was publishing the view-model in a window was actually nested in a WPF command and this command was attached to the MouseDoubleClick event through a InvokeCommandAction. I discovered that even when you bind an event to a command, the event is not marked as handled and therefore will propagate until it is handled. That was causing my main window to take back the focus.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜