Monday, July 21, 2008

Adam Kinney has post some articles in the past showing a Silverlight application for displaying gamer cards.  When the Playstation 3 finally caught up and created a "Portable ID", I thought this would be a decent chance to demonstrate some Silverlight chops. Yes, I have a PS3 and not an XBox360.  I thought maybe I'd make an application with unnecessary animations and sounds for fun, perhaps allow you to sort friends and setup notifcations that are not available from the PSN.  Here's my Playstation portable ID:

The gist of the online status is simply a JPG, in my case http://pid.us.playstation.com/user/drpayne.jpg , that gets updated when your status changes via the console.  Excited to run off and write code I created a Silverlight 2 app and set about downloading this JPG.  Except that it doesn't work because of Silverlight's cross domain security policies.  I'm not a genious in TCP/IP or DNS/BIND, but the policies used by Silverlight (mimicing those used by Flash) seem overly restrictive and make some scenarios that should be common and easy difficult or un-doable.  There may be a reverse-tunnel situation or similar DNS trickery that is capable with this type of application, but denial of service?  Check out what I just did in this blog posting:

<img border=0 src="http://pid.us.playstation.com/user/drpayne.jpg"/

When the markup for this page is downloaded to your browser, the browser then issues seperate http requests for content that lives on other servers.  That content comes from us.playstation.com, and Google analytics, and Blogged, and others.  This is, seemingly, not a security or denial of service risk in this particular situation.  The PS3 network site shown above did not need to place a client policy XML file in the server root, and in fact they would need to do work to prevent cross domain access of this type.  Sure, Silverlight has more than just HTTP networking capabilities, in fact for a future article I have a full blown instant messanger application implemented in Silverlight using Sockets.  In terms of being a good Technology Citizen, I can see Microsoft wanting to be very careful concerning what it allows devlopers to do with more general socket programming.  But HTTP?  Isn't this part of what the web is "about" ?

What do you think?  Is the cross domain policy employed by Silverlight too restrictive?  Does it not go far enough?  Just right?

Monday, July 21, 2008 12:20:51 PM (Central Standard Time, UTC-06:00)  #    Disclaimer  |  Comments [0]  |  Trackback
 Wednesday, June 25, 2008

So, I am building a Silverlight 2 application related to my trip to Klipsch in Indianapolis this year, and found what seems to be to be a terrible bug in Silverlight 2 beta 2.  Multiple UIElements  cannot share event handlers.  What do I mean?  In my case, I have varous Paths inside a canvas and I'm creating hotspots.  When the mouse enters a given region, something slightly different will happen, however the exact same method is called for every single region when the mouse exits that Path.  So, suppose I have a Path called Damon, which happens to have some Damon-y things inside it and I'm going to display my name:

private void _damonPayne_MouseEnter(object sender, MouseEventArgs e)

{

AddCallout("D.R. Payne", "damonrpayne", "Hartford, WI", e);

}

When the mouse exists, I simply remove the UIElement representing the callout, which is the same for all regions on the page:

private void _damonPayne_MouseLeave(object sender, MouseEventArgs e)

{

RemoveOldCallout();

}

Now, it would seem that I could make the functionality in RemoveOldCallout a MouseEventHandler and share the same method among the various paths:

protected void RemoveOldCallout(object sender, MouseEventArgs e)

{

RemoveOldCallout();

}

Now, if I have another Path, I should be able to write code like _someOtherPath.MouseLeave += new MouseEventHandler(RemoveOldCallout);  This code, however, bombs with the following BadPropertyValue error:

 

Now, I have to write a different MouseLeave event for every single path on my page, and there's a lot of them.  I'll submit to Connect and hope this issue is fixed!

Wednesday, June 25, 2008 11:54:43 AM (Central Standard Time, UTC-06:00)  #    Disclaimer  |  Comments [0]  |  Trackback
 Thursday, June 05, 2008

