簡體   English   中英

Windows應用商店應用程序:我使用全局變量和擴展初始屏幕正確嗎?

[英]Windows store app: Am i using global variable and extended splash screen correct?

我創建了一個具有許多Web服務調用的應用程序,為了使流程更好,並可能通過Windows存儲區有關啟動時間的要求,我決定使用擴展的初始屏幕來加載所有主要數據,然后在不同的位置共享這些數據頁面使用App.xaml.cs中定義的全局變量。

我的問題是:以這種方式使用全局變量是否正確,並且在應用程序被掛起/恢復運行時會丟失該數據嗎?因為我只是從他的擴展初始屏幕初始化此數據。

下面是我的代碼

這是來自app.xaml.cs頁面的一些代碼:在這里,我定義全局變量,從我的Web服務中加載應用程序的背景圖像,然后再進入extende初始屏幕

   sealed partial class App : Application
        {

            **public string[] NavigateData { get; set; }
            public NavigationCacheMode NavigationCacheMode { get; set; }**



            public App()
            {
                this.InitializeComponent();
                this.Suspending += OnSuspending;

                //Cache the page     
                this.NavigationCacheMode = Windows.UI.Xaml.Navigation.NavigationCacheMode.Enabled;
            }


            protected async override void OnLaunched(LaunchActivatedEventArgs args)
            {
                Frame rootFrame = Window.Current.Content as Frame;
               //---------------------------------Live tile
                var uris = new List<Uri> 
                { 
                    new Uri("http://XXX.XX.XX.XXX/WebApi/WebService.asmx/GetFirstTile"),

                    new Uri("http://XXX.XX.XX.XXX/WebApi/WebService.asmx/GetSecondTile"),
                    new Uri("http://XXX.XX.XX.XXX/WebApi/WebService.asmx/GetStatisticTile"),
                    new Uri("http://XXX.XX.XX.XXX/WebApi/WebService.asmx/GetNewsTile1"),
                    new Uri("http://XXX.XX.XX.XXX/WebApi/WebService.asmx/GetNewsTile2"),

                };

                TileUpdater LiveTileUpdater = TileUpdateManager.CreateTileUpdaterForApplication();         
                LiveTileUpdater.EnableNotificationQueue(true);  // Enable notifications
                LiveTileUpdater.Clear();  // Clear the current set of updates
                LiveTileUpdater.StartPeriodicUpdateBatch(uris, PeriodicUpdateRecurrence.HalfHour);
                //------------------------------Live tile section end


                // Do not repeat app initialization when the Window already has content,
                // just ensure that the window is active
                if (rootFrame == null)
                {
                    // Create a Frame to act as the navigation context and navigate to the first page
                    rootFrame = new Frame();
                    weather.Common.SuspensionManager.RegisterFrame(rootFrame, "appFrame");


       //When the app is loaded first time it calls the web service to get wich background picture to use
             //--------------------------Getting and setting background image for all the pages--------------------
                    string appBackgGround;
       ServiceReference.WebServiceSoapClient webServiceObj = new ServiceReference.WebServiceSoapClient();
                    // Get the name of the Background picture
       appBackgGround = await webServiceObj.GetBackgroundImageAsync();


                    // Her we set the application background Image for all pages  "backgroundImageBlueSky.jpg"
                    rootFrame.Background = new ImageBrush
                    {
                        Stretch = Windows.UI.Xaml.Media.Stretch.UniformToFill,
                        ImageSource = new BitmapImage { UriSource = new Uri("ms-appx:///Assets/"+appBackgGround) }

                    };
      //--------------------------Bacground image end--------------------



                    if (args.PreviousExecutionState == ApplicationExecutionState.Terminated)
                    {
                        //TODO: Load state from previously suspended application
                        await weather.Common.SuspensionManager.RestoreAsync();
                    }

                    // Place the frame in the current Window
                    Window.Current.Content = rootFrame;
                }

                if (rootFrame.Content == null)
                {
                    // When the navigation stack isn't restored navigate to the first page,
                    // configuring the new page by passing required information as a navigation
                    // parameter
                    //Go to the extended splash screen
                    if (!rootFrame.Navigate(typeof(ExtendedSplashScreen), rootFrame.GetNavigationState()))
                    {
                        throw new Exception("Failed to create initial page");
                    }


                }
                // Ensure the current window is active
                //create the about page
                var _Helper = new Flyouts.SettingsHelper();
                _Helper.AddCommand<Flyouts.About>("About");
                Window.Current.Activate();
            }


            private async void OnSuspending(object sender, SuspendingEventArgs e)
            {
                var deferral = e.SuspendingOperation.GetDeferral();
                //TODO: Save application state and stop any background activity
                await weather.Common.SuspensionManager.SaveAsync();
                deferral.Complete();
            }


        }
    }   

