zoukankan      html  css  js  c++  java
  • windows phone开发中,如何在当前APP实例生命周期内使用全局变量

    首先,不得不说明一下,有个原则是:

    尽量不要使用全局变量

    具体什么原因,我没法说的透彻,只能表达为:管理不便,容易出错

    认识不全面,请见谅!

    但是原则归原则,有的场景下用用,还是很方便的

    方法1:

    第一步:

    app.cs中写入全局变量

        public partial class App : Application
        {
           
            public double current_longitude { get; set; }
            public double current_latitude { get; set; }
            public int current_distance { get; set; }
            public string province = null;
    第二步:存
    (Application.Current as App).current_longitude = cache_longitude;

    第三步:取

    longitude = (Application.Current as App).current_longitude;

    (方法1太简单了,是吧?)

    方法2:

    Windows Phone 7中使用PhoneApplicationService类保存应用程序状态

    我将这篇文章关键部分取出吧,感谢原作者!

    第一步:

    首先我们需要建一个PhoneApplicationService对象,用来管理状态

    PhoneApplicationService phoneAppServeice = PhoneApplicationService.Current;

    第二步:存

                string myValue;
    
                if (settings.TryGetValue<string>("MyValue",out myValue))
                {
                     phoneAppServeice.State["MyValue"]=myValue;
                }

    第三步:取

                if (phoneAppServeice.State.ContainsKey("MyValue"))
                {
                    你要赋值的地方 = phoneAppServeice.State["MyValue"];
                }

    方法3

    使用IsolatedStorageSettings,但声明,这个就不算一个app实例周期内的全局变量了

    只是点一下,告诉大家这个方案的可行性

    这个方法,会在手机中设立一个isolatedstorage来保存数据,山无棱天地合的时候,保存的数据才会没的!

    而不像前两个,在应用列表点击图标新起一个实例后又是一番新天地

    IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
    具体怎么用,大家去google吧!




  • 相关阅读:
    Non-Photorealistic Rendering using OpenCV ( Python, C++ )
    Tensorflow Eager execution and interface
    Linear and Logistic Regression in TensorFlow
    TensorFlow Ops
    Introduction to TensorFlow
    Java Syntax Specification
    java方法的虚分派和方法表
    λ演算
    活性变量分析
    java垃圾回收机制
  • 原文地址:https://www.cnblogs.com/fifa0329/p/4536681.html
Copyright © 2011-2022 走看看