Page Switching With Parameters (the short version)

 

I recently finished Part 2 of Page Switching in which I demonstrate how to pass parameters between Silverlight pages (this video is in production and should be available soon.)

A reader asked if I could provide the essential information, so here it is, very short, and very terse, I’m afraid.

The trick, such as it is, is to overload the Navigate method in the PageSwitcher user control.  Here’s the code from PageSwitcher.xaml.cs

public partial class PageSwitcher : UserControl
{
   public object ObjectValue { get; set; }

   public PageSwitcher()
   {
      ObjectValue = null;
      InitializeComponent();
      if ( this.Content == null )
      {
         this.Content = new Page();
      }
   }

   public void Navigate( UserControl nextPage )
   {
      this.Content = nextPage;
   }


   public void Navigate( UserControl nextPage, object value )
   {
      this.ObjectValue = value;
      this.Content = nextPage;
   }

}

The overloaded Navigate takes an object and stores it in the PageSwitcher’s member variable ObjectValue. The page you switch to then only has to retrieve it.

The call to this overloaded method is demonstrated by creating an object of type person (which in this simplified version has three member variables, FirstName, LastName and a boolean as to whether the person is a member of the club)

 

void Switch_Click( object sender, RoutedEventArgs e )
{
   Person p = new Person();
   p.FirstName = FirstName.Text;
   p.LastName = LastName.Text;
   p.IsMember = RBYes.IsChecked == true;
   PageSwitcher ps = this.Parent as PageSwitcher;
   ps.Navigate( new PageX(), p );
}

 

p is an instance of person, we pass in to Navigate the new instance of PageX and the new instance of the Person object. 

In PageX’s PageLoaded method we look to see if the PageSwitcher’s object is not null, and if not, we cast it to be a person and then we can extract the values…