Ok, so, I found some great Silverlight tips on the afternoon of Day two courtesy of Jeff Prosise (http://www.wintellect.com/TechnicalBioDetail.aspx?Tech=5) .   I am definitely going to be taking his advice on some points and blogging about it as I convert my Beta 1 examples to Beta 2 examples.  Jeff had an implementation of The Game of Life in Silverlight that got me thinking about getting some Concurrency peanut butter into the Silverlight chocolate.  Would the LINQ based ray tracer work without too much modification in Silverlight?  Surely the Task Parallel Library will not work in Silverlight without serious modification and many things will likely not be available at all.  Still, I smell a side project: to what degree could we at least get Parallel.ForEach<> , Parallel.For, and perhaps some of the basic System.Threading.Tasks.Task things working in Silverlight 2.  Possible uses for this include a Silverlight based Folding @home style app and I’m sure there are others.

2008 TechEd MVP Party

I’m writing this on the morning after the 2008 MVP party, hosted in the Voodoo Room at the House of Blues in Orlando.  What a blast!  I showed up and realized that while I knew no one most other folks seemed to already know other MVPs.  Shortly thereafter Carl Franklin walked in the door.  Since I don’t get to meet Internet Famous people often, I said hi and thanked him for introducing me to Steely Dan with his rendition of Home at Last .  He seemed amused and gave me the big High Five when I said I actually bought Aja because of him. 

There were two guys there doing the whole Blues Brother s schtick.  It would seem not as many people came as RSVP’d, so I was able to call The Vanderboom and get him in to the party.  The lower attendance also meant that the free drink tickets flowed in a steady stream for all of us.  There are some pictures circulating of me swing-dancing with a woman who runs a user group in Seattle.  Hopefully no other incriminating evidence exists, everyone remaining when they kicked us out was getting wacky by that time. 

Thanks to Microsoft for putting this event on, and all the MVPs who came for a great time.

Thursday, June 05, 2008 10:01:14 AM (Central Standard Time, UTC-06:00)  #    Disclaimer  |  Comments [0]  |  Trackback
 Tuesday, June 03, 2008

One thing I forgot to mention from the keynote this morning, and msftguy beat me to it.  Silverlight 2 Beta 2 will ship by the end of this week.  One thing Brian does not mention is that Somasegar claimed this version would have a go-live license.

Hopefully this means SL2 is nearly ready to ship.

Tuesday, June 03, 2008 3:09:33 PM (Central Standard Time, UTC-06:00)  #    Disclaimer  |  Comments [0]  |  Trackback
 Saturday, April 12, 2008

http://www.klipschcorner.com/silverlight/DeepZoomTestPage.html

I have a multi scale image sample here, using code from Scott Hanslemann and others.   I had other photos to include, but no matter what machine I tried to build this on, the Deep Zoom Composer seemed to want to consume 100% CPU and 100% memory once I got up to 10 images or so.  The Palladium speakers are certainly beautiful, and with some print quality images you can count the coils on the transistors in the crossover.  These speakers are so beautiful.

Friday, April 11, 2008 11:06:26 PM (Central Standard Time, UTC-06:00)  #    Disclaimer  |  Comments [0]  |  Trackback
 Monday, April 07, 2008

I sat down to continue some actual programming work today since it seemed support and manager-y type things had settled down.  Having gotten into WPF long before Silverlight, I found that to have a similar experience there are a lot of things I'm going to have to implement for the "CarSpot Silverlight Application Framework"

  • Commanding: I have seen references to people creating their own commanding frameworks for Silverlight 2.
  • Pages/Navigation: I wound up creating a Screen Stack for navigating from one Logical Page to another but without Pages and a Navigation Service I have to wire this all together myself. 
  • Security: It seems odd that Client Application Services was left out of Silverlight.  There are any number of ways to hack this in since the Silverlight control is sitting in the browser, but I was surprised to see that there was no "CurrentPrincipal" on the CurrentThread and so forth. I need security for my .aspx apps, why not for my Silverlight apps?  If one is building a line of busniess application, there are surely bits of information or actions that need to be hidden except for certain user roles.

Still, I'll take this over JavaScript any day.

Monday, April 07, 2008 3:30:18 PM (Central Standard Time, UTC-06:00)  #    Disclaimer  |  Comments [0]  |  Trackback
 Sunday, March 23, 2008

I thought that a nice feature for my KlipschCorner System Profiles Area might be a deep zoom for the people who upload very high res photos.  I quickly realized I do not currently see any way to automate the creation of the Deep Zoom .bin files needed by the MultiScaleImage control.  One step beyond automation, it ought to be available in a server-side Silverlight API for the many people in hosted environmnets where they may not be able to install Deep Zoom Composer and shell execute an .exe. 

Saturday, March 22, 2008 11:58:13 PM (Central Standard Time, UTC-06:00)  #    Disclaimer  |  Comments [1]  |  Trackback
 Friday, March 07, 2008

Catastrophic failure!  Now that is an error message worthy of my college dating life!... anyhoo, I digress.  Many controls in winforms and WPF probably try to do things in the constructor or an "OnLoad" event of some kind that are not design-time-friendly, but the magic of the environment mostly keeps things like this from happening.  This magic dust has not yet been sprinkled upon the Silverlight 2.0 tools for VS2008, but I'm still very excited about the possibilities as this platform matures.  The error below occurs if I add some test images into the loaded event of the carousel and try to view the designer. 

Friday, March 07, 2008 8:23:31 PM (Central Standard Time, UTC-06:00)  #    Disclaimer  |  Comments [0]  |  Trackback

Ok, here is my silverlight 2 port of the ever popular Image Carousel sample.  For me, at least, it's much easier to follow what's going on than the JavaScript version.  Here's the markup required in the page:

<UserControl x:Class="ImageUploader.ImageCarousel"

    xmlns="http://schemas.microsoft.com/client/2007"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Width="800" Height="600" Canvas.Top="0">   

<Canvas x:Name="_mainCanvas" Loaded="mainCanvasLoaded" MouseMove="whenMouseMoves"

        MouseLeftButtonDown="mainDown" MouseLeftButtonUp="mainUp" MouseLeave="mainCanvasMouseLeave" >

    <Canvas Name="imagesHolder" Canvas.Left="0" Canvas.Top="0">

        <Canvas Name="mainImageHolder" Canvas.Left="200" Canvas.Top="60" Canvas.ZIndex="169" Opacity="1.0">

            <Image Name="mainImage" Stretch="UniformToFill" Height="225" Width="300"/>

        </Canvas>

    </Canvas>

</Canvas>

</UserControl>

And here is the code-behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;

namespace ImageUploader
{
public partial class ImageCarousel : UserControl
{
public ImageCarousel()
{
InitializeComponent();
_mainDown = false;
_urls = new Dictionary<Image, string>();
_angles = new List<double>();
_imagePaths = new List<string>();
//_imagePaths.Add("Upload/1.jpg");
//_imagePaths.Add("Upload/2.jpg");
//_imagePaths.Add("Upload/3.jpg");
_dispTimer = new System.Windows.Threading.DispatcherTimer();
_dispTimer.Interval = TimeSpan.FromMilliseconds(25);
_dispTimer.Tick += new EventHandler(dt_Tick);
_dispTimer.Stop();
}

private List<double> _angles;
private bool _mainDown = false;
private List<string> _imagePaths;
private List<ScaleTransform> _scaleTransforms = new List<ScaleTransform>();
private double _speed = -0.0425;
private Dictionary<Image, string> _urls;
private System.Windows.Threading.DispatcherTimer _dispTimer;

public void AddImage(string imageUrl)
{
_mainDown = false;
_dispTimer.Stop();
_angles.Clear();
_scaleTransforms.Clear();
_urls.Clear();

for (int i = 0; i < _imagePaths.Count; ++i)
{
Canvas holder = (Canvas)_mainCanvas.FindName("imgHolder" + i);
_mainCanvas.Children.Remove(holder);
}

_imagePaths.Add(imageUrl);
Image img = (Image)_mainCanvas.FindName("mainImage");
string srcPath = _imagePaths[_imagePaths.Count -1 ];
img.Source = new BitmapImage(new Uri(GetURLBase() + srcPath));
img.SetValue(Canvas.ZIndexProperty, 169);

BuildImages();
}

private void mainCanvasLoaded(object sender, RoutedEventArgs e)
{
if (_imagePaths.Count > 0)
{
Image img = (Image)_mainCanvas.FindName("mainImage");
img.Source = new BitmapImage(new Uri(GetURLBase() + _imagePaths[0]));
img.SetValue(Canvas.ZIndexProperty, 169);
//
BuildImages();
}
}

public List<string> ServerImagePaths { get; set; }

protected void BuildImages()
{
int leftPos = 0;
for (int i = 0; i < _imagePaths.Count; ++i)
{
Canvas imgHolder = new Canvas();
imgHolder.SetValue(Canvas.NameProperty, "imgHolder" + i);
imgHolder.SetValue(Canvas.LeftProperty, leftPos);
imgHolder.SetValue(Canvas.TopProperty, 0);
//Come back to reflections...
//Canvas imgHolderReflection = new Canvas();
//imgHolderReflection.SetValue(Canvas.NameProperty, "imgHolderRef" + i);
//imgHolderReflection.SetValue(Canvas.TopProperty, 160);
//imgHolderReflection.Opacity = 1;
//LinearGradientBrush lgb = new LinearGradientBrush();
//lgb.StartPoint = new Point(0, 0);
//lgb.EndPoint = new Point(0, 1);
//lgb.GradientStops.Add(new GradientStop());
//lgb.GradientStops.Add(new GradientStop());
//lgb.GradientStops[0].Offset = .37;
//lgb.GradientStops[0].Color = Color.FromArgb(0x00, 0x00, 0x00, 0x00);
//lgb.GradientStops[1].Offset = 1;
//lgb.GradientStops[1].Color = Color.FromArgb(0x88, 0x00, 0x00, 0x00);
//imgHolderReflection.OpacityMask = lgb;
//imgHolder.Children.Add(imgHolderReflection);
//
Rectangle rec = new Rectangle();
rec.SetValue(Rectangle.NameProperty, "rec" + i);
rec.SetValue(Canvas.TopProperty, -6);
rec.SetValue(Canvas.LeftProperty, -6);
rec.Height = 92.0;
rec.Width = 92.0;
rec.Fill = new SolidColorBrush(Colors.LightGray);
imgHolder.Children.Add(rec);
//
Image img = new Image();
img.Cursor = Cursors.Hand;
img.MouseEnter +=new MouseEventHandler(img_MouseEnter);
img.MouseLeave +=new MouseEventHandler(img_MouseLeave);
img.MouseLeftButtonDown +=new MouseButtonEventHandler(img_MouseLeftButtonDown);
img.MouseLeftButtonUp +=new MouseButtonEventHandler(img_MouseLeftButtonUp);
img.Stretch = Stretch.UniformToFill;
img.SetValue(Image.NameProperty, "img" + i);
BitmapImage imgSource = new BitmapImage(new Uri(GetURLBase() + _imagePaths[i]));
_urls.Add(img, GetURLBase() + _imagePaths[i]);
img.Source = imgSource;
img.Width = 300;
img.Height = 225;

img.Opacity = 1;
imgHolder.Children.Add(img);
ScaleTransform imgCanvasScaleTrans = new ScaleTransform();
_scaleTransforms.Add(imgCanvasScaleTrans);
imgCanvasScaleTrans.ScaleX = 1;
imgCanvasScaleTrans.ScaleY = 1;
imgCanvasScaleTrans.CenterX = 50;
imgCanvasScaleTrans.CenterY = 50;
imgHolder.RenderTransform = imgCanvasScaleTrans;
//
_mainCanvas.Children.Add(imgHolder);
//
_angles.Add( i*((Math.PI*2)/_imagePaths.Count) );
}
PositionItems();
_mainDown = true;// huh?
//Set dispatcher interval
_dispTimer.Start();
}

/// <summary>
/// Not used yet
/// </summary>
/// <param name="i"></param>
protected void AddOne(int i)
{
}

void dt_Tick(object sender, EventArgs e)
{
MoveItems();
}

protected void PositionItems()
{
int radiusX=400;
int radiusY=110;
int centerX=425;
int centerY = 260;

for(int i = 0; i < _imagePaths.Count;++i)
{
double myX = Math.Cos(_angles[i])*radiusX + centerX;
double myY = Math.Sin(_angles[i])*radiusY + centerY;
Canvas imgCanvas = (Canvas)_mainCanvas.FindName("imgHolder" + i);
imgCanvas.SetValue(Canvas.LeftProperty, myX);
imgCanvas.SetValue(Canvas.TopProperty, myY);
ScaleTransform stRef = _scaleTransforms[i];
double sc = (myY - stRef.ScaleY) / (centerY + radiusY-stRef.ScaleY);
stRef.ScaleX = sc;
stRef.ScaleY = sc;
_angles[i] += _speed;
imgCanvas.SetValue(Canvas.ZIndexProperty, (int)myY);
}
}

protected void MoveItems()
{
if (_mainDown)
{
PositionItems();
}
}

void img_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{

}

void img_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Image img = (Image)sender;

     Image mainImg = (Image)_mainCanvas.FindName("mainImage");
mainImg.Source = new BitmapImage(new Uri(_urls[img]));
mainImg.SetValue(Canvas.ZIndexProperty, 169);
}

void img_MouseLeave(object sender, MouseEventArgs e)
{
//Change color BACK or something cool
}

void img_MouseEnter(object sender, MouseEventArgs e)
{
//Change color or something cool
}

private void whenMouseMoves(object sender, MouseEventArgs e)
{
Point pt = e.GetPosition(null);
double _root_xmouse = pt.X;
     double _root_ymouse = pt.Y    ;
_speed = ((_root_xmouse - 500) / 500) * 0.0755;
}

private void mainDown(object sender, MouseButtonEventArgs e)
{
_mainDown = true;
}

private void mainUp(object sender, MouseButtonEventArgs e)
{
_mainDown = false;

}

private void mainCanvasMouseLeave(object sender, MouseEventArgs e)
{

}


protected string GetURLBase()
{
string str = System.Windows.Application.Current.Host.Source.OriginalString;
return str.Substring(0, str.LastIndexOf("/")).Replace("ClientBin", string.Empty);
}
}
}


 

