Using Delegates and Events in Silverlight follows the same procedure as in a windows or web program.
The scenario in which we use a delegate is also the same in silverlight. The Scenario is We have a UserControl A inside an other page or user control B, and we need an event to control the actions of A from B.
Our aim here is to add a mouse click event to the UserControl A
In this case, inside A we declare a delegate as shown below.
public delegate void UserControlClicked(object sender, MouseButtonEventArgs e);
We Now declare an event for this particular delegate. It is this event that we call to invoke the method.
public event UserControlClicked RectangleClicked;
Now we write in the LeftButtonDown event of the UserControl A,
private void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (RectangleClicked!= null)
{
RectangleClicked(sender, e);
}
}
We check for the event condition to be null, just to verify that the event is called at B.
Now at B,
Rectangle b = new Rectangle();
b.RectangleClicked+= new Rectangle.userControlClicked(b_RectangleClicked );
and in the method RectangleClicked,
void b_RectangleClicked(object sender, MouseButtonEventArgs e)
{
FrameworkElement element = sender as FrameworkElement;
Rectangle box = (Rectangle)element;
string detail= "\Height: " + box.Height + " Width: " + box.Width + " Name: " + box.Name
HtmlPage.Window.Alert("Rectangle box "+detail);
}
For these to work, you must include the following namespaces.
using System.Windows.Browser; // to display the alert box.
Any mistakes in the post, please do add a comment.
Thank you.
Tuesday, March 17, 2009
Silverlight 2 - Using initParams
I found this very neat and simple article about using Initparams while searching how to pass value s to the silverlight application.
It is done by adding a tag inside the object call of the silverlight application. as shown below.
This is how you call the silverlight application. The line in bold shows how parameter is to be passed to the application.
We now edit the App.xaml.cs to match our requirements like shown below.
and in the page.xaml.cs file,
It is done by adding a
This is how you call the silverlight application. The line in bold shows how parameter is to be passed to the application.
<object data="data:application/x-silverlight,"
type="application/x-silverlight-2"
width="400" height="200">
<param name="source" value="InitParamsTest.xap"/>
<param name="minRuntimeVersion" value="2.0.31005.0" />
<param name="autoUpgrade" value="true" />
<param name="initParams" value="favColor=Green"/>
<a href="http://go.microsoft.com/fwlink/?LinkID=124807">
<img src="http://go.microsoft.com/fwlink/?LinkId=108181"
alt="Get Microsoft Silverlight"/>
a>
object>
type="application/x-silverlight-2"
width="400" height="200">
<param name="source" value="InitParamsTest.xap"/>
<param name="minRuntimeVersion" value="2.0.31005.0" />
<param name="autoUpgrade" value="true" />
<param name="initParams" value="favColor=Green"/>
<a href="http://go.microsoft.com/fwlink/?LinkID=124807">
<img src="http://go.microsoft.com/fwlink/?LinkId=108181"
alt="Get Microsoft Silverlight"/>
a>
object>
We now edit the App.xaml.cs to match our requirements like shown below.
public partial class App : Application
{
public App()
{
this.Startup += this.Application_Startup;
this.Exit += this.Application_Exit;
InitializeComponent();
}
private void Application_Startup(object sender,
StartupEventArgs e)
{
this.RootVisual = new Page(e.InitParams);
}
private void Application_Exit(object sender, EventArgs e)
{
}
}
{
public App()
{
this.Startup += this.Application_Startup;
this.Exit += this.Application_Exit;
InitializeComponent();
}
private void Application_Startup(object sender,
StartupEventArgs e)
{
this.RootVisual = new Page(e.InitParams);
}
private void Application_Exit(object sender, EventArgs e)
{
}
}
public partial class Page : UserControl
{
public Page(IDictionary<string, string> initParams)
{
InitializeComponent();
_TextField.Text = "My Favorite Color is: " + initParams["favColor"];
_TextField2.Text = "";
foreach (string key in initParams.Keys)
_TextField2.Text += key + ": " + initParams[key] + "\n";
}
}
The link to the Original Article......
{
public Page(IDictionary<string, string> initParams)
{
InitializeComponent();
_TextField.Text = "My Favorite Color is: " + initParams["favColor"];
_TextField2.Text = "";
foreach (string key in initParams.Keys)
_TextField2.Text += key + ": " + initParams[key] + "\n";
}
}
The link to the Original Article......
SilverLight- how to return an XML content from a distant server
I tried a lot and searched a lot to find out a method to return an XML to the silverlight application and wasted almost 3 hours on it . Then I found a way to solve the problem.The code for it is shown below.
It catches security errors if the distant URL needs a session variable to access the file.
If it doesnt need a session variable and data is of the correct format, it works properly.
private void RequestContent()
{
Uri address = new Uri(uri);
//You can supply this URI from a text box or hard code it.
WebClient webClient = new WebClient();
webClient.DownloadStringCompleted += new
DownloadStringCompletedEventHandler
(webClient_DownloadStringCompleted);
webClient.DownloadStringAsync(address);
}
void webClient_DownloadStringCompleted(object sender,
DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
MessageBox.Show(e.Result);
txtBlock.Text = e.Result.ToString();
}
else
{
MessageBox.Show(e.Error.ToString());
txtBlock.Text = e.Error.ToString();
}
}
See you alll.......
It catches security errors if the distant URL needs a session variable to access the file.
If it doesnt need a session variable and data is of the correct format, it works properly.
private void RequestContent()
{
Uri address = new Uri(uri);
//You can supply this URI from a text box or hard code it.
WebClient webClient = new WebClient();
webClient.DownloadStringCompleted += new
DownloadStringCompletedEventHandler
(webClient_DownloadStringCompleted);
webClient.DownloadStringAsync(address);
}
void webClient_DownloadStringCompleted(object sender,
DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
MessageBox.Show(e.Result);
txtBlock.Text = e.Result.ToString();
}
else
{
MessageBox.Show(e.Error.ToString());
txtBlock.Text = e.Error.ToString();
}
}
See you alll.......
Subscribe to:
Comments (Atom)