void Page_Loaded( object sender, RoutedEventArgs e )
{
   PageSwitcher ps = this.Parent as PageSwitcher;
   object theObject = ps.ObjectValue;
   if ( theObject != null )
   {
      Person p = theObject as Person;
if ( p != null )
{

Message.Text = "Found " + p.FirstName + " " + p.LastName + ( p.IsMember ? " is a member!" : " is not a member yet." );

} // end cast is okay } // end object is not null } // end Page_Loaded

 

Message in this case is a TextBlock that displays the output.  

The last bit of of code uses the conditional operator, you read it as “if p.IsMember evaluates true, return the string ‘ is a member ‘ otherwise return the string ‘is not a member yet. ‘

The video goes though this in greater detail, but that is the essence.

Posted in z Silverlight Archives | 3 Comments

Programming Silverlight 2 – Jesse Liberty & Tim Heuer

 

I normally do not pre-announce books.

That said, this book is likely to be of significant interest to this community, and so, now that it is officially listed on Amazon, I thought it reasonable to mention that it is on the way, and that Tim and I are exceedingly proud of it.

Our goal in Programming Silverlight 2 is to tell the story of Silverlight, not to reproduce the documentation. For me, it is a wonderful opportunity to bring together all that I’ve learned in creating the videos and the tutorials and getting your feedback about what is helpful and what is not.

In the book we go far deeper than we’ve had the opportunity to do so far here on the site, but at the risk of undermining sales, let me say that everything we cover in the book we will, no doubt, cover sooner or later here as well. The advantage of the book is that it is all put together in a single place in a single coherent telling.

We’re going to hold the book until Silverlight 2 is certified as final, which means we won’t be first to market, but on the other hand we’ll have it right, and that seems worth while. in addition, the book will mark a beginning, not an end, in that once the book is in print, we will continue to build videos and other material that will add depth for the more advanced programmer, and we’ll continue to create on-ramps for the programmers first arriving at Silverlight from .NET and elsewhere.

Thank you for indulging my book announcements. We now return you to the technical blog, already in progress.

Posted in z Silverlight Archives | Comments Off on Programming Silverlight 2 – Jesse Liberty & Tim Heuer

One of the most unusual books I’ve ever written

I’m pleased to announce the release of what may be one of the most unusual books I’ve ever written, Programming .NET 3.5

Briefly, it was our theory; generated long before I started work at Microsoft, that while there was good reason to write what I call “silo” books on each of the .NET technologies (e.g., WPF, WCF, etc.) there was a coherence to the entire Microsoft framework that was potentially missed by that approach.

Our other theory was that .NET 3.5 (broadly defined) was the first version of .NET to fully facilitate the development of n-tier applications and MVC (imagine our shock when Microsoft developed the MVC library for ASP.NET!)

All in all it was a blast to write, even though we did have to write it twice (originally we wrote Programming .NET 3, then we totally rewrote it for .NET 3.5, so this is really the 2nd edition!).

I’m not sure more than half a dozen people will buy the book, but it may catch a wave, who knows? In any case, we had fun and learned a great deal while writing it.

Posted in z Silverlight Archives | Comments Off on One of the most unusual books I’ve ever written

One Team – 4 Books on ASP.NET 3.5

I just released Learning ASP.NET 3.5 which is very exciting, through I must quickly and freely admit that my co-authors did a fantastic job shouldering the bulk of the task of the massive re-write to bring this book fully up to date for ASP.NET 3.5.

The Learning series is O’Reilly’s on-ramp, while the Programming books are targeted at the more experienced and professional programmer. That said, this book is surprisingly comprehensive and we’re quite HanselmanBookproud of it.

There are quite a few good books on ASP.NET 3.5, one written by my colleague Scott Hanselman,  that has gotten rave reviews.

ASPUnleashed Then there’s ASP.NET 3.5 Unleashed by Stephen Walther, new to our team but not new to writing fantastic books on ASP.NeT

 

 

Oh yes, I almost forgot, I’m about to release Programming ASP.NET 3.5, due out Sept. 15.

 

I suspect there are even some books by people not on our team.

So don’t buy anything until you read through a few books and decide on what you like in terms of style, coverage, approach, exercises and all those intangibles that make for a great technical book. 

Enjoy.

Posted in z Silverlight Archives | 3 Comments

New Tutorial: DataBinding and Data Templates

I’m pleased to announce that we’ve posted a new tutorial: DataBinding and Data Templates Using Expression Blend available in HTML and pdf with source code.

tutorialpdf

  This should nicely complement the three new videos on DataBinding

  Overviewxaml Blend

I hope you find them useful.

Posted in z Silverlight Archives | Comments Off on New Tutorial: DataBinding and Data Templates

Flecks Flash By…

There is a posting going around about how some part(s) of Microsoft are using Flash or some other not-Silverlight technology and how even Microsoft doesn’t believe in Silverlight, blah blah blah.  I’ll leave it to people who have investigated and who care to straighten out this silliness, but I will say that from what I can tell, there are a lot of post-hoc ergo prompter hoc errors floating around.

The good news is that this prompted a discussion with a very bright member of our community (ew, what a phrase; take two:) with a buddy that I thought I would post here, cutting down his comments and fixing up mine to make them seem much more incisive.

After some back and forth, this lead us to a discussion of his desire to choose to work in Flash or Silverlight, but really not to try to work in both for any length of time, and did I have any thoughts on the matter.

I wrote…

…for me it was never a question. Microsoft’s commitment to Silverlight is unquestioned and core; and as a developer I focus on (a) the commitment, (b) how does it fit into the lineup of development products and (c) what tools can I use and (d) what is the customer base.

As a .NET programmer for 8 years, Silverlight fits in perfectly, and the commitment to Xaml, first for WPF, then Workflow, then Silverlight, and the backflow from Silverlight to WPF, and the promotion of Scott Guthrie to VP, and the hiring of Ray Ozzie, and the creation of the Expression tools and the people they hired to head those teams, and the creation of the Developer Liaison Group that I work in, and more that I can’t talk about in terms of commitment to Silverlight going forward,  all add up to a place I had no doubt I wanted to be and that I still want to be.

….speaking as an individual and not as a Microsoft employee (which I’m told is not possible) I personally would base my decision [as to which to commit to] on what is predictable and not on what is unknowable. So, what can we know with reasonable certainty:

1. Both companies are likely to be around in 5 years, as are both products in some form

2. Both products are likely to evolve, especially in response to one another and for all we know there will be an unexpected third player to come along when we least expect it

3. There will never be an objective view point (who would provide it?) – see my blog post on don’t believe anything I say

While I have a friend who has made a great living by being a jack of all trades, that never appealed to me. Like you, I’d rather stay in one general line of technology; there is enough to learn that I won’t get bored.

So, which? The answer to that (and here I am clearly not talking for anyone but me) is to ask yourself if you have a strong stake in either camp.  If you have a strong stake in the Flash/not-Microsoft camp, then I suppose I’m supposed to turn you over to someone who can tell you why you should switch… 

If, like me, you have a strong stake in .NET then it is a no-brainer because everything I’ve seen and heard convinces me that Silverlight is the right place to put my chips (no warranty expressed or implied). 

Then a final observation, one can complain all one wants about Microsoft, but my experience for the past two decades is that the arc of our products (especially  development products) is that they get better over time, usually a lot better and usually quite quickly.

So, I quit 12 years of happy independence and came to work on the Silverlight team. I just passed the 1 year mark, and I’m committed for at least another year. I can’t make a stronger statement than that.

Posted in z Silverlight Archives | Comments Off on Flecks Flash By…

Camtasia Crash Tip Update

Back on July 3 I posted a Camtasia Crash Tip for those of you who use this terrific product for screen captures.

Since then, unfortunately, I’ve been plagued by crashes, and it turns out that TechSmith, the wonderfully responsive folks who make the product have found the problem and a work around.  In short, the problem lies in the metadata in the camrec and there are two solutions until they solve the problem.

1. You can record directly to avi and eliminate the problem (but also eliminate the ability to do picture in picture while you are recording your capture).

Save As AVI

2. You can create your camrec as usual, but then immediately strip it when you are ready to use it,

ExtractCamrecContents

and then import the avi (with your screen capture) and the mov (with your pip) but not the .dat file (which is the source of all evil).

So sayeth techSupport at TechSmith

Posted in z Silverlight Archives | Comments Off on Camtasia Crash Tip Update

Four Twitter Silverlight Blogs

A brief review of the Silverlight Twitter blogs I’m following or using…

SilverlightNews – very popular and closely connected with one of the premier Silverlight aggregators: Silverlight Cream.

Silverlight Events with a Twitter feed and a web page all managed by the extraordinary Adam Kinney

SLGeekToolThe SLMicroblog:  an experiment in “what can I blog that is useful to say in 140 characters. To be honest, it moves among a few different states: Tips of the Day, Very short Blog entries, references to longer blog entries, and short periods of rest.  I’m hoping for more of the first two as they are what make it worth while.

The Silverlight HyperBlog explained in here, but the essence is to record everything and anything I’m working on, thinking “out loud” into the blog, including ruminations, absurd speculations, etc. I have no idea why anyone would want to subscribe, that is part of the experiment; is this the least bit interesting? What I’m finding is that I’m interested in the process, at least from time to time, though I fully admit that there are other times I just work and the Hyperblog goes (blessedly?) silent for a while.

Finally, I should mention that I do have my own Twitter account, but I really only use it when I’m at a conference.

As for keeping up with any/all of this, the best tool I know of is Emil Stoychev that I wrote about here.

Posted in z Silverlight Archives | Comments Off on Four Twitter Silverlight Blogs

In-State Animation In A Nutshell

I’ll be covering this in videos and tutorials in depth, but I’ve come to realize that in-state animation is so straight forward that it can be explained in a quick blog entry. In fact, the really hard part is explaining what it is viz: the Visual State Manager uses animation (story boards) move from one state (e.g., MouseOver) to another state (e.g., MouseDown). In-state Animation allows you to add animation whilst (I’m going to the UK soon) you are in a state.

An example: When teaching templates I show how to have a button “swell” when you mouse over, and then I show how to have that swelling be non-instantaneous (by setting the transition time to non zero). You can see all that in this tutorial or in this video.  If however, you’d like your button not only to swell, but then to pulse gently while you hold the mouse over it, you need a bit of in-state animation. Fortunately, this is incredibly easy to do.

Start With The Tutorial Code

To keep this as simple as possible, I’m going to start with the code from the tutorial which I’ll open in Blend. There is quite a bit of code here, but I don’t care about any of it except modifying the template for the Button – specifically the Mouse-Over behavior.

Thus I’ll click on the Resources tab, turn the triangle to reveal what is in App.xaml and double-click on RoundButton, putting Blend into Template Editing. 

In the Objects and Timeline tab I can see the contents of the Round Button (the Ellipse and the Text). I’ll click on MouseOver which is the state I want to change, and I see that both sub-parts are already being changed (they swell). In properties / Transform I can see that the Ellipse and the Text are increased to 1.2 (120%) of their normal size. Great, ready to go.

To begin the in-state animation, I click on the Show Timeline button:

 

 

ShowTimeline

This immediately causes the timeline to slide out from behind the Objects and Timeline window. There are two ovals at time 0 indicating that what I’m looking at in the properties window is the state of the ButtonEllipse and ButtonText the instant the button is fully in MosueOver state (that is, once the transition completes).

I’ll click on 1 second which will light that line yellow and now I'm ready to set the properties that I want at 1 second; specifically that the ButtonEllipse and the ButtonText will shrink to 0.8 (80% of their initial size).  A connection appears between 0 and 1 second indicating that the time between will be filled for me with the intermediate values, and sure enough if I click on one of the ticks between, I can see in the transforms window an intermediate value.

Setting The Repeat Behavior

ClickOnState

The next step is key. Click on the State  in the Objects and Timeline, which will bring up the Common Properties window in the Properties area…

 

 

 

 

 

… so that you can click on AutoReverse, and Repeat Behavior of Forever.

SetRepeatBehavior

 

 

 

 

 

 

 

 

Note that in my preview of Blend it sticks on 1x but the Xaml is property updated.

That’s it!

Click on the Page.xaml breadcrumb to exit the template editor, and then press F5 to compile and run. Your button should pulse when you mouse over.

Nothing to it

For fun, try having the text grow while the button shrinks. Or you could have the text spin while the button pulses. There is no end of annoying things you can do once you know how.

Enjoy.

Posted in z Silverlight Archives | Tagged | Comments Off on In-State Animation In A Nutshell

Templates and Animation Webcast – Follow up

 

I had the pleasure of presenting a Webcast on Templates and Animation today, and as promised, here is the answer to an open question, and the set of links that were mentioned.

iStock_TwoButtonsXSmall

 

 

 

 

 

 

 

Why couldn’t we animate the Border?

You will remember that I was unable to animate the border thickness in my impromptu demonstration of in-state animation.

Had I looked at the documentation, I would quickly have found that the border thickness is read-only!

Fortunately we were able to animate the opacity which actually gave a more satisfying effect.

Source Code

The complete source code for today’s web cast is available here.

Posted in z Silverlight Archives | Comments Off on Templates and Animation Webcast – Follow up

Check out our new Learn page!

We listened, we heard, we acted.

The new Learn page is designed to make it easier to get started, and easier to find what you are looking for. It is also pretty snazzy.  I’ve shrunk it to point out a few features that I think will go a long way towards making life easier for folks trying to find the information they need.

NewLearnPage

(This image has been cropped and reduced to fit and to focus on the areas of interest)

I personally think the folks who did this did a great job, and I’m very pleased and proud to be associated with it. That said, after a couple days of living with it, we’d be happy to have your feedback, suggestions, ideas and etc., as always!

In the meantime, I’ll enjoy what I think is a pretty significant improvement. Hope you find it not only more attractive, but (much more important) more useful.

Posted in z Silverlight Archives | Comments Off on Check out our new Learn page!

Digging Deeper: Templates and the Visual State Manager

There has been a number of requests for videos, tutorials and Webcasts that dive deeper. A few weeks ago I scheduled a Webcast for this Wednesday to cover  the Visual State Manager which presents a good opportunity to do so.

During this Webcast I will endeavor to provide a context for skinning in general and the VSM in particular, and to provide a general introduction to the overall Parts and States Model. By the end of the hour you should have a solid understanding of what it takes to re-skin any existing control. 

This will be followed by Part II, one month from now, in which we’ll focus on creating Custom Skinnable controls and at that time we’ll dive very deeply into the Parts and States model.

If there are issues you are running into with the VSM, with transitions or with skinning controls, please let me know in the next couple days and I’ll try to address them in my presentation. 

I look forward to seeing you there.

Thanks!

Posted in z Silverlight Archives | Tagged | Comments Off on Digging Deeper: Templates and the Visual State Manager