Add padding to Grid in Silverlight for Row and Column

I spent far too long trying figure out how to add padding to a Grid’s rows and column. I styled the controls, etc. It finally dawned on me that the answer was much simpler. Stupid Simple actually, so just save yourself some headache and INSERT A ROW WITH A FIXED HEIGHT!. Or COLUMN……Stupid simple, I told you.

<Grid>
<Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="4" /> <!-- Look ma, padding -->
        <RowDefinition Height="Auto" />
</Grid.RowDefinitions>
.....
</Grid>

Bind Title for the ChildWindow to a localized resource using MVVM and Extension

I need to ensure my Silverlight application is localizable, so I am binding to a localizable resource file in the project. The issue is, how do I bind to the Title property? It crashed when trying to bind to a local resource since it hasn’t loaded the resources at this point. Even though the designer shows it working, the application doesn’t like it.

So I originally over engineered this by doing a custom extension. After too much coding, and getting it to work by the way, it dawned on me….Much easier way. Just set the childwindow title property after the resources tag…Argghhhh…….

Easy title binding

<controls:ChildWindow.Resources>
 <res:LocalizedStrings xmlns:local ="clr-namespace:PwC.AuraOnline.Modules.DeployEngagement" x:Key="LocalizedStrings" />
 <infrastructure:BooleanToVisibilityConverter x:Key="boolVisibilityConverter"/>
 </controls:ChildWindow.Resources>
 <controls:ChildWindow.Title>
 <TextBlock Text="{Binding  ClientSearchResources.WindowTitle,Source={StaticResource LocalizedStrings}}"></TextBlock>
 </controls:ChildWindow.Title>

I hope this helps someone out.

Happy Coding!

DelegateCommand in Prism loses eventhandler and CanExecute never gets called

I beat my head against the all for half a day wondering why the DelegateCommand doesn’t work in my Silverlight application. Very frustrating. I traced the code in and somehow the event handler loses its reference. I am guessing it something to do with the WeakReference being used to store the event reference.

Anyway, after wasting entirely too much time with it, I just said screw it and went to using the RelayCommand class I  found. It works great and all the problems I had went away. When I have free time, I will go back and see what the true cause for the reference drops were.

Here is the RelayCommand code, if you need it. I got it from an MSDN article a while back for MVVM.
Interface

using System.Windows.Input;
namespace Seekford.Infrastructure.Commanding
{
 /// <summary>
 /// Support for commanding.
 /// </summary>
 public interface IRelayCommand : ICommand
 {
 /// <summary>
 /// Raises the can execute changed.
 /// </summary>
 void RaiseCanExecuteChanged();
 /// <summary>
 /// Determines whether this instance can execute the specified parameter.
 /// </summary>
 /// <param name="parameter">The parameter.</param>
 /// <returns>
 ///     <c>true</c> if this instance can execute the specified parameter; otherwise, <c>false</c>.
 /// </returns>
 bool CanExecute(object parameter);
 /// <summary>
 /// Executes the specified parameter.
 /// </summary>
 /// <param name="parameter">The parameter.</param>
 void Execute(object parameter);
 }
}

Class Definition

using System;
using System.Windows.Input;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics;

namespace Seekford.Infrastructure.Commanding
{

 /// <summary>
 /// A command whose sole purpose is to relay its functionality to other
 /// objects by invoking delegates. The default return value for the CanExecute
 /// method is 'true'.  This class does not allow you to accept command parameters in the
 /// Execute and CanExecute callback methods.
 /// </summary>
 public class RelayCommand :  IRelayCommand
 {
 private readonly Action _execute;
 private readonly Func<bool> _canExecute;

 /// <summary>
 /// Initializes a new instance of the RelayCommand class that
 /// can always execute.
 /// </summary>
 /// <param name="execute">The execution logic.</param>
 /// <exception cref="ArgumentNullException">If the execute argument is null.</exception>
 public RelayCommand(Action execute)
 : this(execute, null)
 {
 }

