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

RadWindow.WindowClosed event problem

3 Answers 76 Views
Window
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Efthymios
Top achievements
Rank 1
Efthymios asked on 01 Mar 2013, 11:41 AM
Hello,
I think that I have found a small problem and I need to find a workaround for it. I have a RadWindow inside a page of my application and I have also assigned via 'code' the WindowClosed event as:
dlg = new WizardDialog();
dlg.window.WindowClosed += new EventHandler<Telerik.Windows.Controls.WindowClosedEventArgs>(wizardWindow_WindowClosed);

My problem is that the window calls the event handler before even the close animation finishes and the window gets totally closed. For this reason, I can not open an other RadWindow straight at that time. Am I doing something wrong?
I couldn't attach a zip file of a demo project that displayes that weird behavior so I copy the code here (sorry about that).

MainPage.xaml.cs -> Contains only a button
private void button1_Click(object sender, RoutedEventArgs e)
        {
            QuestionDialog dlg = new QuestionDialog();
            dlg.window.WindowClosed += new EventHandler<Telerik.Windows.Controls.WindowClosedEventArgs>(window_WindowClosed);
            dlg.CaptionTextBlock.Text = "Question";
            dlg.DescrTextBlock.Text = "Do you like apples?";
            dlg.window.IsOpen = true;
        }
 
        void window_WindowClosed(object sender, Telerik.Windows.Controls.WindowClosedEventArgs e)
        {
            QuestionDialog dlg = new QuestionDialog();
            dlg.window.WindowClosed += new EventHandler<Telerik.Windows.Controls.WindowClosedEventArgs>(window_WindowClosed);
            dlg.CaptionTextBlock.Text = "Question";
            dlg.DescrTextBlock.Text = "Do you red apples?";
            dlg.window.IsOpen = true;
            //throw new NotImplementedException();
        }

QuestionDialog.xaml
<UserControl x:Class="MyRadWindow.QuestionDialog"
    xmlns:telerikCore="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Core"
    xmlns:telerikPrimitives="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Primitives"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    d:DesignHeight="480" d:DesignWidth="480">
     
    <Grid x:Name="LayoutRoot" Height="346" Width="336">
        <telerikPrimitives:RadModalWindow x:Name="window"
                                          Placement="CenterCenter" Canvas.ZIndex="2">
 
 
 
            <telerikPrimitives:RadModalWindow.OpenAnimation>
                <telerikCore:RadPlaneProjectionAnimation CenterY="0.5" CenterX="0" StartAngleX="-90" EndAngleX="0" Axes="X">
                    <telerikCore:RadPlaneProjectionAnimation.Easing>
                        <CubicEase EasingMode="EaseOut"/>
                    </telerikCore:RadPlaneProjectionAnimation.Easing>
                </telerikCore:RadPlaneProjectionAnimation>
            </telerikPrimitives:RadModalWindow.OpenAnimation>
            <Border
                BorderThickness="{StaticResource PhoneBorderThickness}" HorizontalAlignment="Left" VerticalAlignment="Top" BorderBrush="#BFFF0000">
                <Grid Background="#FFC24A4A">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <StackPanel Grid.ColumnSpan="2" Background="Red">
                        <TextBlock x:Name="CaptionTextBlock"
                            TextWrapping="Wrap" Text="This is a caption" Margin="8,0,0,0" Foreground="White"/>
                    </StackPanel>
                    <TextBlock x:Name="DescrTextBlock"
                        Grid.Row="1"
                        Grid.ColumnSpan="2"
                        HorizontalAlignment="Stretch"
                        TextWrapping="Wrap" Text="description" Foreground="#FFFFF1F1" Margin="8,0"
                             />
                    <Button x:Name="OKButton" Grid.Row="3" Grid.Column="0" Content="Yes" Click="OKButton_Click" BorderBrush="#FFFF4F4F" Foreground="#FFFFADAD"/>
                    <Button x:Name="CancelButton" Grid.Row="3" Grid.Column="1" Content="No" Click="CancelButton_Click" BorderBrush="#FFFF4F4F" Foreground="#FFFFADAD"/>
                </Grid>
            </Border>
        </telerikPrimitives:RadModalWindow>
    </Grid>
</UserControl>

QuestionDialog.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
 
namespace MyRadWindow
{
    public partial class QuestionDialog : UserControl
    {
        public Boolean bOKPressed = false;
        public QuestionDialog()
        {
            InitializeComponent();
 
            window.WindowSize = new Size(
                Application.Current.RootVisual.RenderSize.Width,
                Application.Current.RootVisual.RenderSize.Height
                );
        }
 
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            bOKPressed = true;
            this.window.IsOpen = false;
        }
 
        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            bOKPressed = false;
            this.window.IsOpen = false;
        }
 
    }
}


Thank you very much.

Efthymios Kalyviotis

3 Answers, 1 is accepted

Sort by
0
Victor
Telerik team
answered on 01 Mar 2013, 12:46 PM
Hi Efthymios,

You can always subscribe to the Ended event of the close animation if you want to something after the animation finishes.
I hope this helps.

Regards,
Victor
the Telerik team
Have a suggestion or face a problem - you can use the Ideas & Feedback portal to submit ideas, feedback and vote for them.
0
Efthymios
Top achievements
Rank 1
answered on 01 Mar 2013, 02:20 PM
Hello Victor,

The solution that you have given does not seem to work. I have done some further investigation to the problem and I have found the following:

When the WindowClosed event is triggered, the child components inside it have not been deleted (the contents of that window still exist). If inside that WindowClosed event I open a new instance of that window (to display a different message) the system will crash because it will have components with exactly the same name as the previous window. For example the application in its memory will have two buttons with the name 'OKButton' which is obviously a mistake. I have added the following code inside the WindowClosed event before I open my new window and seemed to be working:

dlg.OKButton.Name = "testOK";
dlg.CancelButton.Name = "testCancel";
dlg.CaptionTextBlock.Name = "testCaption";
dlg.DescrTextBlock.Name = "testDescr";

My problem is that such coding was not good. Finally I found a new solution to the problem by calling the following inside the event handler:
(sender as RadModalWindow).Content = null;

Please, since you are more experienced by me, feel free to give any hint if you have any better idea.

Thank you very much.

Efthymios Kalyviotis
0
Victor
Telerik team
answered on 04 Mar 2013, 08:46 AM
Hello Efthymios,

You can easily avoid adding the same element twice by opening your new window using Dispatcher.BeginInvoke(). This way, you will be sure that the element will be removed first before being added again.

We are reluctant to change the behavior of the window event as it is a commonly used API and will certainly break a lot of customer applications.

Also, you can consider avoiding setting a UIElement directly to the content property of a content control. You can achieve the same effect by using the ContentTemplate property to define the visual aspect of your app feature and then set the content to an object that can be reused many times and to which the ContentTemplate will be bound.

Thanks for understanding.

Regards,
Victor
the Telerik team
Have a suggestion or face a problem - you can use the Ideas & Feedback portal to submit ideas, feedback and vote for them.
Tags
Window
Asked by
Efthymios
Top achievements
Rank 1
Answers by
Victor
Telerik team
Efthymios
Top achievements
Rank 1
Share this question
or