简体   繁体   English

在App.xaml.cs中传递全局对象的引用

[英]Passing reference of global object in App.xaml.cs

I am trying to implement a global object which stores every parameter that I can pass between pages. 我正在尝试实现一个全局对象,该对象存储我可以在页面之间传递的每个参数。 My object is called AppObject. 我的对象称为AppObject。 I can declare it in my mainpage.xaml.cs and then pass it through reference using 我可以在mainpage.xaml.cs中声明它,然后使用

Page.Navigate(typeof(anotherpage), AppObject);

However, I would like to save the objects to file through the on_suspending event. 但是,我想通过on_suspending事件将对象保存到文件中。 How can I pass this parameter to the on_suspending event or is there anyway for me to declare this AppObject in the app.xaml.cs and then pass it to the mainpage.xaml.cs? 我如何才能将此参数传递给on_suspending事件,或者我该如何在app.xaml.cs中声明此AppObject,然后将其传递给mainpage.xaml.cs?

My App.xaml.cs 我的App.xaml.cs

AppObject AppObject = new AppObject();

    /// <summary>
    /// Initializes the singleton application object.  This is the first line of authored code
    /// executed, and as such is the logical equivalent of main() or WinMain().
    /// </summary>
    public App()
    {
        this.InitializeComponent();
        this.Suspending += this.OnSuspending;
        Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;
    }

...more code

    /// <summary>
    /// Invoked when application execution is being suspended.  Application state is saved
    /// without knowing whether the application will be terminated or resumed with the contents
    /// of memory still intact.
    /// </summary>
    /// <param name="sender">The source of the suspend request.</param>
    /// <param name="e">Details about the suspend request.</param>
    private async void OnSuspending(object sender, SuspendingEventArgs e)
    {
        var deferral = e.SuspendingOperation.GetDeferral();

        // TODO: Save application state and stop any background activity

        //I want to write to file here and be able to refereence to the AppObject which I pass around.

        deferral.Complete();
    }

In App.xaml.cs, make sure the objects you need are public. 在App.xaml.cs中,确保所需的对象是公共的。 Say you have something like this: 假设您有这样的事情:

public AppObject appObject = new AppObject();

Now, in MainPage.xaml.cs, you just have to create the objects of the same type you want to pass. 现在,在MainPage.xaml.cs中,您只需创建要传递的相同类型的对象。 Then, create another object that has the reference to the App like this: 然后,创建另一个具有对应用程序的引用的对象,如下所示:

var appReference = App.Current as App;
AppObject objectInMain = appReference.appObject;

Now you have another object in MainPage.xaml.cs, that is the exact copy of the one in App.xaml.cs. 现在,MainPage.xaml.cs中有另一个对象,它是App.xaml.cs中的另一个对象的精确副本。 You've just effectively passed data between two pages. 您已经在两个页面之间有效地传递了数据。 You can create a reference to the App in any page as shown above and use the variables declared in App.xaml.cs in that page. 您可以在任何页面上创建对应用程序的引用,如上所示,并在该页面中使用App.xaml.cs中声明的变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM