Friday 18 November 2011

Using the App.Xaml for passing data between pages Or Storing And Retriving Data Globally

Suppose you have created user control in that you are getting values from the database and binding it to the datagrid and same data you want to access into the main page where this user control is added or some other page.
So Go to the App.xaml.cs in that create public property of the List or ObservableCollection like:
  public ObservableCollection<TblXyzDetail> OCTempTblXyzDetail { get; set; }
  public ObservableCollection<TblPQRDetail> OCTempLocationMasterDetail { get; set; }

in the above line TblXyzDetail & TblPQRDetail is my Class(i.e. view which is added into the edmx.)

after that initialize this object in startup event like

  private void Application_Startup(object sender, StartupEventArgs e)
        {
            //following lines are important to initialize the object.
            OCTempTblXyzDetail = new ObservableCollection<Web.Models.TblXyzDetail>();
            OCTempLocationMasterDetail = new    bservableCollection<Web.Models.TblPQRDetail>();
       
            System.Net.WebRequest.RegisterPrefix("http://", System.Net.Browser.WebRequestCreator.ClientHttp);
            // This will enable you to bind controls in XAML files to WebContext.Current
            // properties
            this.Resources.Add("WebContext", WebContext.Current);

            // This will automatically authenticate a user when using windows authentication
            // or when the user chose "Keep me signed in" on a previous login attempt
            WebContext.Current.Authentication.LoadUser(this.Application_UserLoaded, null);
            // Show some UI to the user while LoadUser is in progress
            this.InitializeRootVisual();


}


After this go to the Usercontrol.cs page in that create object of the app.xmal like

private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
     //here I am creating object of the app.xmal page
     App app = (App)Application.Current;

     //here i am creating the new public property of the observable collection and also initializing
             public ObservableCollection<TblXyzDetail> OCTblXyzDetail { get; set; }
OCTblXyzDetail = new ObservableCollection<TblXyzDetail>();


     //here I am initializing the object of the app.xmal page
            app.OCTempTblXyzDetail = new ObservableCollection<TblXyzDetail>();           
        }


So you can access public variables, objects and functions etc. after that assign the data to the OCTempTblXyzDetail  object of the app.xaml.cs like

Here OCTblXyzDetail is my main object of the user control in that I am storing the databse values at the time of page load. Like

private void GetAllAttributeNameByDataDimensionIDQuery_Complete(LoadOperation<TblXyzDetail> operation)
        {
            if (!operation.HasError)
            {
                OCTblXyzDetail = operation.Entities.ToObservableCollection();
                if (OCTblXyzDetail.Count > 0)
                {
Datagrid.ItemsSource = OCTblXyzDetail;
//follwoing line is used to assign the data to the object of the app.xaml page
app.OCTempTblXyzDetail = OCTblXyzDetail;

                }
            }
        }



Now you can get the data from anywhere in application. Here I am retrieving  this data into the my main page here also you have to create the object of the app.xaml page like 
   private void Page_Loaded(object sender, RoutedEventArgs e)
    {
//here I am creating object of the app.xmal page
       App app = (App)Application.Current;
ObservableCollection  OCTblXyzDetail = new ObservableCollection<TblXyzDetail>();

//Following line is used to get data from the object of the app.xaml
OCTblXyzDetail =app. OCTempTblXyzDetail.ToObservableCollection();
    }
Now you can use OCTblXyzDetail to bind to the list or to data grid.


1 comment: