in

Fort Worth .NET Users Group

RogerGuess

June 2008 - Posts

  • Silverlight Links

    There is already an incredible wealth of samples, articles, tutorials, and applications out there for Silverlight. Here is a list of the links I like to keep my eye on.

     

  • Silverlight 2 Beta 2: Value does not fall within the expected range

    This can come from a number of potential errors with XAML. The first thing I now check, is to see if I am attempting to add multiple FrameworkElements with the same x:Name. For me, I was dynamically adding usercontrols to a canvas, but I made the mistake of giving the root element of the usercontrol an x:Name.

     Other cases where I created this error:

    • Specified a Grid.Row or Grid.Column that was greater than the actual number of columns or rows

     

  • SilverLander: A Silverlight 2 Game (updated for Beta 2)

    This was my first Silverlight game. I have now updated it for Silverlight 2 Beta 2 ( here is the Beta 1 version ). The game counts down and begins the lander on approach from the top left of the screen. Your job is to bring the lander to a safe slow (and upright) landing on the surface inside the highlighted landing zone. Each level increases difficulty by increasing gravity, increasing initial velocity, decreasing available fuel, and a decreasing landing zone radius. Use the arrow keys to rotate and thrust.

     

    I started with the building blocks for the gameloop and use of sprites and vectors from Bill Reiss demos on SilverlightRocks

    My next steps are:

    • Gravity is currently straight down, so I need to update that to be the center of the moon/object. The game loop adds a vector straight down instead of directly toward the center of the moon. (Done, thx to Bill Reiss who has way more physics experience than me)
    • Make magnitude of gravity relative to the proximity to the moon, rather than a constant. 
    • There is a random math error during the game loop: you may experience it when the ship just jumps completely out of the screen. but appears to continue moving.
    • Create a formula for scoring, with bonuses for time taken, fuel remaining, difficulty, and slower velocity at landing.
    • Capture scores on a leader board via a web service.
    • Create a 'instrument panel' for indicators; speed, descent rate, remaining fuel, attitude, range to surface, etc.
    • Improve location of messages on the screen.

     

  • Runtime Alternative to XamlReader.Load in Silverlight 2 Beta 2

    This Sample was update for Silverlight 2 Beta 2 ( the beta 1 example is here )

    I can see XamlReader.Load has it's uses for adding FrameworkElements at runtime, but I really hate the way the code looks. Additionally, I don't like the idea of managing a bunch of String.Format statements for my XAML. Things get even more complex when you want to take advantage of events. Luckily, there is an alternative.

    Here is an example of adding elements (ellipse, storyboard, doubleanimation) at runtime, and attaching events so that they are removed when the storyboard ends:

    public Page()
            {
                InitializeComponent();
                this.MouseLeftButtonUp += new MouseButtonEventHandler(Page_MouseLeftButtonUp);
                updateElementCount();
            }
    
            public int spriteCount { get; set; }
    
            void Page_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
            {
                Point point = e.GetPosition(this);
                Ellipse ellipse = new Ellipse();
                SolidColorBrush mySolidColorBrush = new SolidColorBrush();
                DoubleAnimation animation = new DoubleAnimation();
                Storyboard storyBoard = new Storyboard();
                
                spriteCount += 1;
                string spriteName =string.Format("SpriteEllipse{0}", spriteCount);
                ellipse.SetValue(Ellipse.NameProperty, spriteName);
                mySolidColorBrush.Color = Color.FromArgb(255, 0, 0, 0);
                ellipse.Fill = mySolidColorBrush;
                ellipse.Width = 30;
                ellipse.Height = 30;
                ellipse.SetValue(Canvas.TopProperty, (point.Y - ellipse.Width/2));
                ellipse.SetValue(Canvas.LeftProperty, (point.X - ellipse.Width/2));
                LayoutRoot.Children.Add(ellipse);
                
                animation.Duration = new TimeSpan(0,0,3);
                animation.From = 1;
                animation.To = 0;
                animation.SetValue(Storyboard.TargetNameProperty, spriteName);
                animation.SetValue(Storyboard.TargetPropertyProperty, new PropertyPath(String.Format("{0}.Opacity", spriteName)));
    
                storyBoard.Completed+=new EventHandler(storyBoard_Completed);
                storyBoard.SetValue(Storyboard.TargetNameProperty, spriteName);
                
                storyBoard.Children.Add(animation);
                LayoutRoot.Resources.Add(string.Format("{0}_storyBoard", spriteName), storyBoard);
                storyBoard.Begin();
    
                updateElementCount();
            }
    
            void storyBoard_Completed(object sender, EventArgs e)
            {
                Storyboard storyBoard = (Storyboard)sender;
                string spriteName = storyBoard.GetValue(Storyboard.TargetNameProperty).ToString();
                FrameworkElement sprite = (FrameworkElement)LayoutRoot.FindName(spriteName);
                LayoutRoot.Children.Remove(sprite);
                LayoutRoot.Resources.Remove(string.Format("{0}_storyBoard", spriteName));
                updateElementCount();
            }
    
            void updateElementCount()
            {
                this.txtChildren.Text = string.Format("Children.Count = {0}", LayoutRoot.Children.Count());
                this.txtResources.Text = string.Format("Resources.Count = {0}",  LayoutRoot.Resources.Count);
            }
  • Silverlight 2 Beta 2 %project%TextPage.html Looking for Beta 1 (installed version 2.0.30523 requested version 2.0.30226)

    I converted a Beta 1 project to Beta 2 and made a few fixes to get it up and running. However, I noticed the testpage.html was still looking for the previous version of Silverlight 2 Beta 1. It's a quick fix: Create a new Silverlight 2 Beta 2 project/solution and grab the source for the %Project%TestPage.html and use it for your existing project. All you have to change is the path to the .xap file.

  • Silverlight 2 Beta 2: "Operation could destabilize the runtime"

    I ran into this after having Visual Studio convert my Beta 1 project to Beta 2.

    System.Security.VerificationException was unhandled by user code
      Message="Operation could destabilize the runtime."


    I created a new Beta 2 solution from scratch and noticed at least one difference, which turned out to be a fix in my case. The reference to mscorlib was pointing to "%\Program Files\Microsoft Silverlight\2.0.30226.2\mscorlib.dll" for my converted project, but was pointed to "%\Program Files\Microsoft SDKs\Silverlight\v2.0\Reference Assemblies\mscorlib.dll" in the new Beta 2 solution. I updated the reference to the new location and all was well except for a few Beta 2 breaking changes that were easy to fix.

    Note: I had two references to mscorlib in the previous location, inside two projects in my solution. One was a Silverlight Library, and the second was a Silverlight App that referenced the library. The error stack had the error inside the referenced library.

  • The requested operation cannot be performed on a file with a user-mapped section open. Silverlight

    I am getting this error when publishing the a asp.net website that gets added to he solution for Silverlight applications in Visual Studio 2008.

    The requested operation cannot be performed on a file with a user-mapped section open.

    I do not know the root cause yet, but I am currently getting around it by clearing out all the files in the website's ClientBin folder before publishing.

     

  • SilverLander: A Silverlight 2 Game

    This was my first Silverlight game. I have now updated it for Silverlight 2 Beta 1 ( here is the Beta 2 version ). The game counts down and begins the lander on approach from the top left of the screen. Your job is to bring the lander to a safe slow (and upright) landing on the surface inside the highlighted landing zone. Each level increases difficulty by increasing gravity, increasing initial velocity, decreasing available fuel, and a decreasing landing zone radius. Use the arrow keys to rotate and thrust.

     

    I started with the building blocks for the gameloop and use of sprites and vectors from Bill Reiss demos on SilverlightRocks

    My next steps are:

    • Gravity is currently straight down, so I need to update that to be the center of the moon/object. The game loop adds a vector straight down instead of directly toward the center of the moon. (Done, thx to Bill Reiss who has way more physics experience than me)
    • Make magnitude of gravity relative to the proximity to the moon, rather than a constant. 
    • There is a random math error during the game loop: you may experience it when the ship just jumps completely out of the screen. but appears to continue moving.
    • Create a formula for scoring, with bonuses for time taken, fuel remaining, difficulty, and slower velocity at landing.
    • Capture scores on a leader board via a web service.
    • Create a 'instrument panel' for indicators; speed, descent rate, remaining fuel, attitude, range to surface, etc.
    • Improve location of messages on the screen.

     

  • WatiN SelectList Name

    WatiN does not currently expose the name property of a select list. However, there is still a way to get at it if you have the element that represents it with a reference to Microsoft.mshtml: foreach ( Element element in ie.Elements) { string name = Read...
  • Runtime Alternative to XamlReader.Load in Silverlight

    Update: This was a Silverlight Beta 1 example. Beta 2 now has updates to make this even easier. Here is an example.

    I can see XamlReader.Load has it's uses for adding FrameworkElements at runtime, but I really hate the way the code looks. Additionally, I don't like the idea of managing a bunch of String.Format statements for my XAML. Things get even more complex when you want to take advantage of events. Luckily, there is an alternative.

    Here is an example of adding elements (ellipse, storyboard, doubleanimation) at runtime, and attaching events so that they are removed when the storyboard ends:

     public Page()
            {
                InitializeComponent();
                this.MouseLeftButtonUp += new MouseButtonEventHandler(Page_MouseLeftButtonUp);
                updateElementCount();
            }
    
            public int spriteCount { get; set; }
    
            void Page_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
            {
                Point point = e.GetPosition(this);
                Ellipse ellipse = new Ellipse();
                SolidColorBrush mySolidColorBrush = new SolidColorBrush();
                DoubleAnimation animation = new DoubleAnimation();
                Storyboard storyBoard = new Storyboard();
                
                spriteCount += 1;
                string spriteName =string.Format("SpriteEllipse{0}", spriteCount);
                ellipse.SetValue(Ellipse.NameProperty, spriteName);
                mySolidColorBrush.Color = Color.FromArgb(255, 0, 0, 0);
                ellipse.Fill = mySolidColorBrush;
                ellipse.Width = 30;
                ellipse.Height = 30;
                ellipse.SetValue(Canvas.TopProperty, (point.Y - ellipse.Width/2));
                ellipse.SetValue(Canvas.LeftProperty, (point.X - ellipse.Width/2));
                LayoutRoot.Children.Add(ellipse);
                
                animation.Duration = new TimeSpan(0,0,3);
                animation.From = 1;
                animation.To = 0;
                animation.SetValue(Storyboard.TargetNameProperty, spriteName);
                animation.SetValue(Storyboard.TargetPropertyProperty, "Opacity");
    
                storyBoard.Completed+=new EventHandler(storyBoard_Completed);
                storyBoard.SetValue(Storyboard.TargetNameProperty, spriteName);
                storyBoard.Children.Add(animation);
                LayoutRoot.Resources.Add(storyBoard);
                storyBoard.Begin();
    
                updateElementCount();
            }
    
            void storyBoard_Completed(object sender, EventArgs e)
            {
                Storyboard storyBoard = (Storyboard)sender;
                string spriteName = storyBoard.GetValue(Storyboard.TargetNameProperty).ToString();
                FrameworkElement sprite = (FrameworkElement)LayoutRoot.FindName(spriteName);
                LayoutRoot.Children.Remove(sprite);
                LayoutRoot.Resources.Remove(storyBoard);
                updateElementCount();
            }
    
            void updateElementCount()
            {
                this.txtChildren.Text = string.Format("Children.Count = {0}", LayoutRoot.Children.Count());
                this.txtResources.Text = string.Format("Resources.Count = {0}",  LayoutRoot.Resources.Count);
            }
Copyright FWDNUG 2008
Powered by Community Server (Commercial Edition), by Telligent Systems