Not formatted as well as my last posts, I know.  It seems that Google reader doesn't like it when I paste out of Word. Now, one thing that is not very apparent is how to add this UserControl to another UserControl, assuming that JUST having an image carousel is not what you're after.  You need to include the following on the top-level user control:

xmlns:CarSpot="clr-namespace:ImageUploader"

which then allows you to refer to the code:

<CarSpot:ImageCarousel x:Name="_carousel"></CarSpot:ImageCarousel>

This is an aspect of XAML I don't like. There's nothing (that I can see) in the structure/XSD of a XAML document that tells me that an attribute value starting with "clr-namespace:" is something special.  There's a lot of magic like this in XAML.  The Visual Studio "Silverlight Chainer" as the file calls the Beta 1 tools are definately buggy and incomplete.  I am lucky to be able to hit F5 10 times before I get errors trying to debug and having to kill Cassini and VS2008.  I've also found that it's very easy to crash the designer by having naughty things in your code-behind.  It appears that unlike more mature design-time experiences there is no "IsDesignTime" flag that would allow you to keep from having code execute while designing.  I can tell that this is Cider by the wonderfully descriptive stack traces I get, yet things like dragging the new Silverlight controls onto the surface or even moving them around (I'm sick of tweaking location with Canvas.Top/Canvas.Left) are not working yet.  This is a huge step forward, but I've got to think there'll be another beta after this one now.

And that's that.  I should have some more Silverlight 2 observations later, especially about the threading and HTTP aspects.

Friday, March 07, 2008 4:36:07 PM (Central Standard Time, UTC-06:00)  #    Disclaimer  |  Comments [1]  |  Trackback

I have translated the Silverlight "Image Carousel" into Silverlight 2.0 and C# rather than the "JS writing XAML" model.  If I get the code cleaned up this afternoon I will post the carousel portion.

Friday, March 07, 2008 1:33:00 PM (Central Standard Time, UTC-06:00)  #    Disclaimer  |  Comments [0]  |  Trackback