- Custom controls is used to extend the functionality of existing controls and even create new controls with new functionalities.
- Creating custom controls is a great way to make Silverlight application more rich and unique
- Once a custom control is built we can reuse it anywhere in other applications too.
- Create a Silverlight Class Library Project in Visual Studio 2008.
- Because this will be a Control Library we've to add a reference to System.Windows.Controls assembly. This assembly will have 1.0.0.0 as version number for Silverlight 2.0 Beta1
- The next step we will add XmlnsDefinitionAttribute to our control Assembly
- Select the generic.xaml in the Solution Explorer and set the properties to get this file embedded in a format we need
- You've to delete the value of the Custom Tool property and set Build Action to "Resource".
- Write the coding in the Xaml file and the design the media button.
- To test this control we have to create a Silverlight Application Project. In Solution Explorer, right click the Solution's node and add a new Project to the Solution of type Silverlight Application.
- For Silverlight Application Visual Studio asks us about the testing method we like to use for this Silverlight Application. For now, a test HTML page will be fine for us.
- To use the control in our Test Application we've to add a reference to the Control Library Project.
- Open the page.xaml and write the caoe for calling the media button and run the application.
- Then a .dll file will be created. This file can be used to call the media button in other applications.
Friday, June 19, 2009
Developing a Custom Control for Silverlight 2.0
How to Store and Retrieve Application Settings Using Isolated Storage
•This class implements a dictionary that is designed to store name/value pairs.
•Like all data stored in isolated storage using Silverlight 2, application settings are stored locally on the user's computer
To create an application settings dictionary and add an entry
•Create a new instance of the IsolatedStorageSettings class
•Call the Add method to add a key/value pair into the dictionary.
To retrieve a value from the application settings dictionary
•Reference the key and assign a new value
To delete an item in the application settings dictionary
•Call the Remove method and pass the key of the item to remove, as shown in the following example
•appSettings.Remove("email");
Basics of the Security Policy System
• The existing crossdomain.xml policy file used by Adobe Flash.
•This policy file can only be used by the WebClient and HTTP classes in the System.Net namespace.
•A Flash policy file must allow access to all domains to be used by the Silverlight 2 runtime.
Silverlight policy file
•The Silverlight policy file that can be used by the WebClient and HTTP classes in the System.Net namespace and also by the sockets classes in the System.Net.Sockets namespace.
• This policy file has a different format than the Flash policy file.
Two primary means for networking applications
Potential Networking Threats
Denial of Service (DoS) attacks
• A large number of remote hosts are used to attack a target site so that the target is unable to service valid requests.
DNS Rebinding attacks
•Use DNS to force a remote host to rebind a trusted host name (site of origin) to a victim’s IP address, thus allowing access to a host other than site of origin.
Reverse tunnel attack
•Use a remote client’s outgoing connection as a back tunnel to the client’s private network.
Sockets security
security considerations and restrictions when communicating with sockets
•Working with Sockets Describes the managed implementation of the sockets networking interface.
•Network Security Access Restrictions in Silverlight 2 Describes sockets and the security policy system in Silverlight.
HTTP security
• Cookies and authentication are sent with all HTTP requests.
•for security reasons, Silverlight has restrictions on the types of messages that can be sent and received with HTTP.
•HTTP Communication and Security with Silverlight Describes several HTTP communication scenarios and how you can enable these scenarios.
•Network Security Access Restrictions in Silverlight 2 Describes how to use the security policy system in Silverlight and describes the policy file formats that are supported.
•URL Access Restrictions in Silverlight 2 Describes how the Silverlight runtime restricts access to certain classes of URLs.
•Making a Service Available Across Domain Boundaries Describes the mechanisms that Silverlight supports to opt-in to cross-domain access.
•Security Considerations for Service Access Describes security considerations when using Silverlight with Web services
Application security
•And whether you allow calls between the HTML DOM and the Silverlight plug-in.
Security in silverlight
•Application security
•HTTP security
•Sockets security
How toPerform Isolated Storage Tasks
•Creating a file system of directories and files.
•Writing to a file and then reading it.
•Deleting a file.
•Finding directories.
•Requesting an increased quota size.
•Removing the store.
Isolated Storage Overview
•The isolated storage data compartment is an abstraction, not a specific storage location.
It consists
•one or more isolated storage files,
•called stores, which contain the actual directory locations where data is stored.
•Stores usually reside on the client but an application can also use isolated storage on a server.
•Silverlight does not support roaming profiles that enable information to travel with a user.
Error Handling Using the Default OnError Event Handler
•When you leave the onError parameter in a CreateObject call unspecified or null, the default handler function defined in Silverlight.js is invoked
The OnError handler is used for
•XAML parsing errors
• errors that occur during load
•run-time errors
•errors in synchronous method calls that are not handled in a try/catch block
•asynchronous error events that do not have an attached event listener.
Error Handling
• In order to handle exceptions that originate from your application's user code, you can register a handler for UnhandledException
• Platform code exceptions, as well as exceptions that you choose not to handle with UnhandledException, are passed to the native/unmanaged error mechanism in the Silverlight plug-in.
•At this level, you can choose to process the errors using the OnError handler that is specified during plug-in instantiation
•Some errors and exceptions prevent further coding or scripting to Silverlight
•Others are considered less serious and allow you to continue coding or scripting
data binding diagram and its explanation
•The source object that contains the data that flows between the source and target. The source can be any CLR object.
•The target UI property that displays and possibly allows user changes to the data. The target can be any DependencyProperty of a FrameworkElement.
•The direction of the data flow. The direction is specified by setting the Mode property on the Binding object.
•The optional value converter that applies to the data as it is passed. The value converter is a class that implements IValueConverter.
Data Binding in silverlight
•The way data is displayed is separated from the management of the data
•. A connection, or binding, between the UI and a data object allows data to flow between the two.
•When a binding is established and the data changes, the UI elements that are bound to the data can reflect changes automatically.
• Similarly, changes made by the user in a UI element can be reflected in the data object.
Thursday, June 18, 2009
Cryptographic Services in Silverlight
• create hashes and digital signatures, and provide random number generation
Cryptographic Signatures
•Cryptographic digital signatures use a Hash-based Message Authentication Code (HMAC) to provide data integrity.
•When you sign data with a digital signature, someone else can verify the signature,
• and can prove that the data originated from you
•and was not altered after you signed it
Commonly Used Collection Types
• List Collection Types
•Dictionary Collection Types
• Queue Collection Types
•Stack Collection Types
• Bit Collection Type
Collections and Data Structures
• Instead of writing separate code to handle each individual object, you can use the same code to process all the elements of a collection
•To manage a collection, use the Array class and the System.Collection classes to
•add
•remove &
• modify
either individual elements of the collection or a range of elements.
•An entire collection can even be copied to another collection
•Some Collections classes have sorting capabilities, and most are indexed.
•Memory management is handled automatically, and the capacity of a collection is expanded as required.
•Synchronization provides thread safety when accessing members of the collection.
•Some Collections classes can generate wrappers that make the collection read-only or fixed-size.
• Any Collections class can generate its own enumerator that makes it easy to iterate through the elements
•System.Collections.Generic
•System.Collections.ObjectModel
are the namespaces used for the collections
Friday, June 12, 2009
Creating a Silverlight plug-in
The structure of a .xap file
Silverlight supported protocols&logfields
•http
•https
•mms (You can use an mms URL, but it will fall back to http.)
•rtsp (You can use an rtsp URL, but it will fall back to http.)
•rtspt (You can use an rtspt URL, but it will fall back to http.)
Supported Fields of Server Logs
•A server administrator might want to get information about Silverlight client connections to their servers, particularly related to streaming media.
•This can be done by accessing logs on the server; however, the information in these logs is determined by what information the Silverlight client sends to the server
Silverlight supported protocols&logfields
The following Web protocols/schemes are supported:
•http
•https
•mms (You can use an mms URL, but it will fall back to http.)
•rtsp (You can use an rtsp URL, but it will fall back to http.)
•rtspt (You can use an rtspt URL, but it will fall back to http.)
Supported Fields of Server Logs
•A server administrator might want to get information about Silverlight client connections to their servers, particularly related to streaming media.
•This can be done by accessing logs on the server; however, the information in these logs is determined by what information the Silverlight client sends to the server
Silverlight The Layout Model
That said, there are times when you want to do something the existing controls just don?t provide, and familiarity with how Silverlight lays out controls can be both fascinating and essential.
Steps for Integrating DRM in Silverlight
• Set up the necessary server infrastructure to serve up DRM protected content.
•Point to this protected content from your Silverlight application using MediaElement.
•Handle expected errors (for example, when the end user does not allow DRM content).
•If needed, subclass the LicenseAcquirer class to gain custom business logic.
Limitations When Using Silverlight DRM
•Silverlight does not support all the file types that PlayReady does. Silverlight DRM only supports Windows Media Audio (WMA), Windows Media Video (WMV), and PYA/PYV files for PlayReady.
•PlayReady license can specify rights and restrictions that define exactly how the content may be used and under what conditions; however, when using Silverlight DRM the only license type available is a non-persistent, play-once license
•No license caching
Silverlight DRM Architecture
•Silverlight Application :A website's in-browser application for interacting with media. A Silverlight-based player application usually contains transport controls (play, stop, rewind, forward) as well as content navigation and discovery.
•Silverlight Plug-in :The Silverlight runtime installed on a user's computer. The Plug-in runs within the browser powering a website's Silverlight Application.
•DRM Client :A part of the Silverlight Plug-in that provides the secure content protection functionality
What is DRM
•Silverlight DRM is, naturallly enough, a cross-platform version of the PlayReady client used only by the Silverlight browser plugin
• It is compatible with Windows Media DRM 10 content, and will support live and on-demand streaming as well as progressive downloads
Digital Rights Management
•And the server/client interactions needed to facilitate those processes
ØThe end user attempts to play some DRM protected content in a Silverlight application that is stored on the distribution server
ØThe Silverlight client downloads the content and the header.
Client server communication over Internet
Designing Grid Control Using Blend
•If you do not see the Grid panel in the Toolbox, you can locate it by right-clicking the arrows in the bottom right corner of each other tool in the Toolbox to see a default list of the controls that are assigned to that spot in the Toolbox
•Under Objects and Timeline, make sure that your new Grid ([Grid]) is selected
•If it is selected, the background behind the element name should be highlighted.
•Click the Properties tab to view the Properties panel
•Under Layout in the Properties panel, you can adjust the Margin properties to fine tune the Grid layout
•In the Toolbox, click Selection .
•On the artboard, move the mouse pointer over the thick blue ruler area at the top of MainGrid.
•An orange column ruler will follow your mouse pointer and indicate where a new column divider would be placed should you click.
•Can Set the Width and Height properties to Auto
•Set the Margin property to the default (zero) by clicking the Property Marker
Silverliught2 Supported Media Formats
•SMPTE VC-1
WMV9 - now an open standard recognized by the Society of Motion Picture and Television Engineers (SMPTE) and shipping in all Blu-Ray and HD-DVD drives.
•Input: ISO/MPEG Layer-3 compliant data stream
•Channel configurations: mono, stereo
•Sampling frequencies: 8, 11.025, 12, 16, 22.05, 24, 32, 44.1, and 48 kHz
•Bit rates: 8-320 kbps, variable bit rate
•Limitations: "free format mode" (see ISO/IEC 11172-3, sub clause 2.4.2.3) is not supported
Tools
•Microsoft's tool Expression Encoder 1.0 can convert A/V files to the Silverlight format
Audio and Video Overview
•Adding media to a page is as simple as adding a MediaElement to your markup and providing a Uniform Resource Identifier (URI) to the media to play.
• The following example creates a MediaElement and sets its Source property to the URI of a video file.
•The MediaElement begins playing when the page loads
TOOLTIP CONTROLIN SILVERLIGHT
•The SetToolTip and GetToolTip static methods are used to set and get the tooltip of a control.
•The following code snippet creates a ToolTipService.ToolTip for a control.
•This sample code shows how to display an image within a tooltip in Silverlight:
Tab Control Control In Silverlight
•The tabs can contain pictures and other controls.
To add TabControl
•drag a TabControl from the Toolbox to your XAML file inside the Grid tag
•following code sets the height, font, font size, and background color of the tab control.
Adding Tab Items
•The
•The Header property of TabItem represents the header text of the header
Stack control
•By using the control you can stack controls on top of each other vertically or display them horizontally.
•The StackPanel will place its children in either a column (default) or row.
•This is controlled by the Orientation property. A vertical StackPanel can have its width and height specified;
•By using the StackPanel you can arrange controls on an interface without defining absolute positions for each control.
•if the height is not specified it will take up as much space vertically as is required to fit all of its children.
•An unconstrained horizontal StackPanel will be as wide as necessary to hold all of its children, and as tall as the tallest child. :
Environment setup for Silverlight 2
Software needed to work with Silverlight application
•An Editor to create an XAML and managed code
•Moreover, SDK
A designer needs to install some designer tools and a development environment which work with XAML.
How to Set Development Environment
•To go with the following I advice you to go first with following link :
http://www.silverlight.net/GetStarted
Following are the steps direct how to Set up the Environment
•Install Visual Studio 2008 with Visual Web Developer Feature
•Install Silverlight Plug-in: download Silverlight Plug-in from http://www.microsoft.com/silverlight and Install Silverlight2, make sure you have uninstalled all previous versions of silverlight
How to Install Silverlight SDK
•Goto : http://www.silverlight.net/GetStarted
•Installation of Silverlight SDK depends upon your environment like if you have Visual Studio 2008 installed or not on your computer.
different Types of Silverlight Styling
•Style Element
•Applying Styles
•Overriding Styles
•Expanded Setters
•Missing WPF Features
Silverlight Deployment Guide
• The guide describes the system requirements and deployment methods
•And the techniques to maintain and support Silverlight after deployment.
•To install Silverlight successfully, you must carefully plan your deployment processes and strategies
diff deployment policies
•Microsoft Systems Management Server
•Group Policy
•Manual Options
Page.xaml Vs App.xaml
application.
•App.xaml and App.xaml.cs – The App.xaml is required to configure your Silverlight application. You can also declare resources that will be available in all pages of your application.
App.xaml
•App.xaml is a file used by Silverlight applications to declare shared resources like brushes, various style objects etc.
• The code behind file of app.xaml is used to handle global application level events like Application_Startup, Application_Exit and Application_UnhandledException. (Similar to Global.asax file for ASP.NET applications)
Page.xaml
•When you create a Silverlight project using Visual Studio 2008, it creates a default xaml file called "Page.xaml".
•This is just a dummy start page created by Visual Studio and it does not contain any visible UI elements
wpf VS Silverlight
Ships as part of the .NET Framework (version 3.0 and onward)
Runs as Windows application or as web "browser application" (called XBAP, for "XAML Browser Application"). Note that XBAPs run only in Internet Explorer with .NET 3.0 and in both Internet Explorer and Firefox with .NET 3.5.
Runs on Windows machines only (Windows XP, Windows Server 2003, Windows Vista, and Windows Server 2008)
Richest functionality, including 3D graphics
Silverlight:
Ships independently
Runs in web browsers only (Internet Explorer, Firefox, Safari)
Runs on Windows or Mac operating systems (also on Linux via Moonlight, which is an open source implementation of Silverlight based on Mono)
Functionality is a subset of WPF's feature set
WPF is a more mature technology and was designed with a richer feature set. It also has the advantage of being able to run in a browser or as an installed Windows-Form-type app.
Silverlight has a broader reach. You can access Silverlight from many operating systems and web browsers.
The most important reason to choose one over the other should be based on the intended audience for the application. For example, if a corporation is designing an application for internal use only and every employee has Windows XP as the company standard OS, then go with WPF to leverage the richer feature set. If a corporation is designing an external-facing website, then Silverlight is the better choice because potential customers can access
WPF advantages over Silverlight
v Full access to all the Windows API and the full .NET API
o E.g. TCP/Binary WCF support
v Powerful printing support
v Desktop or browser hosted with Customizable Sandbox
o XBAP works on IE and Firefox
v Offline support
v Easy integration with other UI technologies
o WPF controls can be hosted in Office, WPF app can call Office API
o WPF controls can be hosted in WinForm
o WPF apps can host WinForm and ActiveX controls
v Proven technology (3.0, 3.5, 3.5 SP1)
v Virtualization (data or visuals) for large data (from 3.5 sp1)
v Large third party controls selection
v Hardware accelerated, 3D
Silverlight advantages over WPF
Small runtime install 4 MB, 1 min, no reboot
o Default auto updating (no admin rights needed in Vista for runtime updates)
o Can be easily deployed even on login script or GPO
No app deployment
o App is cached in browser cache
Cross Platform, Cross Browser
o Windows 2000, XP, Vista, 2003, 2008, Mac OS
o Linux through partnership with Novell
Easier to learn
o Due to the reduced API subset
Can be integrated in current web pages and web based portals
o Is an island on the page
o Full access to the Browser DOM
o Fully scriptable from JavaScript
Share code and components between Intra and Internet solutions
In future it will run on Mobiles, CE
XAP FILE
•The file is actually a .zip file that contains all the files necessary for the application
•XAP is the file extension for a Silverlight-based application package (.xap). This file contains the compressed assemblies and resources of a Silverlight 2 application.
What is a .xap file?
•A .xap file is a Silverlight-based application package (.xap) that is generated when the Silverlight project is built.
• .XAP file contains the client DLL for the Silverlight application as extra DLLs of assemblies that you are using in your application.
•Just rename the .xap file to have a .zip extension and then you can open it up to see its contents.
Grid control
•It can have multiple children, and acts rather like a spreadsheet.
•It acts much like HTML's table tag and allows data and controls to be arranged in a tabular-style view.
•A row is the same height and a column is the same width across the entire Grid,
•But elements can be made to span multiple cells
•.Cells can contain more than one item.
•The placement of an element in the Grid is specified using attached DependencyProperties
• Grid.Row,
• Grid.Column,
•Grid.RowSpan
• Grid.ColumnSpan.
What is layout controls
• Layout controls that arrange items horizontally or vertically and also contain other layout groups with different orientation.
•Their main purpose is the positioning and arranging of their child controls
• This architecture allows you to build complex and flexible Layouts using simple elements
•Arranging controls on a user interface in a flexible manner is key to building successful applications.
Dependencies Properties
• A property that is backed by the Silverlight property system is known as a dependency property
•Simply put, a Dependency Property is like any other property but can hold a default value, with built in mechanism.
Dependency Properties Features
•Root class must be inherited from UIElement or DependencyObject
(E.g.., Controls, User Controls, Windows (WPF), or any class derived from UIElement (Button, List, Slider etc.))
•Dependency Properties have to be registered using DependencyProperty.Register method.
•Since DependencyProperty.Register is a static (C#) method, all dependency properties should also be static/ fields,
•also the property changed callback methods should be static/methods.
•Any property must be a dependency property if the property is being binded to.
•By convention, all Dependency Property fields should include the word “Property” at the end of the property name (
e.g., for property Value, the Dependency Property name should be ValueProperty
Creating Rich, Dynamic User Interfaces with Silverlight 2
–Re-use
–Modularization
–Encapsulation
•Same concept as in WPF
•No more need for "InitializeFromXaml()", KeyDown Event can now be handled on any component level, not just the application level like before
•Data binding is very simple in Silverlight
• flow layout is probably even sweeter than databinding.
•Accessibility of controls
–Keyboard tab, focus, directional navigation
–Screen reader support based on UIAutomation (check Mark Rideout's videos)
•Custom Control, motivations
–For default L&F
•add generic.xaml file
•set BuildAction to Resource
•remove CustomTool build action
•add ResourceDictionary with default L&F for the Custom control.
Canvas control
•It was present in Silverlight 1.0
•It lets its children be as big as they want to be, and positions them according to the Canvas properties
•Canvas defines a area within which you can place other controls by specifying the x and y coordinate position
•It is possible to overlap multiple controls within a canvas
Canvas Properties
•Canvas exposes several properties such as
•Name,
•Background,
•Cursor,
•Height, Width,
•HorizontalAlignment, VerticalAlignment,
•Opacity,
•OpacityMask,
•RenderTransform and Visibility
Border control
•Enables you to add borders to other controls
•It is a control that enables you to draw boxes in your Silverlight application,
•allows you to add other Silverlight controls directly into border
•This is a much more convenient way to wrap elements in a box than trying to manually position everything in a rectangle on your page
•Properties:
CornerRadius property
•This property makes it very easy to round the corners of your Border box