This is a migrated thread and some comments may be shown as answers.

WPFWindow ShowDialog not working the second time?

6 Answers 2032 Views
Window
This is a migrated thread and some comments may be shown as answers.
yonadav
Top achievements
Rank 1
yonadav asked on 14 Feb 2010, 06:21 PM
Hi,

We've got the following code in the application's App class's constructor (the fact that we're doing this in the App class appears to be related to the issue):

            bool allGood = false
            do 
            { 
                LoginWindow loginWindow = new LoginWindow(); 
                loginWindow.ShowDialog(); 
                 
                if (loginWindow.DialogResult.GetValueOrDefault(false)) 
                { 
                    try 
                    { 
                        // Try and do something... 
                        allGood = true
                    } 
                    catch (Exception) 
                    { 
                        lastLoginFailed = true
                    } 
                } 
                else 
                    return
            } while (!allGood); 

In the LoginWindow, there's a button whose Click handler ends with a call to Close(). The "Try and do something" code tries something that may possibly throw an Exception. If it does, then allGood stays false and the loop happens again. If it doesn't throw an exception, allGood is set to true and the job ends.

The problem is this - the first call to ShowDialog works fine. The second call does nothing.

Any idea what we're doing wrong here?

Thanks,
yonadav

6 Answers, 1 is accepted

Sort by
0
Miroslav Nedyalkov
Telerik team
answered on 17 Feb 2010, 01:56 PM
Hello Yonadav,

 I tried the following:

public App()
{
    this.InitializeComponent();
    bool allGood = false;
    do
    {
        var loginWindow = new RadWindow();
        loginWindow.ShowDialog();
 
        if (loginWindow.DialogResult.GetValueOrDefault(true))
        {
            try
            {
                // Try and do something...
                allGood = false;
            }
            catch (Exception)
            {
            }
        }
        else
            return;
    } while (!allGood);
}

And it works just fine. First time didn't put IniitializeComponents() and it was working as you said. After that I tried using WPF Window instead of RadWindow and the same thing happend. What I suspect is that if you show another window before setting a MainWindow, when this window is closed the application ends.

What I could suggest you is to either use this code (call InitializeComponents before showing your login) or to move this code in the constructor of your main window.

Hope this helps.

Kind regards,
Miroslav Nedyalkov
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
yonadav
Top achievements
Rank 1
answered on 24 Feb 2010, 02:40 PM
Hi Miroslav,

You are correct, it appears that the first WPF window created registers as the main window. When it closes, the application shuts down. It took us a while to figure out, though, that the RadWindow does not create a WPF Window until it's actually shown (hence it doesn't register as the main window either...). Maybe this behavior is incorrect?

Thanks for the help,
yonadav
0
Miroslav Nedyalkov
Telerik team
answered on 25 Feb 2010, 11:50 AM
Hello Yonadav,

 The RadWindow control is just not meant to be used that way - you need to have a MainWindow before using the RadWindow control.

Sincerely yours,
Miroslav Nedyalkov
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Garry Clark
Top achievements
Rank 1
answered on 26 Mar 2010, 01:57 PM
Miroslav,
I am having the same issue, but different scenario. In my MVVM Prism app I have a button that is loaded into a ToolbarRegion on the Shell. On the view I have an ICommand that closes the RadWindow. Everything works fine the first time, but after the window is closed it can never be opened again. When RadWindow closes, my module is still registered with the IoC and any button clicks to reopen RadWindow should work, but that appears not to be the case. Also RadWindow is being called as a Showdialog and is not the MainWindow in this case as the Shell is.

Any ideas what could cause this behavoir?

View
<telerikNavigation:RadWindow x:Class="EliteExtender.DatabaseSynchronization.Views.SynchronizationView" 
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"   
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:telerikNavigation="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Navigation" 
             xmlns:telerikInput="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Input" 
             xmlns:telerik="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls" 
             mc:Ignorable="d"   
             d:DesignHeight="218" d:DesignWidth="479" 
             telerik:StyleManager.Theme="{Binding Source={StaticResource settings}, Path=Default.Theme}" 
             Header="Synchronizing local database cache..." Height="254" Width="548" CanClose="False" ResizeMode="NoResize">  
    <Grid> 
        <telerik:RadButton Command="{Binding CloseCommand}" Height="30" Width="60"   
                           telerik:StyleManager.Theme="{Binding Source={StaticResource settings}, Path=Default.Theme}">  
            <telerik:RadButton.Content> 
                <Label Content="Close"/>  
            </telerik:RadButton.Content> 
        </telerik:RadButton> 
    </Grid> 
    </telerikNavigation:RadWindow> 
 


View Model
using EliteExtender.Composite.Extensions;  
using EliteExtender.DatabaseSynchronization.Views;  
using EliteExtender.Infrastructure;  
using System;  
using System.Windows;  
using System.Windows.Input;  
using Telerik.Windows.Controls;  
 
namespace EliteExtender.DatabaseSynchronization.ViewModels  
{  
    internal class SynchronizationViewModel : PresentationModel<ISynchronizationView>  
    {  
        private readonly IMessageService messageService;  
        private readonly IApplicationContainer container;  
        private RadWindow window;  
        private readonly DelegateCommand closeCommand;         
 
