Entries Tagged 'Silverlight 1.0' ↓

Free Silverlight Seminar in Dallas Texas Feb 18, 2008

Extra

Shawn Wildermuth, theADOguy (who should really rename himself the Silverlight guy) is giving a free seminar at the Microsoft office in Dallas on 2/18. Shawn is a very smart cool guy who knows a lot about a lot, and even more about Silverlight, so this should be a great event, and I'm told he's capping it at 32 seats so "don't say I didn't warn yer."

Pragmatic ADO.NET: Data Access for the Internet World (Microsoft .NET Development Series)
by Shawn Wildermuth

Read more about this title...

Webcast Follow-up - CreateFromXAML

Today's Webcast focused on factoring out repeated creation of objects in XAML (and their manipulation in the code behind) by creating objects dynamically using CreateFromXaml (This will be available for viewing as soon as the recording is processed).

The webcast began by showing this application, that uses animation to display changes in data over time.

It then explored the problems with this approach and offered a solution as shown in this video.

During the course of the webcast I mentioned a number of postings that you may want to read:

Thanks again.

Silverlight Cream for February 5, 2008 -2 — #193

Adam Kinney demonstrates building a very cool bar chart in 1.0, and Jesse Liberty shows setting properties on controls also in 1.0.

From SilverlightCream.com:
Silverlight 1.0 Bar Chart Tutorial
Adam takes off on something I keep bringing up to a friend of mine as an example that might be fun to do... dang, I should have done it ... all with source :)
Tips of the Day: Setting properties in custom controls in 1.0
Jesse answers a common forum question with this tip... cool stuff for getting the basics under your belt... good code examples!

Stay in the 'Light!

Twitter SLNews | Join me @ SilverlightCream | SL Web Articles | My Articles | My Tutorials | My Tooltips | SilverlightCream

Adam Kinney’s Too Great To Miss Tutorial on Making a Chart In 1.0

Do not miss Adam Kinney's excellent tutorial on creating a bar chart in Silverlight 1.0; it is brilliant, and it is a great staring point on working with XAML, JavaScript, JSON data and many other interesting aspects of Silverlight.

Also, while I have you, a quick apology; in my haste to put up today's Tip of the Day I posted a msg. that I would be late; brainlessly forgetting that said message would be sent out via RSS wasting everyone's time and bits. Won't happen again.

Tips of the Day: Setting properties in custom controls in 1.0

Got a nice international email today that said in part...

...things get more interesting it is a common scenario to write custom controls....I need to specify the parameters in code behind. Can I create custom xaml tags which are connected to the code-behind? ... I mean something like...

   <myButton width="100" height="20" Text="Hello World" />

Is this possibly for 2.0?

Assuming I understand this, it is no problem and you don't have to wait for 2.0. Fire up Visual Studio, create a new 1.0 application, clean out the XAML and the code behind and add this crude custom button:

CustomButtonXAML

In this XAML file we create a custom button that is composed of a canvas enclosing a rectangle (the shape of the button) and a TextBlock (to hold the text of the button).  Our initial settings make for a somewhat simple button,

FirstButton

Our goal is to set some of the properties of this button from within the Javascript. This we can do.

The first step is to get a reference to the Button and to its component parts, from within the JavaScript. We'll do that by using the FindName method of the content subpart of the Silverlight control that is passed as a parameter into the handleLoad event handler, fired when the Silverlight control is first loaded. 

In order to remember the names, I'll split the visual studio editor into two panes (a handy option) so that I can see the code and the code-behind at the same time. (There's a lot to go over in the next figure, we'll take it one piece at a time)

Step1Code

You can see the one to one relationship between the names of the Button and its parts in the XAML and the string passed to FindName in the JavaScript (highlighted in yellow).

(Remember that the first parameter passed to handleLoad, plugIn, is a reference to the Silverlight control itself. This reference can also be obtained by calling GetHost() on any Silverlight object.)

