0

I am developing an app with WPF.

In my case, the users have 2 monitors. When he opens the app in the secondary screen, it will load data for nearly 5 seconds. During this period, he may turn to the primary screen for personal stuff, like visit Facebook or visit twitter.

After the data are loaded, a dialogbox should be prompted. What bothers me is that it often shows in the primary screen where he deals with personal stuff, not the secondary screen where he opens the app. The dialog window is supposed to show on the top of the app.

I am thinking it's because that the app is not active when the data are loaded. Do you guys have any idea?

I know that the MessageBox.Show() has a "owner" parameter can fix this. How can I automatically get the correct owner? I am using a PRISM pattern so that it's not easy for me to find the window as the owner.

Code:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private async void Button_Click(object sender, RoutedEventArgs e)
    {
        await Task.Delay(5000);
        MessageBox.Show("Loaded!");
    }
}
Kanjie Lu
  • 997
  • 2
  • 9
  • 26

2 Answers2

0

Yes the owner parameter should fix that. Regarding Prism, there are different approaches. The easiest for me was to use the Application.Current.MainWindow which refers to the main window of the application.

MessageBox.Show(Application.Current.MainWindow, "bla bla");

You could as well try to resolve your Shell window via your IoC container and take this as owner.

Martin
  • 818
  • 9
  • 20
  • Thank you. For this specific case it can work. But I am looking for a general way to show the messagebox. In Prism, the mainwindow is usually Shell. If there exists a popup window other than the main window and I want the dialog box shown on that window, I can't use MainWindow as the parameter. – Kanjie Lu May 28 '15 at 09:44
0

You could try to use MessageBox from Extended WPF Toolkit Community Edition (NuGet Package Extended.Wpf.Toolkit):

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <xctk:WindowContainer>
            <xctk:ChildWindow Height="100" Width="250" Left="10" Top="10" Name="chWindow">
                <TextBlock Text="Hello World ..." />
            </xctk:ChildWindow>
            <xctk:MessageBox Height="100" Width="250" Left="10" Top="100" Name="msgBox">                
            </xctk:MessageBox>
        </xctk:WindowContainer>
        <Button Name="btnTst" Click="btnTst_Click" Content="Test" Width="65" Height="30" HorizontalAlignment="Left" VerticalAlignment="Bottom" />
    </Grid>
</Window>

And:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private async void btnTst_Click(object sender, RoutedEventArgs e)
    {
        await Task.Delay(5000);
        //this.chWindow.Show();
        this.msgBox.ShowMessageBox();
    }
}

MessageBox is inside Window…

Jofta
  • 94
  • 1
  • 1
  • 4