Blog

Application Object  In WPF

Posted by Mahak Gupta Blogs | WPF Jan 03, 2012
The Application object provides an interface between our application and system.

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

 

COMMENT USING
PREMIUM SPONSORS
Hire Mobile & Web Developer on demand. 100% satisfaction. Try for 1 week or Money Back. Local and remote developers available all over USA.
SPONSORED BY
  • MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.