ExtendedSplashScreen:在這里,我在進入主頁之前從Web服務加載了一些應用程序的初始化數據。 數據將保存到App.xaml.cs中定義的全局變量。 在加載此數據時,將顯示進度環。 數據加載后,我進入主頁

public sealed partial class ExtendedSplashScreen : Page
{

    parameterItem max1DayAgo = new parameterItem();
    parameterItem min1DayAgo = new parameterItem();


    public ExtendedSplashScreen()
    {
        this.InitializeComponent();
    }

    protected override async void OnNavigatedTo(NavigationEventArgs e)
    {

        string[] periodSelector     = { "1DayAgo", "1WeekAgo", "1MonthAgo" };
        string[] modeSelector       = { "max", "min" };
        string[] parameterSelector  = { "umtTemp1", "umtWindSpeed", "umtAdjBaromPress", "umtRainRate" };

        //---------------GETTING WEBSERVICE DATA FOR STARTUP-------------
        //Create a webservice object
        ServiceReference.WebServiceSoapClient webServiceObj = new ServiceReference.WebServiceSoapClient();       
        var getMax1DayAgoObj = await webServiceObj.GetSelectedMaxMinDataAsync(parameterSelector, periodSelector[0], modeSelector[0]);

        //create an object that holds min data for yesterday

        var getMin1DayAgoObj = await webServiceObj.GetSelectedMaxMinDataAsync(parameterSelector, periodSelector[0], modeSelector[1]);
        //Save arrayOfValue and arrayOfUnit to a parameterItem object. these objects are created during startup
        // and the can be accessed and updated by all methods in this page later we will see that maxMinButton_Click method
        //for the maxMinButton will use these data
        max1DayAgo.arrayOfValue = getMax1DayAgoObj.arrayOfValue;
        max1DayAgo.arrayOfUnit = getMax1DayAgoObj.arrayOfUnit;

        min1DayAgo.arrayOfValue = getMin1DayAgoObj.arrayOfValue;
        min1DayAgo.arrayOfUnit = getMin1DayAgoObj.arrayOfUnit;

        string[] startupData = new string[13];

        startupData[0] = " " + max1DayAgo.arrayOfValue[0] + " " + max1DayAgo.arrayOfUnit[0]; //    maxTemp 
        startupData[1] = " " + max1DayAgo.arrayOfValue[1] + " " + max1DayAgo.arrayOfUnit[1]; //    maxWindSped 
        startupData[2] = " " + max1DayAgo.arrayOfValue[2] + " " + max1DayAgo.arrayOfUnit[2]; //    maxAirPressure 
        startupData[3] = " " + max1DayAgo.arrayOfValue[3] + " " + max1DayAgo.arrayOfUnit[3];//     maxRainfall

        startupData[4] = " " + min1DayAgo.arrayOfValue[0] + " " + min1DayAgo.arrayOfUnit[0]; //    minTemp 
        startupData[5] = " " + min1DayAgo.arrayOfValue[1] + " " + min1DayAgo.arrayOfUnit[1];//     minWindSped 
        startupData[6] = " " + min1DayAgo.arrayOfValue[2] + " " + min1DayAgo.arrayOfUnit[2];//     minAirPressure  
        startupData[7] = " " + min1DayAgo.arrayOfValue[3] + " " + min1DayAgo.arrayOfUnit[3];//     minRainfall
        // Main fields

        var getLatestTempObj        = await webServiceObj.GetLatestDataAsync("umtTemp1");
        var getLatestWindObj        = await webServiceObj.GetLatestDataAsync("umtWindSpeed");
        var getLatestwindDirObj     = await webServiceObj.GetLatestDataAsync("umtAdjWinDir");
        var getLatestairPressureObj = await webServiceObj.GetLatestDataAsync("umtAdjBaromPress");

        startupData[8] = " " + getLatestTempObj.Value + " " + getLatestTempObj.Unit;//temperatureMainTxtBlock.Text
        startupData[9] = " " + getLatestWindObj.Value + " " + getLatestWindObj.Unit;//temperatureMainTxtBlock.Text
        startupData[10] = "" + getLatestwindDirObj.Value; //temperatureMainTxtBlock.Text
        startupData[11] = " " + getLatestairPressureObj.Value + " " + getLatestairPressureObj.Unit;//temperatureMainTxtBlock.Text
        startupData[12] = "Last update: " + getLatestwindDirObj.Timestamp;//temperatureMainTxtBlock.Text
        **//pass the webservice data to the global variable
        (App.Current as App).NavigateData = startupData;**

        //since im using extendes splash screen i reset the navigation history so the user cannot go back to the extended splash screen      
        this.Frame.SetNavigationState(e.Parameter as string);
        //Go to mainpage
        this.Frame.Navigate(typeof(MainPage));
    }
}

