| « C# code format for blogs | Silverlight Configuration (almost) like regular .NET » |
I'm currently working on a navigation service in Silverlight in order to hook perfectly Prism with the browser's navigation utilities (back, forward and URL entry). More on that later.
In doing so, I made a view whose role is to display a series of buttons hooked to commands to form a navigation bar. Each module registered with the service would get a button to navigate to it. My service holds a collection of registered modules, but it lacked a real Command object. Instead of exposing that collection, my ViewModel then exposes an enumerable list of anonymous types enriched with a command:
public IEnumerable CommandDefinitions
{
get
{
foreach (var entry in this.navigationService.ModuleEntries)
{
yield return new
{
Command = new ModuleNavigationCommand(this.navigationService),
CommandText = entry.CommandText,
ModuleName = entry.Name
};
}
}
}
Unfortunately, that doesn't work. The View sees that there are 2 modules and creates 2 buttons, but the buttons have no content, and when I click them, the commands don't execute! Seems like Silverlight is unable to databind on anonymous types... (does SL4 have the same problem? WPF?)
The solution is to create an holder class with the same public interface as my anonymous type, and use it instead (yes, it *has* to be public) :
public IEnumerable CommandDefinitions
{
get
{
foreach (var entry in this.navigationService.ModuleEntries)
{
yield return new CommandDefinition
{
Command = new ModuleNavigationCommand(this.navigationService),
CommandText = entry.CommandText,
ModuleName = entry.Name
};
}
}
}
public struct CommandDefinition
{
public ICommand Command { get; set; }
public string CommandText { get; set; }
public string ModuleName { get; set; }
}
This post has 18 feedbacks awaiting moderation...