How to make a string value available from one scope to another in a C # Silverlight application
Update: creating a userIP socket seemed to work. However, I found out that MainPage () is explicated before Application_Startup (), so the InitParam values ββare not immediately available. I might have to put my code somewhere else.
I am writing a Silverlight application and taking a variable in InitParams, and I want that variable to be available in some way to other areas of my code. I would rather not immediately assign a value to an element in XAML, and instead use C # if possible. I have to take one more step before using the data to modify the XAML. Here's what I've done so far:
In my App.xaml.cs file, I added the userIP line to the App class, hoping to access this value later. Then I try to assign the value of the InitParams variable to the userIP string I did above. This is what it looks like.
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 am wondering if it is correct to do this so that this data is available later.
In my MainPage.xaml.cs file, I am trying to reference the userIP value given earlier, but I cannot figure out how. For example, I want to create a new line and then set it to userIP:
public MainPage()
{
InitializeComponent();
string myUserIP;
myUserIP = VideoDemo2.App.userIP;
}
Then I get the error: Error 1 Object reference is required for non-static field, method or property "VideoDemo2.App.userIP".
I need to do something with 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 XAML if possible. What must happen so that I can "see" the value later in the application? I'm new to C #, so any help would be much appreciated.
source to share
FlySwat and James Cadd's answers were helpful, but I found it better for Silverlight to use an application resource dictionary.
In an ASPX or HTML page, use the tag <param>
in the Silverlight tag <object>
:
<param name="initParams" value="txtSomeVariable=SomeValue"/>
Then, in the Application_Startup method from App.xaml.cs, use a foreach loop to add values ββto the resource dictionary:
private void Application_Startup(object sender, StartupEventArgs e)
{
//Method 1 - Resource Dictionary
if (e.InitParams != null)
{
foreach (var item in e.InitParams)
{
this.Resources.Add(item.Key, item.Value);
}
}
this.RootVisual = new MainPage();
}
To pull a value from a dictionary for the entire life of your application, simply use:
App.Current.Resources["txtSomeVariable"].ToString();
I learned about InitParams and the Application Resource Dictionary from Tim Heuer's video in Silverlight.Net: Using Launch Parameters in Silverlight .
Also, I wrote a blog post describing this situation in more detail: Pass the user's IP address to Silverlight as a parameter .
I hope this information helps other users who stumbled upon this question!
source to share
The problem is that VideoDemo2.App is not an instance but a type.
If you want to access userIP, you need to access the actual instance of your application.
Silverlight provides a static property that exposes the current instance of the application:
App runningApp = (VideoDemo2.App)Application.Current;
string myUserIP = runningApp.userIP;
Or you can make userIP a static string in your application. You remove "this" where it is installed, but you can type-access it from anywhere.
namespace VideoDemo2
{
public partial class App : Application
{
public static 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();
App.userIP = e.InitParams["txtUserIP"];
}
...}
source to share
You can find your instance of the App class in the static property Application.Current. Try this from anywhere in your SL app:
((VideoDemo2.App)Application.Current).userIP;
EDIT: By contrast, I'd rather keep such settings in a single class (its a simple template to implement in C #, a quick lookup should be enough). Also, for "good form" everything that the public should be property and public properties should be imposed with pascal. Change the public userIp string to:
public string UserIP { get; set; }
And you instantly win friends and influence people;)
EDIT: This is good information about the lifecycle of the Application class in Silverlight http://msdn.microsoft.com/en-us/library/system.windows.application%28VS.95%29.aspx
source to share