WP7 Development App/Page wide variable
Does anyone know how to set a variable that ca开发者_C百科n be accessed on all pages of a Windows Phone 7 App?
Without knowing any more specifics of your situation, you could create the variables as members of your App class:
public partial class App : Application
{
...
public int foo { get; set; }
...
}
Then access if from wherever:
(App.Current as App).foo = 3;
Setup global variables, quick and easy, make a new class for GlobalVariables:
public static class GlobalVariables
{
public static string my_string = "";
public static int my_int = -1;
}
Then you access the Global Variables class like this:
GlobalVariables.variable_name;
I had the same problem with some variables, I solved it initializing the variables in the constructor of the page and not in any method:
Dictionary<string, int> clients;//Global variable
// Constructor
public MainPage()
{
InitializeComponent();
clients = new Dictionary<string, int>();
}
精彩评论