Friday, July 25, 2008

[Silverlight] Adding an image - Why isn't the image showing???

At first all the examples seemed so simple, however, my image was still not showing up when I ran my application.

As it turns out I was placing my image in the incorrect location. I have a Silverlight web application, and as it turns out:
The paths are relative to the .xap file found under the folder [myAppName]/[myAppName]/[myAppNameWeb]/ClientBin.
Like a noob I had been placing them in the folder that contained the .xaml/.cs pages and didn't understand why the images weren't showing...doh!


.

Monday, July 21, 2008

[Silverlight, C#] Animations on Transforms...

So I have been creating a little user control which allows me to dynamically create animations from 'templates', hook them up to elements, run the animation and then kill the animation once it is complete.

I have created a fadeIn/FadeOut quite successfully, however, when trying to animate a shrink/grow I have ran into an issue.

I cannot seem to create an transform animation on an element that does not explicitly have a Group Transform [Scale X / Y in my case] setup on it.

With other properties, like Opacity, you can still animate the property even if it has not been explicitly setup... but this does not seem to be the case with transform properties. I will be doing some forum searching on this tomorrow.


.

Tuesday, July 15, 2008

[Silverlight, C#] WebClient Caching Issue

In my project I am let the user do dynamic loads and saves to the server. During this process I found out that given identical URLS, WebClient [and HTTPBrowserRequest] will retrieve cached copies of the retrieved files. As handy as this is, I couldn't find a way to flag these classes to NOT cache.

Thus, thanks to this forum post, I have created a workaround which involves creating a unique URI.
client.DownloadStringAsync(new Uri(Global.PhpURL + "Graphics/" + fileName + "?FudgeCache=" + DateTime.Now.ToString()));
Fun Stuff. Maybe it will get fixed later, since from what I've read you can flag no cache in WPF.

.

Friday, July 11, 2008

[Silverlight, C#] TextBox Fuuuuuuuuun!

So this afternoon I dove into the wonderful world of TextBoxes, as I now wanted to add the ability for people to add text to my WYSIWYG drag/drop app. And boy oh boy does this look fun [*rollseyes*]. Tabstops and focus issues oh my!

Giving Focus to a TextBox Programatically

While I thought this was as easy as calling the Focus() function, but as it turns out, you also have to make sure that the IsTabStop property is true as well.
this.mytextbox.IsTabStop = true;
this.mytextbox.Focus();

Focus Puts the Caret in the TextBox, but Keystrokes are not captured
/shrug

[Silverlight, C#] Determining Coords with respect to the application

So I am working with multiple user controls, and I have elements in those user controls that I want to drag/drop them seamlessly between without the user knowing about the fact that the elements were being passed back and forth between user controls.

Originally I had been recursively adding together the Top/Left values all the way up to the "root"... however, when I introduced the user controls, it became tricky. Each user control has it's own "root"... so my recursive function would stop at the user control root, and thus my Top/Left values were now only relative to the user control root! Doh!

After digging for awhile, I came across a wonderfully handy little function:
UIElement.TransformToVisual
Returns a transform object that can be used to transform coordinates from the UIElement to the specified object.

With this I made a function that would return me a Point containing the offset X/Y values that would give me the relative coord change needed to move to the new coord space of the wrt element.
/// < summary >
/// Returns the Point that can translate the desired element w.r.t to another element.
/// < /summary >
public static Point getVisualCoordsWRT(UIElement findMe, UIElement wrtMe)
{
  GeneralTransform gt = findMe.TransformToVisual(wrtMe);
  return gt.Transform(new Point(0,0));
}

So to get coords wrt the application I could do this:
Point offset = getVisualCoordsWRT(controlElement, Application.Current.RootVisual);


Thanks to this Silverlight forum post.

.

Thursday, July 3, 2008

[Silverlight, C#] Creating Attached "Collection" Properties

Sorry for a lengthy delay in posting... it's been a frustrating past few weeks. Anyhoot - FINALLY I have a solution for a problem that has been plaguing me for awhile now.

What I wanted to do:
I wanted to be able to have a collection of random elements [in my case, a collection of Link elements [my own class] and be able to associate that collection with ANY element in my XAML DOM... not just Panel elements - Rectangles, Paths etc...

Essentially I wanted my code to look like this [where local is the access to my namespace]:
< Rectangle x:Name="testingRectangle" >
  < local:LinkManager.Links >
    < local:LinkCollection>
      < local:Link ObjRef="sdf" / >
    < / local:LinkCollection>
   < / local:LinkManager.Links >
< / Rectangle >

Creating the Link Class:
I spent a ton of time trying to figure out how to get the ObjRef property to be implemented correctly so that it could be accessible in syntax to how I showed in teh above XAML... turns out it's dead simple. No need to register AttachedProperties or DependancyProperties and whatever the heck else I was grasping at straws to do. It's as simple as this:
public class Link
{
 private String _ObjRef;
 public String ObjRef
 {
  get { return _ObjRef; }
  set { _ObjRef = value; }
 }
}


Adding the Attached Collection:
In order to do this, I created a class which would act as the LinkCollection, which derived from the ObservableCollection generic.
public class LinkCollection : ObservableCollection < Link > { }


Creating the Attached Property:
Now I needed to create a class which would essentially be a 'hook' which would allow me to create an attached property which all other elements could access. I called this LinkManager, and it only consists of the registering a attached property [giving access to the newly created LinkCollection].
public class LinkManager
{
 public static readonly DependencyProperty LinksProperty =   DependencyProperty.RegisterAttached(
   "Links",
   typeof(LinkCollection),
   typeof(LinkManager),
   null);

 // Accessors for the dependency property:
 public static LinkCollection GetLinks(DependencyObject obj)
 {
  return (LinkCollection)obj.GetValue(LinksProperty);
 }
 public static void SetLinks(DependencyObject obj, LinkCollection value)
 {
  obj.SetValue(LinksProperty, value);
 }
}

Accessing the Collection in Code Behind:
Then it was easy enough to grab the collection given the above XAML!
LinkCollection testCollection = this.testingRectangle.GetValue(LinkManager.LinksProperty) as LinkCollection;


And that's it! Now I can attach a collection of Links to whatever XAML element I want via AttachedProperties!

Huge thanks to dcstraw on the Silverlight forums I posted on ;)
http://silverlight.net/forums/p/19884/69373.aspx#69373

Cleaning up the code a little:

I was hoping to do something like this post talks about, which would remove one of the levels of XAML code... but for some reason I cannot get it to work properly.


.