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

Execute Event

1 Answer 71 Views
Window
This is a migrated thread and some comments may be shown as answers.
Josemar Maia
Top achievements
Rank 1
Josemar Maia asked on 30 Jul 2010, 07:54 PM
How do execute the event MainPage.TestApp in SecondPage? Note: SecondPage is called the MainPage.

MainPage.Xaml
<UserControl x:Class="SilverlightApp.MainPage"
    mc:Ignorable="d"
    xmlns:controlsToolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"
    d:DesignHeight="300" d:DesignWidth="400" xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation">
 
  <controlsToolkit:BusyIndicator x:Name="biProcess">
    <Grid x:Name="LayoutRoot" Background="White">
      <telerik:RadButton Content="Load Second Page" Height="Auto" HorizontalAlignment="Left" Margin="168,133,0,0" Name="radButton1" VerticalAlignment="Top" Width="Auto" Click="radButton1_Click" />
    </Grid>
  </controlsToolkit:BusyIndicator>
</UserControl>

MainPage.Xaml.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Resources;
using System.Xml;
using Telerik.Windows.Controls;
 
namespace SilverlightApp
{
  public partial class MainPage : UserControl
  {
    public MainPage()
    {
      InitializeComponent();
    }
 
    private void radButton1_Click(object sender, RoutedEventArgs e)
    {
      ProcessingOptionClicked("SilverlightApp", "SilverlightApp.SecondPage", "SecondPage");
    }
 
    private void testApp(string strTest)
    {
      RadWindow.Alert(strTest);
    }
 
    private List<XAPLoader> listXAPs = new List<XAPLoader>();
    private List<RadWindow> listWindows = new List<RadWindow>();
 
    private string _TitleNewWindow;
    private string _TypeClassNewWindow;
    private void ProcessingOptionClicked(string xap, string classe, string TitleNewWindow)
    {
      _TitleNewWindow = TitleNewWindow;
      _TypeClassNewWindow = classe;
 
      RadWindow windowInList = listWindows.FirstOrDefault(x => x.Tag.ToString().ToLower() == _TitleNewWindow.ToLower());
      if (windowInList != null)
      {
        windowInList.Show();
      }
      else
      {
        XAPLoader xapInList = listXAPs.FirstOrDefault(x => x.NameXAP.ToLower() == xap.ToLower());
 
        if (xapInList == null)
        {
          biProcess.BusyContent = "Load Module...";
          biProcess.IsBusy = true;
 
          XAPLoader loader = new XAPLoader(xap, biProcess);
          loader.XapLoaded += loader_XapLoaded;
          loader.LoadXap(xap);
        }
        else
        {
          ShowWindow(xapInList, _TypeClassNewWindow, _TitleNewWindow);
        }
      }
    }
 
    private void loader_XapLoaded(object sender, EventArgs e)
    {
      biProcess.IsBusy = false;
 
      XAPLoader xapLoaded = (XAPLoader)sender;
 
      listXAPs.Add(xapLoaded);
 
      ShowWindow(xapLoaded, _TypeClassNewWindow, _TitleNewWindow);
    }
 
    private void ShowWindow(XAPLoader xap, string typeClassNewWindow, string TitleNewWindow)
    {
 
      RadWindow window = null;
      UserControl userControl = null;
 
      try
      {
        window = new RadWindow();
 
        userControl = (UserControl)xap.InstantiateClass(typeClassNewWindow);
 
        double widthWindow = (userControl.MinWidth != 0 ? userControl.MinWidth : userControl.Width);
        double heightWindow = (userControl.MinHeight != 0 ? userControl.MinHeight : userControl.Height);
 
        window.VerticalAlignment = System.Windows.VerticalAlignment.Center;
        window.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
        window.Width = widthWindow + 14;
        window.Height = heightWindow + 30;
        window.WindowStartupLocation = Telerik.Windows.Controls.WindowStartupLocation.CenterScreen;
        window.Content = userControl;
        window.Header = _TitleNewWindow;
        window.Tag = _TitleNewWindow;
      }
      catch (Exception ex)
      {
        biProcess.IsBusy = false;
 
        string mensagemErro = "Error!";
 
        RadWindow.Alert(string.Format("Error in load of window {0}.{1}{1}{2}", _TitleNewWindow, Environment.NewLine, mensagemErro));
      }
 
      if (window != null && userControl != null)
      {
        window.Loaded += new RoutedEventHandler(window_Loaded);
        window.Closed += new EventHandler<WindowClosedEventArgs>(window_Closed);
        window.Show();
      }
    }
 
