*Update: making the userIP static seemed to work. However, I learned that MainPage() is exectuting before Application_Startup(), so the InitParam values aren't immediately available. I may have to put my code somewhere else.*
I'm writing a Silverlight App and am taking in a variable in InitParams and I want that variable to be accessible in some way to other areas of my code. I'd prefer not to immediately assign the value to an element in XAML and instead use C# if possible. I have to do another step before I use the data to modify the XAML. Here's what I did so far:
In my App.xaml.cs file, I added a string userIP to the App class in hopes of accessing this value later. I then try to assign the value of the InitParams variable to the userIP string that I made above. Here's how it looks.
namespace VideoDemo2
{
public partial class App : Application
{
public string userIP;
public App()
{
this.Startup += this.Application_Startup;
this.Exit += this.Application_Exit;
this.UnhandledException += this.Application_UnhandledException;
InitializeComponent();
}
private void Application_Startup(object sender, StartupEventArgs e)
{
this.RootVisual = new MainPage();
this.userIP = e.InitParams["txtUserIP"];
}
...}
The only lines I added to the code were public string userIP;
and this.userIP = e.InitParams["txtUserIP"];
. I'm wondering if this is the right way to go about this to make this data available later.
In my MainPage.xaml.cs file, I'm trying to reference the userIP value I specified earlier, but I can't figure out how to do so. For example, I want to create a new string and then set it to be equal to the userIP:
public MainPage()
{
InitializeComponent();
string myUserIP;
myUserIP = VideoDemo2.App.userIP;
}
Then I get an error that says: Error 1 An object reference is required for the non-static field, method, or property 'VideoDemo2.App.userIP'.
I have to do something with the InitParams
in App.xaml.cs because that's where the arguments are passed, but I want to make one of those parameters available to other parts of my application, without putting it in the XAML, if possible. What needs to happen so that I can "see" the value later on in the application? I'm brand new to C#, so any help would be very much appreciated.