Once we have a reference to all three objects, we set up an event handler for the button. In yesterday's Tip of the Day I hooked up an event handler in the XAML file, and that could have been done here, but I prefer to have the event handlers "wired up" in  the code behind because that is where they are implemented, and that allows me to encapsulate the handler and its relationship to the object in one place (if I change the handler's name, for instance, I only have to update one file).

Note that this code behind is using the pseudo-object oriented form of Javascript. If this is new to you, you'll want a pretty modern book on JavaScript such as

JavaScript The Definitive Guide
by David Flanagan

Read more about this title...

The key thing to remember is that if you name a variable with "this" it is available to all the "member methods" but if you name it with var it is local to the method in which you name it. The job of "Silverlight.createDelegate" (a method provided by Visual Studio) is to ensure that the method name handleMouseUp is resolved properly, as indicated by the arrow.

The net effect is that when the user clicks on the button, the handleMouseUp event will be called. (Caution, there is no "fill" for the rectangle, and so clicking in the rectangle will have no effect, unless you click on the text!) As noted yesterday, the default fill is "null" and will not respond to events.

I've put a test alert message into the event handler to make sure it is working; when you click on the text in the button; the alert box comes up; and that assures us of two things: FindName is in fact returning a reference to the button (or else wiring up the event handler would not have been possible) and the event handler is working.

EventHandlerWorking

