Microsoft Silverlight - SPLessons

Microsoft Silverlight Working with Controls

Home > Lesson > Chapter 8
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Microsoft Silverlight Working with Controls

Microsoft Silverlight Working with Controls

shape Description

Microsoft Silverlight Controls are User Interface Elements which are used to design the application. Each and every Microsoft Silverlight controls have instructive conduct, for example, the way the secure lights while moving the mouse towards it and advances it before squeeze it, looking over and determination conduct of a summary box. In every one of the cases, the controls go past essential detectable quality.

shape Toolbox

All the controls resides in the ToolBox as shown in the below figure.

Placing the controls in the form

shape Description

By using xaml tags, we can place the controls in terms of form or web page. By default, every project have the one .xaml page. If you want to add more xaml pages then you have to follow below steps.

shape Step - 1

Right click on the Project which resides in the Solution Explorer and then Select Add->New Item as shown in the below figure.

shape Step - 2

Then, the following window will be open. Select the Visual C# and then Silverlight User Control and also assign the Name and click on Add button.

shape Step - 3

To place the controls in the terms of forms, simply drag the controls from Toolbox and drop the controls in the forms.

Setting the default .xaml page 

shape Description

If the project contains one or more xaml pages. Then, we have to set which page going to be execute. To set the default xaml page follow the below steps.

shape Step - 1

Go to the Solution Explorer and Double click on the App.xaml.cs as shown in the below figure.

shape Step - 2

Then, the following code file will be open. [csharp] using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; namespace SilverlightExample { public partial class App : Application { 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(); } private void Application_Exit(object sender, EventArgs e) { } private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e) { // If the app is running outside of the debugger then report the exception using // the browser's exception mechanism. On IE this will display it a yellow alert // icon in the status bar and Firefox will display a script error. if (!System.Diagnostics.Debugger.IsAttached) { // NOTE: This will allow the application to continue running after an exception has been thrown // but not handled. // For production applications this error handling should be replaced with something that will // report the error to the website and stop the application. e.Handled = true; Deployment.Current.Dispatcher.BeginInvoke(delegate { ReportErrorToDOM(e); }); } } private void ReportErrorToDOM(ApplicationUnhandledExceptionEventArgs e) { try { string errorMsg = e.ExceptionObject.Message + e.ExceptionObject.StackTrace; errorMsg = errorMsg.Replace('"', '\'').Replace("\r\n", @"\n"); System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(\"Unhandled Error in Silverlight Application " + errorMsg + "\");"); } catch (Exception) { } } } } [/csharp]

shape Step - 2

Change the RouteVisual which resides under the Application_Startup as shown below. [csharp]private void Application_Startup(object sender, StartupEventArgs e) { this.RootVisual = new Secondpage(); }[/csharp]

Opening the xaml page from another xaml page

shape Description

As a matter of course, we can open stand out xaml page. On the off chance that we need to open numerous xaml pages at once after pressing click button.Then, we need to raise the Click event of the knob furthermore we need to utilize the property called "Content". Write the following code in the click event. Then the page will be redirect to another page after button click. [csharp]private void Login_Click(object sender, RoutedEventArgs e) { this.Content = new Welcomepage(); }[/csharp]

Opening a particular xaml page

shape Description

After creation of new silver light project, you will get the form and also code. Write the following code in the App.xaml.cs file. [csharp] private void Application_Startup(object sender, StartupEventArgs e) { IDictionary parameters = e.InitParams; if (parameters == null) { this.RootVisual = new DefaultPage(); } else if (parameters["PageName"] == "Firstpage") { this.RootVisual = new Page1(); } else if (parameters["PageName"] == "Secondpage") { this.RootVisual = new Secondpage(); } else { this.RootVisual = new DefaultPage(); } } [/csharp]

Summary

shape Points

  • Microsoft Silverlight Controls - Are User Interface Elements which are used to design the application.
  • Tool box - Contain entire controls.
  • Placing controls in the forms - By using XML tags we can place the controls in terms of forms and web pages.
  • .Xaml pages - Execute multiple web pages.
  • Opening Xaml pages from another Xaml pages - Combining multiple Xaml pages.
  • Opening a particular Xaml pages - By using forms and web pages a particular Xaml pages can be opened.