Wednesday, December 15, 2010

WPF VisualStateManager GotoState Bug

So, I just wasted a couple of hours trying to figure this one out so I thought I’d post about it so you guys don’t have to waste as much time as I did on it.

The WPF VisualStateManager implementation is still not as rock solid as Silverlight’s implementation (ok, let’s face it, Silverlight’s implementation is far from rock solid too, but it is much better of at this point). If you are trying to call the VisualStateManager.GotoState Method on a Window, you are going to be very very frustrated. Why? Because it doesn’t work. If you are inside a user control, it is fine, but inside of a Window… nope!

Instead of calling this

VisualStateManager.GoToState(this, "stateName", true);


You are going to need to call this



VisualStateManager.GoToElementState(this.RootElement as FrameworkElement, "stateName", true);


That should do it for you. Microsoft is aware of the issue and has promised to fix this, but who knows when that’s going to happen.



Happy state-changing!



 




WPF - Accessing Resource Dictionaries in Separate Assemblies

There are certain situations where you will need to access resource dictionaries outside of your application. For example, you may want to share one resource dictionary among several applications that share the same styles.

What you want to do is use WPF’s pack URI Scheme.

Remember to include a reference of the assembly containing the resource dictionary (YourAssembly) in the application project. Then, all you have to do is use this rather strange looking syntax to access that resource dictionary within your xaml.

<ResourceDictionary Source="pack://application:,,,/YourAssembly;component/Subfolder/YourResourceDictionary.xaml"/> 

There are many variations of this syntax that you can use. Which one is right for you depends on exactly what you are trying to do. The above syntax should work for a majority of the situations. Accessing these resources can be done in both xaml and the code behind.

For more information on the pack URI Scheme syntax, follow this link:

http://msdn.microsoft.com/en-us/library/aa970069(v=vs.85).aspx

Note - this syntax is not only for resource dictionaries. This is useful for accessing several types of data files like images for example. This is commonly used for accessing resource dictionaries, but it is not limited to that.

 

Friday, August 13, 2010

Silverlight: Blank Line in TextBlock

 

This will be a relatively short one. I just wanted to post this out there in case anyone is wondering how to do this.There are several ways to create a blank line within a single textblock (or label) in Silverlight. The simplest is to use these special characters:

&#xd;&#xa;

So, the following xaml

<TextBlock Text="hello&#xd;&#xa;world"/>

produces this text block

image

Tuesday, July 13, 2010

Bing Videos… Are you Kidding Me?!?!?!?

So there is nothing technical about this post, but I found it interesting. I have been a big supporter of Microsoft Silverlight, but I do find it a bit frustrating when I find out they don’t use their own technologies internally. Check this post out, it points out how Microsoft uses Flash for their Bing videos rather than Silverlight!

image

Now, when you click on the video, you are taken to a Silverlight player where you can view the entire clip, but why the use of Flash for the preview? Why not Silverlight? What was the technical challenge that caused them to go with Flash?

http://geekygab.blogspot.com/2010/07/bing-videos-are-you-kidding-me.html

 

Friday, July 2, 2010

Get Local Time Zone

Here’s a quick post on how to get the local time zone in your Silverlight Applications. You need to use the System.TimeZoneInfo class to get this information. This simple call will give you your local time zone.

TimeZoneInfo.Local.StandardName



If you have a DateTime value you want to convert to local time, just call the ToLocalTime method on it and that should do it. You can also use the ToUniversalTime method on it to convert it to UTC time.



Thursday, June 17, 2010

Snippet Designer

I am a big proponent of code snippets. I use them all the time and I tend to write my own snippets for code I write over and over. Recently, I found a nice Visual Studio extension out in codeplex that lets you write your own code snippets a bit faster than editing the xml yourself. Although it isn’t very difficult, I have found this new method of writing snippets a bit faster, so I thought I would blog about it. If you are interested in trying this out, here is the url: http://snippetdesigner.codeplex.com/

The extension takes no time to install. once you have it installed, all you do is select the code you want to be created as a snippet and right click and export it as a snippet. From there, you open up a nice designer that lets you change the details of your snippet and create Replacements. It is a very cool tool and once you play around with it for a while, you’ll be creating snippets in no time and well on your way to saving time in your coding.

image

image

Enjoy!

Tuesday, May 11, 2010

Another Strange Silverlight Error: “Layout cycle detected. Layout could not complete”

Wow! Is this one ever frustrating! So here’s the situation:

I’ve got a datagrid which uses RowGroups. Everything is working just fine until I get a request from the product owner to hide/show several of the columns on that datagrid by clicking a button. I figured “This isn’t going to be too bad. I’ve done this before with other datagrids. Great, I’ll have it done in 10 minutes.” WRONG! To my surprise, I wrote some code very similar to what I’ve done in the past, but I started seeing this funky error “Layout cycle detected. Layout could not complete”. Because this is a Silverlight error, I did not get any other helpful information. Thanks so much Silverlight. Well, I can’t tell you that I completely understand the internals of Silverlight and what is causing this issue, but I do know that it has something to do with looping inside the the UpdateLayout method is executing for the datagrid. It seems to be getting stuck in an infinite loop. After a lot of searching and trying out different things, I came up with a workaround. If you are experiencing this problem, try removing the datagrid from its parent prior to making the change (in my case adding/removing columns) and then add the datagrid back to it’s parent’s Children collection immediately after you make the change.

This solved it for me. Here is a snippet:

            if (show == true)
{
_clmOne.Visibility = Visibility.Visible;
_clmTwo.Visibility = Visibility.Visible;
_clmThree.Visibility = Visibility.Visible;
}
else
{
//Hack:Need to remove the grid prior to adding columns to avoid Exception.
this.LayoutRoot.Children.Remove(myDataGrid);
_clmOne.Visibility = Visibility.Collapsed;
_clmTwo.Visibility = Visibility.Collapsed;
_clmThree.Visibility = Visibility.Collapsed;
this.LayoutRoot.Children.Add(myDataGrid);
}


I hope this helps you guys out. Happy Silverlighting!