Controlling Silverlight outside of the plugin - the .Content property

Posted on January 31, 2008
Filed Under 1.0 Javascript, Uncategorized | Leave a Comment

This one is pretty basic but it is useful and the info doesn’t seem to be easily accessible so I decided to post a quick example. You can use the content property of the plugin to gain access to elements in your XAML.

So say you want to have a link or button on your page control some aspect of your Silverlight App, like load a new Video into a MediaElement, you could do something like this:

function loadVideo(wmv_file) {    

   var videoRef
   var controlRef
   controlRef=document.getElementById("SilverlightControl")
   videoRef=controlRef.content.findName("videoElement")
   videoRef.source=wmv_file 

}

and then in you could just call the loadVideo function within an onclick, onmousdown, or in the href like so:

a href="javascript:loadVideo('my_video.wmv')" mce_href="javascript:loadVideo('my_video.wmv')"

Accessing the canvas in a Silverlight 1.1 User Control

Posted on January 30, 2008
Filed Under 1.1 Alpha, Application, Components, User Interface, XAML | Leave a Comment

When you create a Silverlight User Control from a piece of XAML, you expect to be able to modify the Canvas using the a standard property. After all, the control’s XAML is a Canvas.

Foo.xaml
<Canvas xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="100"
Height="100"
Background="CornflowerBlue"
>
</Canvas>

However, the Control interface doesn’t have an accessor for the Canvas. Looking at the control in the debugger, you find that the Canvas is buried down deep in the control in a non-public area.

Control Canvs

So, how do you access the Canvas?
Well, there’s a simple 3-step process.


public class Foo : Control
{
// STEP1: Save the canvas locally
private Canvas _TheCanvas;


// STEP2: Add a public property so that clients can access
// the canvas easily.
//
// Control.Canvas
public Canvas Canvas
{
get { return _TheCanvas; }
}

    public Foo()
{
// STEP3: Save the canvas as the control is being created.
System.IO.Stream s = this.GetType().Assembly.GetManifestResourceStream("UserControls.Foo.xaml");
_TheCanvas = this.InitializeFromXaml(new System.IO.StreamReader(s).ReadToEnd()) as Canvas;
}
}

That’s it. Now you can access the canvas of your control at any time.

Recently


Categories


Archives