Silverlight is likely to be the main development platform for Windows Phone OS.
-
Recent Posts
Recent Comments
Archives
Categories
Meta
Silverlight is likely to be the main development platform for Windows Phone OS.
Silverlight integration is already included in SharePoint 2010 – when you try to create a new list, library, discussion board or a simple page without Silverlight installed you will find the traditional SharePoint Create page. [SharepointMonitor.com]
Silverlight’s HtmlBrush is an alternative to the WebBrowser control for displaying HTML content in Silverlight app. There are two primary differences between the HtmlBrush and WebBrowser controls :
The below sample shows how to paint a simple rectangle with the HtmlBrush:
You can render HTML content with HtmlBrush, too. The following XAML snippet ‘paints’ a Rectangle with an HTML page.
<WebBrowser Name=”demoWB” Source=”/silverlightace.htm” />
<Rectangle Canvas.Left=”1″ Canvas.Top=”20″ Height=”100″ Width=”100″>
<Rectangle.Fill>
<HtmlBrush SourceName=”demoWB” x:Name=”demoBrush” />
</Rectangle.Fill>
</Rectangle>
Silverlight is still a maturing technology not suitable for every purpose. As such, hybrid applications are often developed where a Silverlight element is embedded in an HTML page. However, this was obviously not an option for out-of-browser Silverlight applications.
The new WebBrowser control allows for an HTML element to be embedded in a Silverlight application when the app is running out-of-browser. This is an important addition for several scenario’s – for example migrating an ASP.NET application to Silverlight can be done in stages as some of the ASP.NET code can be run and accessed from the WebBrowser control.
The WebBrowser control is a relatively simple one and contains the following properties:
The syntax for the WebBrowser control is shown below:
<WebBrowser x:Name="DemoBrowser" Width="1000" Height="800"></WebBrowser>
To set the URI use the following code:
DemoBrowser.Navigate(new Uri("URIstring"));
It is important to note that unless the Silverlight application has elevated trust the WebBrowser will only show HTML from the same domain as the app (a workaround this is to use an iframe to redirect to a page on another domain).
An alternative to the WebBrowser control is the HtmlBrush Control which paints static HTML into objects.
New to Silverlight 4.0 – StringFormat provides a simple solution for databinding in XAML and formatting the output. StringFormat is primarily used to format dates, numbers and currency values.
For example, formatting a date in a text box:
<TextBox x:Name=”dummyBox” Text=”{Binding Path=PublishedDate, Mode=OneWay, StringFormat=’MM-dd-yyyy’}”/>
TargetNullValue can be used to set the null value for the control:
<TextBox x:Name=”dummyBox” Text=”{Binding Path=QuantityOnHand, Mode=TwoWay, TargetNullValue=0}” />
Alternatively a FallBackValue can be set:
<TextBox x:Name=”dummyBox” Text=”{Binding Path=SomeBindingValue, Mode=TwoWay, FallbackValue=N/A}” />
The FallbackValue displays a value when the binding operation is unsuccessful, where the TargetNullValue helps provide a value when the result of the binding value is NULL.
Silverlight 4 introduces WebCam support which is probably the highlight of the Silverlight 4 release. The WebCam API is relatively simple to use, a webcam can be added, video captured and placed on the screen using the following procedure:
//access the WebCam
VideoCaptureDevice webcamObj = CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice();
//user confirmation clips
if (CaptureDeviceConfiguration.RequestDeviceAccess())
CaptureSource captureSourceObj = new CaptureSource();
captureSourceObj.VideoCaptureDevice = webcamObj;
VideoBrush videoBrushObj = new VideoBrush();
videoBrushObj.SetSource(captureSourceObj);
videoBrushObj.Stretch = Stretch.UniformToFill;
//start WebCam
videoBrushObj.Start();
//fill the rectangle with the video
this.MyVideo.Fill = videoBrushObj;
This code only works for a single WebCam but CaptureDeviceConfiguration.GetAvailableVideoCaptureDevices will return a list of all available webcams.
Silverlight 4 introduces a new API for accessing the clipboard through 3 static methods – GetText(), SetText(), ContainsText() . Using these methods is very simple, for example to test if there is anything on the clipboard and then paste it to a text box.
if (Clipboard.ContainsText())
{
txtText.Text = Clipboard.GetText();
}
To place text on the clipboard:
Clipboard.SetText(txtText.Text);
Silverlight can only access the clipboard from a user initiated action (such as from the mouse or keyboard) after performing this action the user is prompted to acknowledge the app is accessing the clipboard, this occurs only once per session.
In Silverlight 4 apps which run with Elevated Trust can access local files by using System.IO and related types that would otherwise be unavailable. This allows apps to access user files directly without recourse to the OpenFileDialog and SaveFileDialog classes. However, access is limited to the the MyDocuments, MyPictures,MyMusic, and MyVideos folders.
Use the local file access APIs in the same way as the desktop framework, but use the values of the System.Environment..SpecialFolder enumeration to create the paths. For example:
private void TestLocalFileAccess()
{
if (Application.Current.HasElevatedPermissions)
{
string myDocuments = Environment.GetFolderPath(
Environment.SpecialFolder.MyDocuments);
string filenamestr = "testfile.txt";
string pathstr = System.IO.Path.Combine(myDocuments, filenamestr);
if (File.Exists(pathstr))
{
string contents = File.ReadAllText(pathstr);
MessageBox.Show(contents);
}
}
}