Application Object  In WPF

Application Object  In WPF

The Application object provides an interface between our application and system.  It is accessible from all WPF projects. It is the entry point of the WPF application, where the initialization can take place. It works like global.asax file which manages session and application information. Each virtual method corresponds to an event by the same name. For example, the OnActivated virtual method raises the Activated event within the Application object. So we  can either override the OnActivated method or create an event handler for the Activated event to insert functionality at that point shortly after.

The Application object wraps a set of XAML files and creates a shared data environment between the

Pages. The use of the Application object to share variables across pages .

In this Example, we can access these properties at any point within our application only by adding the data using a string key value of Student .

Step  1: First we write the Following code:

public class Student
        {
            public string FName;
            public string LName;
        }
        public Window1()
        {
            InitializeComponent();
            Student s=new Student();
            s.FName="mahak";
            s.LName="garg";
            Application.Current.Properties["Student"] = s;
        }

Step2:  After that we take a Button(button1)

<Button Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click">Button</Button>

we write the following code in the Onclick event of the button. Now when we click on the button the MessageBox will be appear. Here we see the FName as a message and LName as a caption.

private void button1_Click(object sender, RoutedEventArgs e)
        {
            Student y = (Student)Application.Current.Properties["Student"];
            MessageBox.Show(y.FName, y.LName, MessageBoxButton.OK,
            MessageBoxImage.Hand);
        }

Clipboard02.jpg

 

Ebook Download
View all
Learn
View all