3

I have a Silverlight application (edit: an Out-of-browser application with elevated trust) which must open a ChildWindow (for login purpose). When I (try to) call the event from the main (or any function called in the main), with the code below, nothing happens.

var credentials = new Credentials();
credentials.Closed += new EventHandler(Credentials_Closed);
credentials.Show();

I tried with a small test application and it was not working either. But then I tried to call the event from a Button_Click event... surprise, it works ! It also works when called by a DispatcherTimer.

I thought maybe it will works if I start it in another thread but it didn't solve the issue...

Your help will be very appreciated !

Philippe

Source : Why Won't the Silverlight ChildWindow Display?

For the test program : www.philippebellefeuille.com/StackOverflow/8074918/BugChildWindowInMain.rar

Community
  • 1
  • 1
Philippe
  • 986
  • 1
  • 6
  • 17

1 Answers1

4

Just tried your sample and it was both a UI threading issue and "something else" :)

The timer version works fine as-is (because the ShowDialog is deferred).

The newthread version fails because of threading. Got it working using my old favorite helper method (OnUiThread). Again it only worked because the ShowDialog is deferred:

protected delegate void OnUiThreadDelegate();

protected void OnUiThread(OnUiThreadDelegate onUiThreadDelegate)
{
    if (Deployment.Current.Dispatcher.CheckAccess())
    {
        onUiThreadDelegate();
    }
    else
    {
        Deployment.Current.Dispatcher.BeginInvoke(onUiThreadDelegate);
    }
}

with a call like this:

private void showdialog()
{
    this.OnUiThread(() =>
    {
        var credentials = new Credentials();
        credentials.Closed += new EventHandler(Credentials_Closed);
        credentials.Show();
    });
}

The direct call is interesting as it just fails, even if you use the showDialog above.

The cause is as follows...

App.xaml.cs does this:

this.RootVisual = new MainPage();

That means that the MainPage is not part of the visual tree until after the constructor returns! If the parent of a ChildWindow is not in the visual tree then a ChildWindow will not show.

Solution - wait until the page is loaded!

e.g.

public MainPage()
{
    InitializeComponent();
    this.Loaded += MainPage_Loaded;
}

void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    showdialog();
}
iCollect.it Ltd
  • 92,391
  • 25
  • 181
  • 202
  • Hello! Thank you for you answer. My application is already OOB and I required elevated trust. – Philippe Nov 10 '11 at 14:12
  • 1
    Is the security restriction really true for ChildWindows? I´m using Prism and Commands for showing ChildWindows and i have no problem showing them. Even after calling a DomainService operation and displaying a possible error message. I know the restriction for Dialogs. – Jehof Nov 10 '11 at 14:44
  • I just tried in a DispatcherTimer and the childwindow is showing correctly. It seems the problem is that I try to open it in the Main. – Philippe Nov 10 '11 at 14:57
  • @HiTech Magic: I already set background pictures, transitions and some text content in the main so I guess it is on the UI thread. I tried to start another thread in the main with a threadstart but then it throw me an error "Invalid cross-thread access." BTW: the program doesn't throw an exception when called from the main but nothing happens... – Philippe Nov 10 '11 at 15:19
  • 1
    You are welcome to send me code to test via my website (on user page). I am now even more curious why this is failing, but not enough detail to go on. – iCollect.it Ltd Nov 10 '11 at 15:20
  • I really appreciate your implication. You can download the test program I made here : www.philippebellefeuille.com/StackOverflow/8074918/BugChildWindowInMain.rar – Philippe Nov 10 '11 at 15:43
  • 1
    @Philippe: Sorted. quite obvious in hind-site. Thanks for providing a really handy sample. That always makes it easy to test. – iCollect.it Ltd Nov 10 '11 at 16:12
  • Thank you very much ! Very helpful and thoroughgoing explanation. I really appreciate! – Philippe Nov 10 '11 at 16:23