Silverlight for Windows Phone 7

Silverlight is likely to be the main development platform for Windows Phone OS.

Posted in FAQ | Leave a comment

Getting Started with Silverlight in SharePoint 2010

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]

Posted in FAQ | Tagged | Leave a comment

Silverlight 4 – HtmlBrush

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 :

  1. WebBrowser can display “live” content, whereas the HtmlBrush only display static html content
  2. HtmlBrush can have transforms applied to it, so for visual effects this is the control of choice.

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>

Posted in FAQ | Tagged | Leave a comment

Silverlight 4.0 – WebBrowser Control

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:

  • Source – Gets or sets the URI which is rendered in the WebBrowser control
  • Navigate: – Sets the URI to be loaded in the WebBrowser control (identical to the Source property)
  • NavigateToString: – Displays on-the-fly generated HTML. This can be done using this method.

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.

Posted in FAQ | Tagged | Leave a comment

Silverlight 4.0 – StringFormat

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.

Posted in FAQ | Tagged , | Leave a comment

Silverlight 4 – WebCam Support

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:

WebCam Video Capture Procedure

  1. Add a rectangle plus a button.
  2. Get a handle on the video capture device using CaptureDeviceConfiguration.
  3. Request access to the WebCam using CaptureDeviceConfiguration.RequestDeviceAccess().
  4. After the user clicks Yes at the prompt , create a CaptureSource object and set the VideoCaptureDevice to the selected webcam.
  5. Create a VideoBrush and set its source.
  6. Start the WebCam.
  7. Fill the Rectangle.

Code for WebCam Video Capture

//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.

Posted in FAQ | Tagged | Leave a comment

Silverlight 4 – Clipboard Access

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.

Posted in FAQ | Tagged | Leave a comment

Silverlight 4 – Out of Browser Features

Silverlight 4 introduces several new features for out-of-browser applications that are not available to browser-based applications, namely:

  • New window features including run-time resizing and always on top capability.
  • Offline digital rights management (DRM).
  • HTML hosting with the WebBrowser control.
  • Popup notifications using the NotificationWindow class.
  • Elevated trust support.
Posted in FAQ | Tagged | Leave a comment

Silverlight 4 – Detecting Elevated Trust

Elevated Trust is a new feature in Silverlight 4 which allows Silverlight apps to be run with fewer security restrictions. To check whether an app is  running in elevated trust  use the HasElevatedPermissions property.

If an app requires elevated trust, users must first consent before installing the application for out-of-browser use. Thus you will not normally have to detect whether an app is running in elevated trust. Instead, you can simply check if an app is running outside of the browser by checking the Application.IsRunningOutOfBrowser property.

Posted in FAQ | Tagged , | Leave a comment

Silverlight 4 – Local File Access

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);
        }
    }
}
Posted in FAQ | Tagged | Leave a comment