Since we now have references to the button and its parts, we have only to make them member variables (so that we can access them from the event handler, which is a member method, and then we can set whatever properties we want; even properties not yet declared in the XAML.

The first thing to do is to change the var's in handleLoad to this (making btn, btnRect and btnText member variables)

Using this

Return to the event handler, remove the alert box, and start setting properties!

SetPropertiesInCode

In the event handler we set properties on the objects within the button (setting the color weight and text of the text block, and the width and height of the rectangle, and then we reposition the button itself.

The result is that when we click on the button, it is redrawn as if these properties had been injected into the object.

AfterClicking

Technorati

Silverlight Cream for February 5, 2008 — #192

Jesse Liberty shows how to create XAML in JS on a mouse event, and Shawn Wildermuth explains how to get intellisense on XAML in VS2008.

From SilverlightCream.com:
How to create XAML objects in Javascript
Jesse did a very nice write-up on createFromXaml'ing (!) yesterday afternoon that answers a question I see lots on the forum and that is building something live in response to a mouse event... all with source, and great information!
Getting Silverlight XAML Intellisense in VS2008
And for the folks that diss JS and are using VS2008 to hork around their XAML, Shawn Wildermuth answers the next question which is "How do I get Intellisense turned on?"

Stay in the 'Light!

 


SLNews on Twitter | Join me at SilverlightCream | Silverlight Tagged Web Articles | My Articles | My Tutorials | My Tooltips | SilverlightCream

SilverlightCream Yahoo Group

When I put up SilverlightCream.com, or to be more specific, when I was *thinking* about putting up SilverlightCream.com, I thought for a while about putting it up as a Community Server site. I went back and forth about it, and annoyed some friends with discussion and questions. In the end, as you know, I did not do that.

My reason for not putting up a forum and having logins and all that was for a few reasons, not the least of which is that Silverlight.net/forums is sitting exactly in that space, and doing a heck of a fine job.

Developers can go to Silverlight.net and get all the resources, they can go to the forums and get their questions asked, they can go over to SilverlightCream and submit a link to their hard work, and then... hmmm... that seems to be the spot I get stuck on.

Yeah we are all out here creating stuff and answering questions, but Silverlight.net doesn't really have the *feel* for kicking ideas around and flying out some code and asking for opinions. It's more of a "get you going" than a "let's talk" sort of thing... or at least that's the impression I've gotten, and I've gotten some email from devs that feel somewhat the same.

For instance, just last week, I started a thread about a dragging issue that me and others are seeing with my OutlookBar code. It doesn't poot, but it isn't exactly right. I don't really think Silverlight.net is the place to be kicking that around, but that was the only place.

Not any more

I considered this for a while over the weekend while grading papers (again... sigh), and decided that rather than try to add something else onto SilverlightCream, why not use what's out there already that works just great and everyone knows... Yahoo Groups.

So I started a Yahoo Group named SilverlightCream

The intent is NOT to do what Silverlight.net/forums does. My first response to any "How do I..." questions is going to be a link to Silverlight.net.

SilverlightCream is and has from even the first blog post been about the community of Silverlight developers, and getting people's hard work out where it can be seen.

Let's take it up another level and talk to each other about what we've done, and how we did it. Tear code apart and put it back together, share xaml fragments, hints, tips, etc.

Everyone comes in moderated until we know you, because I don't want any fanboys or dreck... I'm way too busy to deal with all that stuff. I'm in the SouthWestern US (but you knew that by the cowboy boots, right?), and I have a moderator in the UK. If it gets really big and we need more moderators, we'll add more.

If it doesn't work out, hey, we gave it a shot, but I think it might be a nice addition to SilverlightCream.com

Stay in the 'Light!


SLNews on Twitter | Join me at SilverlightCream | Silverlight Tagged Web Articles | My Articles | My Tutorials | My Tooltips | SilverlightCream

Silverlight Cream for February 4, 2008 — #191

Early cream from Imran Shaik on creating xaml from clipart, and Pete Brown helps us all unravel WPF, Winforms, SL, and Ajax (oh my)

From SilverlightCream.com:
Converting Clip Art to Silverlight XAML
I don't have all the toys Imran uses in this article, but it certainly looks great and I have LOTS of artwork, so all I need is... :) Thanks Imran!
Silverlight, WPF, Windows Forms, Ajax - Which One is for Me?
Let Pete help you understand the differences and similarities and try to keep everything straight... need a scorecard anyone? :)

Stay in the 'Light!

Silverlight News on Twitter | Silverlight Tagged Web Articles | My Articles | My Tutorials | My Tooltips | SilverlightCream

Did You Know… How to create XAML objects in Javascript?

I have had a very strong positive reaction to focusing on the Javascript in code behind for Silverlight 1.0, and so will devote not only today's Tip of the Day to a bit more on the topic, but will make sure that I do so on a frequent basis for a while.

Before I start: a small house keeping measure: the convention of starting every Tip of the Day with "Did You Know" is getting old. Thus, starting with the very next one it will be replaced with Tip Of The Day:

A reader (let's call this one Joe) writes,

What I've had trouble understanding is a small bit of code....

 Now, I have to say that the best way to get this kind of question answered is to post it to our forums; that way a lot more people see it a lot more quickly; but since you did write to me, I'll feel free to use it as grist for the Tip of the Day mill...

sender.fill = createLinearGradientBrush(sender.getHost(), sender);

function createLinearGradientBrush(plugin)
{
    var xamlFragment = '<RadialGradientBrush>';
    xamlFragment += '<GradientStop Color="LimeGreen" Offset="0.0" />';
    xamlFragment += '<GradientStop Color="Green" Offset="1.0" />';
    xamlFragment += '</RadialGradientBrush>';

    return plugin.content.createFromXaml(xamlFragment, false);
}

I understand 'what' it is doing and i understand 'why' it is doing it, but I don't know 'how'... I found the code to do it on a forum return plugin.content.createFromXaml(xamlFragment, false)

It isn't surprising you're baffled by this code as it is pretty advanced and it starts in the middle.

sender.fill = createLinearGradientBrush(sender.getHost(), sender);

What can we see or guess?

1. the purpose of this line is to fill sender with a LinearGradientBrush.

2.  the LinearGradientBrush will be created by the function createLinearGradientBrush.

3. The function createLinearGradientBrush takes two parameters: the getHost() method of the sender and the sender itself

4. The function crateLinearGradientBrush returns a Brush (and it better be a LinearGradientBrush or someone should be hurt) that be assigned to the sender's fill property.

What is sender?  It's not a big leap to assume sender is a shape.

How did we get here? We assume "here" is the middle of some function, and sender was a parameter to that function.

To do this right, I think we want to create a simple application that might get us to just this point.  So I'll fire up Visual Studio, strip out the starter application and put the following into Scene.xaml:

image

This creates two shapes, both of which respond to the MouseLeftButtonUp by calling an event handler method named onMouseUp, which we will expect to find in the code-behind file.

Note that you must provide a fill or if you click inside the shapes the event handlers will not fire! If you want the boxes to appear unfilled, you can use Fill="Transparent" as I've done here, but if you leave the fill out, it will default to null and clicking inside the shape will have no effect.

Here's the event handler:

image

  Presumably the event handler has work to do or there is more than one event handler. In any case, the programmer has decided to factor out the work of dynamically creating the Linear Gradient Brush. We now know what sender is; it is whatever shape we clicked on (in this case) but of course you can imagine other programs in which sender will be a shape retrieved by other means (picked from a menu in a drawing program?)

We saw CreateLinearGradientBrush in the original code above, but it is needlessly complex and we can simplify both the call to it and its own logic. Let's send just the sender, and encapsulate within createLinearGradientBrush the work of getting the SilverligthHost. First, we change the call,

image

Notice that we no longer ask the calling method to know that we need to retrieve the Silverlight control by calling GetHost; we just pass along the sender (the shape). Next, we create the brush by creating the XAML as a string and then asking the Silverlight control to create the brush we need from that string,

image

The top half of the method creates a string that replicates exactly how you would create the brush in XAML. To make it clear, I've put in the spacing I'd use in a XMAL file. Though this is not required, I believe it is good coding practice and far easier to maintain.

The bottom three lines (above the comment) act as follows;

var SilverligthHost = sender.GetHost()

GetHost() can be called by any Silverlight object as explained in this article and in the Silverlight 1.0 Help Files (an often overlooked and quite useful resource

image

Now, I admit it takes a while to translate some of that back into English, but what it tells you is that you can call GetHost on any Silverlight object (e.g., sender) and get back the plug-in (that is, the Silverlight control). Handy.

The second line is the key to this entire exercise

var brush = silverlightHost.content.createFromXAML(xamlFragment, false);

To understand this, you need to know that there is a method, createFromXAML that takes a string of XAML and returns a XAML object. That is explained in some detail in this video.

In addition it is helpful to know that the Silverlight control has sub-objects, one of which is named content, as explained in

this article.

You also want to know that createFromXAML takes two arguments, the second of which is used to create a unique namespace for the objects it creates, as explained both in the video and in the Silverlight documentation

image

I've commented out the line that is actually the way most programmers (or at least most of us who grew up in the C/Unix world where terseness is prized despite the fact that it makes for harder to maintain or understand code, and we should all be slapped silly)

return sender.GetHost().content.createFromXaml(xamlFragment, false);

I include it here not because I think it is the right way to do it, but because you'll see it and will want to know how to read it; the answer is "inside out" --

"Return from this method the result of calling GetHost on the sender object and then accessing the content sub-object on the returned Silverlight control so that you can call createFromXAML and pass in the text fragment that represents the object you are creating plus the boolean value false signifying that you do not want to protect namespaces." 

Easy.

So; when you bring up the application, the objects look like they are unfilled (their fill is transparent, but if you click on them, the event handler is called, which in turn calls createLinearGradientBrush passing in a reference to whichever object was clicked on. That object accesses the Silverlight control and asks it to instantiate a brush, which it uses to fill itself with color,

image

Hope that helps.

-j

image

Silverlight Cream for February 3, 2008 - 2 — #190

Late Cream: Moayad Mardini brings another Silverlight article to his fellow Arabic-speakers, and Jesse Liberty discusses what's in the code behind.

From SilverlightCream.com:
For ASP.NET programmers who want to program Silverlight applications
Moayad Mardini follows up his 3-part series with another for ASP.NET programmers (In Arabic)
Did You Know That... The Code Behind is Just... Code?
Jesse Liberty disscusses what's behind the curtain and talks about the code-behind that normally gets left behind in presentations

Stay in the 'Light!

Silverlight News on Twitter | Silverlight Tagged Web Articles | My Articles | My Tutorials | My Tooltips | SilverlightCream