 /// <summary>
 /// Initializes a new instance of the RelayCommand class.
 /// </summary>
 /// <param name="execute">The execution logic.</param>
 /// <param name="canExecute">The execution status logic.</param>
 /// <exception cref="ArgumentNullException">If the execute argument is null.</exception>
 public RelayCommand(Action execute, Func<bool> canExecute)
 {
 if (execute == null)
 {
 throw new ArgumentNullException("execute");
 }

 _execute = execute;
 _canExecute = canExecute;
 }

#if SILVERLIGHT
 /// <summary>
 /// Occurs when changes occur that affect whether the command should execute.
 /// </summary>
 public event EventHandler CanExecuteChanged;
#else
 /// <summary>
 /// Occurs when changes occur that affect whether the command should execute.
 /// </summary>
 public event EventHandler CanExecuteChanged
 {
 add
 {
 if (_canExecute != null)
 {
 CommandManager.RequerySuggested += value;
 }
 }

 remove
 {
 if (_canExecute != null)
 {
 CommandManager.RequerySuggested -= value;
 }
 }
 }
#endif

 /// <summary>
 /// Raises the <see cref="CanExecuteChanged" /> event.
 /// </summary>
 [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic",
 Justification = "The this keyword is used in the Silverlight version")]
 [SuppressMessage("Microsoft.Design", "CA1030:UseEventsWhereAppropriate",
 Justification = "This cannot be an event")]
 public void RaiseCanExecuteChanged()
 {
#if SILVERLIGHT
 var handler = CanExecuteChanged;
 if (handler != null)
 {
 handler(this, EventArgs.Empty);
 }
#else
 CommandManager.InvalidateRequerySuggested();
#endif
 }

 /// <summary>
 /// Defines the method that determines whether the command can execute in its current state.
 /// </summary>
 /// <param name="parameter">This parameter will always be ignored.</param>
 /// <returns>true if this command can be executed; otherwise, false.</returns>
 [DebuggerStepThrough]
 public bool CanExecute(object parameter)
 {
 return _canExecute == null ? true : _canExecute();
 }

 /// <summary>
 /// Defines the method to be called when the command is invoked.
 /// </summary>
 /// <param name="parameter">This parameter will always be ignored.</param>
 public void Execute(object parameter)
 {
 _execute();
 }
 }
}

Display Silverlight loading Interstitial without blocking design surface

My Silverlight application makes a lot of web service calls on the initialization of the view model. I don’t want the user touching things while it loads but I also want the user to be able to be visibly notified something is happening and to wait.

In comes a nice interstitial control/loading panel I wrote. The problem is, the control takes up the entire form and it needs to be the topmost control. What’s the problem? Well, can’t see the form or work on it when the loading panel is in my face.

The resolution was to create a Boolean to Opacity converter . The converter works great for making the Control appear and disappear and the use of the FallbackValue lets me work with it in design mode.

Resources definition for converter

<UserControl.Resources>
 <infrastructure:BooleanToVisibilityConverter x:Key="boolConverter"/>
 <infrastructure:BooleanToOpacityConverter x:Key="boolOpacityConverter"/>
 </UserControl.Resources>

Declaration of the control in the xaml

<my:LoadingInterstitial   x:Name="LoadingInterstitial1" Opacity="{Binding FallbackValue=0,Path=IsReady,Converter={StaticResource boolOpacityConverter}, ConverterParameter=True }" Visibility ="{Binding FallbackValue=Collapsed,Path=IsReady,Converter={StaticResource boolConverter}, ConverterParameter=True }"  />

Implementation of the value converter

using System;
using System.Windows;
using System.Globalization;
using System.Windows.Data;

namespace PwC.AuraOnline.Infrastructure.Converters
{
 public class BooleanToOpacityConverter : IValueConverter
 {
 /// <summary>
 /// Modifies the source data before passing it to the target for display in the UI.
 /// </summary>
 /// <param name="value">The source data being passed to the target.</param>
 /// <param name="targetType">The <see cref="T:System.Type"/> of data expected by the target dependency property.</param>
 /// <param name="parameter">An optional parameter to be used in the converter logic.</param>
 /// <param name="culture">The culture of the conversion.</param>
 /// <returns>
 /// The value to be passed to the target dependency property.
 /// </returns>
 public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
 {
 var flag = false;
 if (value is bool)
 {
 flag = (bool)value;
 }
 else if (value is bool?)
 {
 var nullable = (bool?)value;
 flag = nullable.GetValueOrDefault();
 }
 if (parameter != null)
 {
 if (bool.Parse((string)parameter))
 {
 flag = !flag;
 }
 }
 if (flag)
 {
 return 100;
 }
 else
 {
 return 0;
 }
 }