MainPage:在Mainpage中,我在文本框中顯示由擴展啟動屏幕加載的全局數據。

public sealed partial class MainPage : weather.Common.LayoutAwarePage
    {
        //Defining objects use through this page
        maxMinSelector maxMinButtonSelector = new maxMinSelector();

        parameterItem max1DayAgo = new parameterItem();
        parameterItem min1DayAgo = new parameterItem();

        parameterItem max1WeekAgo = new parameterItem();
        parameterItem min1WeekAgo = new parameterItem();

        parameterItem max1MonthAgo = new parameterItem();
        parameterItem min1MonthAgo = new parameterItem();

        EasingDoubleKeyFrame keyFrame = new EasingDoubleKeyFrame();

        public MainPage()
        {
            this.InitializeComponent();
            Loaded += MainPage_Loaded;


             Code
         ------------------
         ------------------
         ------------------
         ------------------
            //Cache the page     
            this.NavigationCacheMode = Windows.UI.Xaml.Navigation.NavigationCacheMode.Enabled;
        }


        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
             Code
            ------------------
         ------------------
         ------------------
         ------------------
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {

            // HERE i use the global data initialised in the extended splash screen
            base.OnNavigatedTo(e);
            maxTempTextblock.Text = (App.Current as App).NavigateData[0];
            maxWindSpedTextBlock.Text = (App.Current as App).NavigateData[1];
            maxAirPressureTextBlock.Text = (App.Current as App).NavigateData[2];
            maxRainfallTextBlock.Text = (App.Current as App).NavigateData[3];

            minTempTextblock.Text = (App.Current as App).NavigateData[4];
            minWindSpedTextBlock.Text = (App.Current as App).NavigateData[5];
            minAirPressureTextBlock.Text = (App.Current as App).NavigateData[6];
            minRainfallTextBlock.Text = (App.Current as App).NavigateData[7];


            temperatureMainTxtBlock.Text = (App.Current as App).NavigateData[8];
            WindSpeedTxtBlockLower.Text = (App.Current as App).NavigateData[9];
            WindDirectionTxtBlockLower.Text = (App.Current as App).NavigateData[10];
            airPressureTxtBlockLower.Text = (App.Current as App).NavigateData[11];
            LastUpdateTextField.Text = (App.Current as App).NavigateData[12];


        }

        Code
          ------------------
         ------------------
         ------------------
          ------------------
         ------------------
         ------------------
         ------------------

          ------------------
         ------------------
         ------------------
         ------------------

         ------------------



    }

這實際上並不是您所要提出的問題的答案,但是Build 2012中有一段很棒的視頻介紹了啟動響應性和XAML性能,這可能會觸及您的要求。 他確實談到了如何處理具有很多啟動瑣事的應用程序。

http://channel9.msdn.com/Events/Build/2012/4-103

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM