SilverBites 1: Adding metadata to Xaml in design-time

code, silverbites, silverlight, tutorial, video — Szili @ 6:22 pm

This little lolcat will show you a cool feature I discovered recently.

Using its ‘Tag’ property you can add metadata to a FrameworkElement (so practically every graphical element Silverlight provides) and read it out in code as a string.

Even better, you can set Tag property in design-time with Expression Blend support!

This is a start of my new project, SilverBites. It’s a series of bite sized video tutorials on Silverlight 2 with a maximum length of 90 seconds.
I’m pretty microphone shy yet, but watch my alterego in the coming parts!

Data Binding in Silverlight and XamlParseException

code, silverlight — Szili @ 2:16 pm

Playing with data binding I had the System.Windows.Markup.XamlParseException whenever I ran my app.

It turns out the problem was putting a binding statement into a <Run … /> element.
Silverlight supports data binding only on FrameworkElements and the Run class contrary to the Textblock is not a descendant.

Beware the decimal separator

code, silverlight — Szili @ 3:18 pm

The clash between decimal separators, double values and application culture already made Point type a victim. Today I found abound another one..

I had an iframe positioned above my silverlight control. Its dimensions was controlled from C# managed code (with the use of System.Windows.Browser library).
Or at least it should have been.

ERROR:

No matter I gave the right double values to set the iframe’s width, it just didn’t changed:

Iframe.SetStyleAttribute("width", value.ToString() + "px");

SOLUTION:

After a few rounds of debugging I found the solution. The problem was caused by the decimal separator being a comma instead of a point.

The fix is the same as with the Point type:

Iframe.SetStyleAttribute("width", value.ToString(System.Globalization.CultureInfo.InvariantCulture) + “px”);

Fear Blend 2 and keep your Xaml dry

code, silverlight — Szili @ 10:15 am

Okay, I like Blend 2, but it has its tricks..

A control’s xaml when was loaded in its constructor threw a Catastrophic failure.

I designed the control in Blend 2 without touching the xaml’s source. It was a pretty simple graphic, four rectangles in a canvas.
All had unique names so the problem wasn’t related to namescopes.

To cut a long story short it occurred to me, that one rectangle had three spaces after the value in its height property. Fixing this solved the problem.

The full Exception info after the jump.

(more…)

Loading libraries run-time

code, silverlight — Szili @ 12:16 pm

I’m working on an application with the ability to upload and run Silverlight 1.1 gadgets. This means my program needs to be able to load assemblies in run-time.

After several tests and an e-mail exchange with an MS Evangelist it seams like it’s impossible.

UPDATE: Here is how I managed to do it. The trick is that the xaml downloads and loads its assembly automatically.
void DownloadXamlFile()
{
GadgetXamlDownloader.Completed +=
new EventHandler(GadgetXamlDownloader_Completed);
GadgetXamlDownloader.Open(
“GET”, new Uri(“./Gadgets/TestGadget/Page.xaml”, UriKind.Relative));
GadgetXamlDownloader.Send();
}


void
GadgetXamlDownloader_Completed(object sender, EventArgs e)
{
ObjectFromXamlString(GadgetXamlDownloader.ResponseText);
}


void
ObjectFromXamlString(string XamlString)
{
DependencyObject Gadget = XamlReader.Load(XamlString,true); //Namescoping is turned on
Gadget.SetValue(
Canvas.TopProperty,this.Height/2-(Gadget as Canvas).Height/2);
Gadget.SetValue(
Canvas.LeftProperty, this.Width / 2 - (Gadget as Canvas).Width / 2);
this.Children.Add(Gadget as Visual);
}

My old failed tests after the jump, they might be useful.

(more…)

NoteToMyself: Xaml stream is null

code, silverlight — Szili @ 11:39 am

If your Xaml stream doesn’t load, chances are:

  • Your Xaml reference isn’t pointing correctly to the Xaml file.
  • Your Xaml is not valid.
  • Your Control is not set to be an Embedded Resource.
  • InitilazeFromXaml(…) is not in the lowest controls’ contructor in your inheritance tree.

NoteToMyself: Frameworkelement and System.ArgumentException

code, silverlight — Szili @ 9:06 am

Just a reminder to myself.

  • Something is wrong with the names
  • You are trying to add the Frameworkelment as a child, but it’s already a child of someone else, and you haven’t removed it.
  • It’s null.

Silverlight Loaded Event and Object Initializers

code, silverlight — Szili @ 12:38 pm

Got into a tricky situation today.

I used the new C# Object Initializer “syntactic sugar” to initialize my object:
MyType MyObject = new MyType{MyBool=true};

At the same time I used the property as a condition in MyObject.

First I used it in the constructor, but it didn’t work since the new value of the property was given only after the constructor ran (pretty logical).

Then I used it in the Loaded Event, but the event fires way after I need the changes made by MyBool.

Three things learned:

  • The Object Initializer give value to the properties after the object is instantiated.
  • I thought the Loaded event fires right after the object is instantiated. It’s not.
  • I’m still a Noob.

(The solution was to give parameters to my constructor and give value to the property through it. But this is not the important part.)

Giving value to a Point

code, silverlight — Szili @ 6:05 pm

Point class constructor problems can be caused by regional settings. Dot vs. comma stuff: http://silverlight.net/forums/t/4142.aspx

I’m still hopeless!!!

Solution 1
Can’t really call it a solution. Just inverses the problem..

double x = Convert.ToDouble("11,5");
double y = Convert.ToDouble("0,44");
Point TestPoint = new Point(x,y);

Solution 2
Didn’t really work for me either. I guess it’s my fault, but i had problem with constructing Point from results of calculations:

http://www.codeproject.com/csharp/floatparse.asp

Solution 3
My solution. It’s cool until you use it to create a new point. But as you try to give value to a Point property, like GradientOrigin it fails.

double x;
string _x = (2.0 / 3.0).ToString(System.Globalization.CultureInfo.InvariantCulture);
x = double.Parse(_x, System.Globalization.CultureInfo.InvariantCulture);
double y;
string _y = (1.0 / 2.0).ToString(System.Globalization.CultureInfo.InvariantCulture);
y = double.Parse(_y, System.Globalization.CultureInfo.InvariantCulture);
Point TestPoint = new Point(x,y);

Extensive tests with September refresh

(with no results…)

(more…)

Silverlight Emotional Handbook

code, silverlight — Szili @ 8:20 pm

I met her. I fell in love with her. Now I need a frickin’ handbook for her emotional life…

Silverlight Error Codes Explained!
Comprehensive error collection with solutions!

“Requested registry access is not allowed.”
When trying to create a new C# Silverlight project in Visual Studio 2008 Beta 2 under Windows Vista.

  • Turning UAC back on didn’t work.
  • Vista re-install did work (if you call this a solution..).

It happened again after installing Visual Studio 2008. When trying to create a new c# poject I got the error message saying “Requested registry access is not allowed.” But this time i did found a solution:
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2400179&SiteID=1
The only trick is to rename the vb* to cs* files in the cmd file.

Point class hell.
Point class problems caused by regional settings. Dot vs. comma stuff.

Silverlight Loaded Event and Object Initializers

Frameworkelement and System.ArgumentException

Xaml stream is null

Children.Add Freeze

If the app freezes on Children.Add check the objects Load events in the code and Load triggers in its Xaml.

System.Exception after Storyboard.Begin()

You haven’t added the Storyboard to a Children collection.
The Storyboard is not a descendant of the Page.

Control Width and Height

Control inherits Height and Width from FrameworkElement, but is not aware of the Control implementation root concept.

An important line I believe. It resolves a great amount of confusion in me. Source

Silverlight control over silverlight control

Every control should have isWindowless:"true" property set in its createSilverlight() function in the javascript files.
The ones with transparent background should have their background property set to transparent: background: '#00000000'
Then you can set their order in css with z-index.

Setting properties of objects created from xaml.

You can not set the value of a property which wasn’t declared  in markup.
For example, setting the Storyboard.Target of a DoubleAnimation in codebehind will throw a CatastrophicFailure if the property wasn’t present in the markup.

Reading in Xaml throws Catasthrophic failure

Beware the decimal separator

Dashed line in Xaml

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.
(c) 2010 Szili | powered by WordPress with Barecity