Checking for Design Time in Silverlight Applications
Occasionally, you’ll want to alter your application logic to behave different when in design mode. (E.g., when you’re viewing your application in Blend or Visual Studio "Cider”.) Usually, you’ll want to alter your application to use mock data sources and perhaps skip out on validation, user prompts, etc..
So how do you check for design time? Easy! Use the DesignerProperties.GetIsInDesignMode(…) method and it’ll return ‘true’ if your application is currently hosted by a designer.
Works great for Blend. Unfortunately, this currently does not work with Cider. (Cider, btw, is the designer surface you see in Visual Studio.) I’ve found it useful to create my own application-level property to provide hints about design time to work around the Cider issue.
I’ve created a static Boolean property that uses DesignerProperties.GetIsInDesignMode(…) first (because it does work in Blend) and falls back to examining the current Application instance. This works because the Application instantiated during design time is, for the time being, either nothing or of type Application.
public static bool InDesignMode
{
get
{
if (inDesignMode.HasValue == false)
{
inDesignMode =
Application.Current == null ||
Application.Current.GetType() == typeof(Application);
}
return inDesignMode.Value;
}
}
Simple enough and it gets the job done. :) That said, this will probably be scrapped in Visual Studio 2010 when we get a better XAML editor … I hope.
If you want to see this working in a real designer, don’t forget that you can open up another instance of Visual Studio (or your favorite debugger) and attach to VS/Blend/<whatever> and set your breakpoints to watch how your application really behaves while in design mode. Very useful!
Silverlight 2 – Using Generic.xaml to define control templates
I’ve seen a number of posts describing troubles in getting Generic.xaml working in a Silverlight 2 project. After going through some of the common pitfalls myself, I thought it might be useful to post a concise list of steps required.
- In Silverlight 2 and beyond, place the Generic.xaml file in a (project root) folder named Themes. (Names are not case-sensitive)
- Make sure Generic.xaml is configured such that:
- Build Action is set to "Resource"
- Copy to Output Directory is set to "Do not copy"
- Custom Tool is blank
- In any of your controls that are trying to use templates defined in Generic.xaml, make sure you set the DefaultStyleKey property in your class constructor.
public MyControl(){ this.DefaultStyleKey = typeof(MyControl);}
- Verify the general structure of your generic.xaml file.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:my="clr-namespace:MyExampleControl"> <Style TargetType="my:MyControl"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="my:MyControl"> <TextBlock Text="Foo" /> </ControlTemplate> </Setter.Value> </Setter> </Style></ResourceDictionary>
Remember that control templates are only applied when the control is added to the visual tree. If you're still not able to see your template after verifying the above steps, take a close look at your code to make sure your control is being added to the visual tree for rendering.
Debugging Silverlight in Firefox, Safari
If you’re already familiar with attaching to processes in Visual Studio (or WinDbg), this is nothing new. However, I’ve seen this question come up a few times—particularly with people coming over from competing technologies.
In Visual Studio, just select the “Attach to process” option (available in the Debug or Tools menu, depending on your workspace view) and select the type of code you want to debug, Silverlight in this case, and the process you want to debug. This applies to any process that is hosting a Silverlight instance.
You should be aware that this general advice applies to all sorts of applications. For example, if you want to debug the behavior of your Silverlight control library during design-time operations, you could use one instance of the debugger to attach to the application that is rendering the design surface. (E.g., attach to Blend or event to another instance of Visual Studio.)
Firefox loads localhost resources slowly
If while debugging your Silverlight application using the Visual Studio web development server, you notice Firefox is painfully slow to load any resource, you might want to disable IPv6 support. This should get Firefox (<=3) working on par with Safari, IE, et al..
- Open Firefox
- Navigate to “about:blank”
- Find the entry for “network.dns.disableIPv6” and set it to false.
Now try loading your local development site! This applies to the general case of Firefox loading anything from localhost.
Silverlight Lynx Beta Plugin Available
Looks like the first version of Silverlight for Lynx is available in Beta format. Can’t wait for RTM! ;)