分類彙整: Silverlight

Network Security Access Restrictions in Silverlight

Reference from

[1]http://msdn.microsoft.com/en-us/library/cc645032(VS.95).aspx

[2]http://msdn.microsoft.com/en-us/library/cc197955%28VS.95%29.aspx

[3]http://blogs.msdn.com/b/silverlightws/archive/2008/03/30/some-tips-on-cross-domain-calls.aspx


What kind of classes require access permission?

WebClient and HTTP classes in System.Net namespace

Sockets classes in System.Net.Sockets namespace

UdpAnySourceMulticastClient and UdpSingleSourceMulticastClient class in System.Net.Sockets namespace

Why is there network security access restriction?

It provides extra security since it prevent Silverlight applications from initiating unauthorized connections. These conenctions may favour Denial of Service(DoS) attacks, DNS Rebinding attacks and Reverse tunnel attack.

What is used to enforce sercurity policy system in Silverlight?

Silverlight supports two types of security policy files:

  Flash policy file Silverlight policy file
Origin Adobe Flash Microsoft Silverlight
Classes that can use this file WebClient and HTTP classes in System.Net namespace WebClient and HTTP classes in System.Net namespace
Sockets classes in System.Net.Sockets namespace
File name crossdomain.xml clientaccesspolicy.xml

Either of the files should be placed at the root of the domain where the service is hosted.

HTTP redirects for the policy file are not allowed. A redirect for a policy file will result in a SecurityException of access denied.[1]

For the format of the policy file, please visit http://msdn.microsoft.com/en-us/library/cc645032(VS.95).aspx

Silverlight: Using ItemsControl and Binding

Reference from

[1]Microsoft Silverlight 4 Step by Step, written by Laurence Moroney, published by Microsoft Press

[2]http://msdn.microsoft.com/en-us/library/k166wx47(v=VS.100).aspx


What is data binding in ItemsControl?

The ItemsControl binds to classes that expose the IEnumerable interface. One such IEnumerable is the List<> class. Many others can be found in [2]. This form list that is restricted to containing any type.

How can ItemsControl bind to a class?

Example[1]

This example can be done on visual studio 2010 express.

1. Create a new Silverlight 4 project, for instance, SL4ItemsControlBindingEx

2. Add a contact list consisting 3 contacts. Add the following code in MainPage.xaml.cs

public void showContactList()

{

    List<String> contactList = new List<String>();

    contactList.Add("Peter Tel:1000203102");

    contactList.Add("Mary Tel:3406435112");

    contactList.Add("Tom Tel:6800203108");

}

3. Now we will add ItemsControl in the MainPage.xaml. Within the <Grid></Grid>, add the following code

<ItemsControl x:Name="itemsControlContact">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Height="23" HorizontalAlignment="Left" Text="{Binding String}" VerticalAlignment="Top" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

4. We will display the contactList in ItemsControl itemsControlContact. Add the following code after contactList.Add("Tom Tel:6800203108"); in showContactList()

itemsControlContact.ItemsSource = contactList;

5. Finally, showContactList will be invoked in void MainPage_Loaded(object sender, RoutedEventArgs e)

void MainPage_Loaded(object sender, RoutedEventArgs e)

{

    showContactList();

}

6. Press F5 to compile and see the result.

Silverlight: Using Webcam

Adopt from

[1]Microsoft Silverlight 4 Step by Step, written by Laurence Moroney, published by Microsoft Press

[2]http://www.silverlightshow.net/items/Capturing-the-Webcam-in-Silverlight-4.aspx


The ability to use webcam to capture and render video is a new feature in Silverlight 4.

Example[1]

This example can be done on visual studio 2010 express.

1. Create a new Silverlight 4 project, for instance, SL4WebCamEx

2. Add the following code in MainPage.xaml

<Grid x:Name="LayoutRoot" Background="White">

    <StackPanel Orientation="Vertical">

        <Rectangle Height="200" Width="300" x:Name="cam" />

        <Button x:Name="bStart" Content="Start Webcam" Click="bStart_Click" />

    </StackPanel>

</Grid>

The rectangle named cam will display the video capture from the webcam. The start of the capture is triggered by the "Start Webcam" button.

3. Add the following code in MainPage.cs

private void bStart_Clickobject sender, RoutedEventArgs e)

    //ask for capture permission from the client

    bool allowCapture = CaptureDeviceConfiguration.RequestDeviceAccess();

    if (allowCapture)

    {

        CaptureSource cs = new CaptureSource

        {

            VideoCaptureDevice = CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice()

        };

/*//Based on [2], the above segment is equivalent to

CaptureSource cs.VideoCaptureDevice = CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice();

*/

        VideoBrush vidBrush = new VideoBrush();

        vidBrush.SetSource(cs);

        cam.Fill = vidBrush; //Going to paint display cam by the webcam captures

        cs.Start(); //Start the webcam

    }

}

The above code handles the click event from "Start Webcam" button.

4. Press F5 to compile and run the appplication. A dialog box as shown below will pop up to ask for the access permission of the camera.