    void window_Loaded(object sender, RoutedEventArgs e)
    {
      listWindows.Add((RadWindow)sender);
 
      WeakReference referance = new WeakReference(sender);
    }
 
    void window_Closed(object sender, WindowClosedEventArgs e)
    {
      RadWindow window = (RadWindow)sender;
 
      GC.Collect();
      GC.WaitForPendingFinalizers();
      GC.Collect();
 
      listWindows.Remove(window);
    }
 
  }
 
  public class XAPLoader
  {
    private string _m_rootAssembly;
    private Assembly _asm;
 
    public event EventHandler<EventArgs> XapLoaded;
 
    public string NameXAP { get; set; }
 
    private BusyIndicator _biProcess { get; set; }
 
    public XAPLoader(string namexap, BusyIndicator biProcess)
    {
      NameXAP = namexap;
      _biProcess = biProcess;
    }
 
    public void LoadXap(string nameXAP)
    {
      Uri xapUri = new Uri((nameXAP + ".xap"), UriKind.Relative);
 
      _m_rootAssembly = System.IO.Path.GetFileNameWithoutExtension(xapUri.ToString());
 
      WebClient wc = new WebClient();
      wc.OpenReadCompleted += wc_OpenReadCompleted;
      wc.OpenReadAsync(xapUri);
    }
 
    private void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {
      try
      {
        if (e.Error != null)
        {
          string mensagemError = string.Format("Error creating instance {0}.{1}{1}{2}", NameXAP, Environment.NewLine, e.Error.Message);
 
          throw new Exception(mensagemError);
        }
 
        var manifestStream = Application.GetResourceStream(
            new StreamResourceInfo(e.Result, null),
            new Uri("AppManifest.xaml", UriKind.Relative));
 
        string appManifest = new StreamReader(manifestStream.Stream).ReadToEnd();
        string assemblyName = _m_rootAssembly + ".dll";
        XmlReader reader = XmlReader.Create(new StringReader(appManifest));
        _asm = null;
        while (reader.Read())
        {
          if (reader.IsStartElement("AssemblyPart"))
          {
            reader.MoveToAttribute("Source");
            reader.ReadAttributeValue();
            if (reader.Value == assemblyName)
            {
              var assemblyStream = new StreamResourceInfo(e.Result, "application/binary");
              var si = Application.GetResourceStream(assemblyStream, new Uri(reader.Value, UriKind.Relative));
              AssemblyPart p = new AssemblyPart();
              _asm = p.Load(si.Stream);
              break;
            }
          }
        }
 
        if (_asm == null)
          throw new InvalidOperationException("XapLoader: Could not load the assembly " + assemblyName + ".");
      }
      catch (Exception ex)
      {
        _biProcess.IsBusy = false;
 
        throw new Exception(ex.Message);
      }
 
      RaiseXapLoadedEvent();
    }
 
    public object InstantiateClass(string typeClass)
    {
      var retorno = _asm.CreateInstance(typeClass);
 
      if (retorno == null)
      {
        throw new InvalidOperationException("XapLoader: could not instantiate the type " + typeClass + ".");
      }
 
      return retorno;
    }
 
    private void RaiseXapLoadedEvent()
    {
      if (XapLoaded != null)
      {
        XapLoaded(this, new EventArgs());
      }
    }
  }
 
}

SecondPage.Xaml

<UserControl x:Class="SilverlightApp.SecondPage"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400"
     
    <Grid x:Name="LayoutRoot" Background="White">
    <telerik:RadButton Content="Close!" Height="Auto" HorizontalAlignment="Left" Margin="323,251,0,0" Name="radButton1" VerticalAlignment="Top" Width="Auto" Click="radButton1_Click" />
  </Grid>
</UserControl>

SecondPage.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;
using Telerik.Windows.Controls;
 
namespace SilverlightApp
{
  public partial class SecondPage : UserControl
  {
    public SecondPage()
    {
      InitializeComponent();
    }
 
    private void radButton1_Click(object sender, RoutedEventArgs e)
    {
      (this.Parent as RadWindow).Close();
    }
  }
}

1 Answer, 1 is accepted

Sort by
0
George
Telerik team
answered on 05 Aug 2010, 01:08 PM
Hi Josemar Maia,

Could you please give us more detailed information about your scenario and what is the exact problem with RadWindow you are facing. 

I will be glad to assist you further.

Regards,
George
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
Tags
Window
Asked by
Josemar Maia
Top achievements
Rank 1
Answers by
George
Telerik team
Share this question
or