        public SynchronizationViewModel(IApplicationContainer container, ISynchronizationView view, IMessageService messageService)  
            : base(view)  
        {  
            this.container = container;  
            this.messageService = messageService;  
            this.window = (RadWindow)view;  
            this.window.WindowStartupLocation = WindowStartupLocation.CenterScreen;  
            this.window.Closed += new EventHandler<WindowClosedEventArgs>(RadWindow_Closed);  
            this.window.Opened += new RoutedEventHandler(RadWindow_Opened);  
              
            closeCommand = new DelegateCommand(Close, CanCloseExecute);              
        }  
 
        public void Open()  
        {  
            if (!this.window.IsOpen)  
            {  
                this.window.ShowDialog();  
            }  
        }  
 
        private void RadWindow_Closed(object sender, WindowClosedEventArgs e)  
        {  
             if (this.window.IsOpen)  
             {  
                 this.window.Close();  
             }  
        }  
 
        private void RadWindow_Opened(object sender, RoutedEventArgs e)  
        {  
            SynchronizeDB();  
        }  
 
        private void SynchronizeDB()  
        {  
            messageService.ShowInformation("Got DB Synch""");  
        }  
 
        public ICommand CloseCommand { get { return closeCommand; } }  
          
        private bool CanCloseExecute()  
        {  
            return true;  
        }  
        private void Close()  
        {  
            this.window.Close();  
        }  
    }  
}  
 

Module
using EliteExtender.Composite.Extensions;  
using EliteExtender.DatabaseSynchronization;  
using EliteExtender.DatabaseSynchronization.Domain;  
using EliteExtender.DatabaseSynchronization.ViewModels;  
using EliteExtender.DatabaseSynchronization.Views;  
using Microsoft.Practices.Composite.Modularity;  
using Microsoft.Practices.Composite.Regions;  
 
namespace EliteExtender.DatabaseSynchronization  
{  
    [Module(ModuleName = "DBSynchronization")]  
    public class Module : IModule  
    {  
        private readonly IApplicationContainer container;  
 
        public Module(IApplicationContainer container, IRegionManager regionManager)  
        {  
            this.container = container;  
 
            container.RegisterType<ISynchronizationView, SynchronizationView>(true);  
        }  
 
        public void Initialize()  
        {  
            container.Resolve<ModuleController>().Initialize();  
        }  
    }  
}  
 

Controller
using System;  
using System.Collections.Generic;  
using System.Collections.Specialized;  
using System.Linq;  
using System.Text;  
using Microsoft.Practices.Composite.Regions;  
using System.Reflection;  
using EliteExtender.Composite.Extensions;  
using EliteExtender.DatabaseSynchronization.ViewModels;  
using EliteExtender.DatabaseSynchronization.Properties;  
using EliteExtender.Infrastructure;  
using Microsoft.Practices.Composite.Regions;  
 
namespace EliteExtender.DatabaseSynchronization.Domain  
{  
    internal class ModuleController  
    {  
        private readonly IApplicationContainer container;  
        private readonly IRegionManager regionManager;  
        private readonly IUIElementCreationService uiElementCreationService;  
        private SynchronizationViewModel presentationModel;  
 
        public ModuleController(IApplicationContainer container, IRegionManager regionManager, IUIElementCreationService uiElementCreationService)  
        {  
            this.container = container;  
            this.regionManager = regionManager;  
            this.uiElementCreationService = uiElementCreationService;  
        }  
 
        public void Initialize()  
        {  
            AddToolbarItems();  
        }  
 
        private void AddToolbarItems()  
        {  
            IToolBarItem toolBarItem = uiElementCreationService.CreateToolBarItem(Resources.DBSynchButtonText, Resources.SynchDB);  
            toolBarItem.Click += SynchDBToolBarItemClick;              
              
            regionManager.Regions[RegionNames.MainToolbarRegion].Add(toolBarItem);  
        }  
 
        private void SynchDBToolBarItemClick(object sender, EventArgs e)  
        {  
            if (presentationModel == null)  
            {  
                presentationModel = container.Resolve<SynchronizationViewModel>();  
                presentationModel.Open();  
            }  
        }  
    }  
}  
 
0
Miroslav Nedyalkov
Telerik team
answered on 30 Mar 2010, 08:23 AM
Hello Garry,

 Could you please give us some more information - what version of RadControls for WPF do you use, did you try to debug your application to see is the Show method of the window called, etc. If you could open a support ticket and provide us with a sample project this would be very helpful.

Kind regards,
Miroslav Nedyalkov
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Garry Clark
Top achievements
Rank 1
answered on 31 Mar 2010, 07:01 PM
Miroslav,
Apologies, but apparently I have something goofey in my code that I am trying to track down now. When I created a simplified solution, copying and pasting most of the code from my live app, I can get it to work. There are some differences between the two, but nothing I can see that should cause this type of behavoir.

I'm going to try and start stripping down my live app until I get to the point of either it starts to work or I can't strip it down any further. I will keep you posted to my results.

Thanks!
Tags
Window
Asked by
yonadav
Top achievements
Rank 1
Answers by
Miroslav Nedyalkov
Telerik team
yonadav
Top achievements
Rank 1
Garry Clark
Top achievements
Rank 1
Share this question
or