Archive for March, 2009

Flash XML Import code

This is the code for using the custom class ‘xmlloader’ – sorry this ones not mine so its private.

var _xml:XML;

//Load XML
var _xmlLoader:XMLLoader = new XMLLoader("archive.xml");
_xmlLoader.addEventListener(Event.COMPLETE, evtXMLLoaded);

function evtXMLLoaded(e:Event):void
{
_xml = _xmlLoader.xml;
setupTitles();
setupPlayer();
}
//--------

Calls to the XML objects can then be made in the following way

_videoSource = _xml.source;
_slidePath   = _xml.sliderooturl;

Playing Sound in Silverlight

Placing a media element in Blend and controling it’s playback is pretty simple but sometimes you just want to do it all in code.

using System.Windows.Resources;

MediaElement m = new MediaElement();
StreamResourceInfo sri = Application.GetResourceStream(new Uri("HBO_Usercontrols_XAML;component/Takeofff.mp3", UriKind.Relative));
m.SetSource(sri.Stream);
LayoutRoot.Children.Add(m);
m.Play();
VisualStateManager.GoToState(this, "ShowButtons", true);

Cool C# Stuff

Begining to get to grips with the basics of C# so I thought I post a few reminders for myself.

UserControl Properties. These are just properties of a method but in Silverlight you can think of them as properties of any usercontrol.  For example you might have a button that has a ‘simple mode’ that switches of the animation and sounds.

private bool _simpleMode = false;

public bool SimpleMode
{
set { _simpleMode = value; }
get { return _simpleMode; }
}

You can then query _simpleMode before playing animations or sounds.

Resquest as syntax. Sometime you want an element as a certain class (elements can be requested as a class they inherit from).  The neatest way to do this is with the syntax below.

FrameworkElement fe = (FrameworkElement)BriefInfoUserControl;
VisualStateManager.GoToState((Control)fe, "Hide", false);

This requests a user Control as a FrameworkElement and then requests it again as a Usercontrol. Pointless but a good example 🙂

Dynamic Silverlight Clipping Masks

Here’s a quick way to set a dynamic clipping mask in Silverlight. Quite proud of this.

The .Clip property is common to lots of objects so this can be adapted to Canvases or Button, etc.

public void SetDynamicClipRect(FrameworkElement Sender, String ObjectToClip)
{
Grid c = Sender.FindName(ObjectToClip) as Grid;

Double aw = c.ActualWidth;
Double ah = c.ActualHeight;

RectangleGeometry rg = new RectangleGeometry();
rg.Rect = new Rect(0, 0, aw, ah);

c.Clip = rg;
}

Silverlight X and Y

This is good to keep in mind for alsorts of things but X and Y properties being the prime example.

// Add these public variables to all your User Controls to position
// them using the more familiar X and Y properties!
public Double X
{
get { return (double)this.GetValue(Canvas.LeftProperty); }
set { this.SetValue(Canvas.LeftProperty, value); }
}

public Double Y
{
get { return (double)this.GetValue(Canvas.TopProperty); }
set { this.SetValue(Canvas.TopProperty, value); }
}

Silverlight Alert

Sometimes it’s useful just to have a simple alert pop-up box for debugging. Well it’s possible to do in Silverlight.

using System.Windows.Browser;

string myString = currentState.ToString();
HtmlPage.Window.Alert(myString);

CSS Centre Code

This is great piece of code for creating a centred design without using minus margins.










html, body {
                height: 100%;         /* required */
}
body {
                text-align: center;   /* horizontal centering hack for IE */
                padding: 0;           /* required to "hide" distance div */
                margin: 0;            /* required to "hide" distance div */
}
div#distance {
                margin-bottom: -300px; /* half of content height */
                width: 1px;           /* required to "hide" distance div */
                height: 50%;          /* required */
                float: left;          /* required */
				background-color:#9F0;

}
div#wrapper1 {
                position: relative;   /* positions content on top of distance */
                text-align: center;     /* horizontal centering hack for IE */
                height: 600px;         /* required - desired height */
                width: 700px;     /* cosmetic */
                margin: 0 auto;       /* required */
                clear: left;          /* required */
                background-color: #993300;
}

    


  <div id="distance"></div>
  <div id="wrapper1">Test</div>