 /// <summary>
 /// Modifies the target data before passing it to the source object.  This method is called only in <see cref="F:System.Windows.Data.BindingMode.TwoWay"/> bindings.
 /// </summary>
 /// <param name="value">The target data being passed to the source.</param>
 /// <param name="targetType">The <see cref="T:System.Type"/> of data expected by the source object.</param>
 /// <param name="parameter">An optional parameter to be used in the converter logic.</param>
 /// <param name="culture">The culture of the conversion.</param>
 /// <returns>
 /// The value to be passed to the source object.
 /// </returns>
 public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
 {
 var back = value.GetHashCode() == 100;
 if (parameter != null)
 {
 if ((bool)parameter)
 {
 back = !back;
 }
 }
 return back;
 }
 }
}

The above code will help you on your way.

If you haven’t used converters or are really new to Silverlight, I suggest reading a lot of the helpful materials out on the web.
If you are experienced, this is really easy to implement.

Happy Coding!

“The request is not supported.” when loading your Silverlight Application in IIS

So, you load your Silver light application and get excited to view it for the first time on your web server. Boom!

You get a blank screen. Or if you have debugging enabled, you get an error trying to load Silverlight application file. hmmmm. What can this be?

Try typing in the direct url for the xap file on your server and you get “The request is not supported”.

It turns out it is simply the MIME type for XAP not being registered. Register it on your IIS server and problem solved.

Here is a nice post about how to do that .

Happy coding!

Best Buy cancels every StarCraft II Pre-Order

Best Buy in their infinite stupidity decided to cancel every order for StarCraft II. Why? Well, if you are expecting a good reason, it’s not coming.
They found that their was a glitch on their site and some people were able to order the game for $5.99 instead of $59.99. I ordered it as the proper price, and so did a lot of other people.

How did Best Buy decided to handle the issue? Cancel just the errant orders you say? Of course not. Some retard made the decision to cancel them all. Every order. Over 9,000+ orders maybe even a lot more.

It’s a big company, so the decision maker is probably going to keep his job despite causing the company to lose half a million in revenue on top of the customer frustration, call center costs, and general ill will.

Good Job Best Buy! Way to go, brilliant management.

Excuse the rant, they just really pi$$ed me off.

Lotus Notes JVM Terminated Error=8096

We all know Lotus Notes is a big pile of crap. Well, I kept getting this lovely error JVM Terminated Error=8096 after a power failure.

Good times.

It turns out, that after much head banging, simply blowing out your TEMP folder will fix it.

Just open your TEMP folder and delete the folder called XPDPLAT.

You will then be on your way.

Easy way to get there: Type %TEMP% into your Explorer and hit ENTER.
Happy Coding!

Silverlight Developer Runtime not installed. Silverlight 4

Recommended reading for you Silverlight junkies:
Good book for Silverlight Development: Pro Silverlight 4 in C#


I keep running into issues with Silverlight not thinking it is installed. I combined this with always have the consumer edition of Silverlight installed on and not the Developer edition.

Add to the fact that finding the Developer edition is near impossible unless you read the fine print on the Silverlight page.

Here is the link to download the Silverlight Developer Runtime from Microsoft. I put this here for my convenience more than anything.

You can also go here: http://www.silverlight.net/getstarted/ and hit CTRL + F and type windows developer runtime.

EDIT: I have noticed a large number of people hitting this page. Well, let me help you guys out a little more. It turns out that EVERY time Microsoft decides to update the Silverlight runtime. A patch, string change, someone looked at it wrong, the Developer runtime gets invalidated.

You will need to reinstall it each time. So make sure you keep it handy! Thanks Microsoft for such a wonder feature. I love reinstalling things every week.

Happy Coding!