I have a custom markup extension which resolve commands using dependency injection. It is very handy for me, because I don't have to create commands in view model and bindings for them. Recently I've been told that using such markup extension isn't a good practice in mvvm and I should avoid that. Is that true?
Code of markup extension:
public class InjectCommandExtension : MarkupExtension
{
#region Props
[ConstructorArgument("key")]
public string Key { get; set; }
#endregion
#region ctor
public InjectCommandExtension()
{
}
public InjectCommandExtension(string key)
{
Key = key;
}
#endregion
#region ProvideValue
public override object ProvideValue(IServiceProvider serviceProvider)
{
if (Key == null)
throw new ArgumentNullException("Key");
return ServiceLocator.Current.GetInstance<ICommand>(Key);
}
#endregion
}
Use in XAML:
<Button Content="Delete" Command="{mext:InjectCommand DeleteOrderCommand}"/>
Personally don't see, if we speak in this general way, about custom markup injection. The only thing I can cancern about is the complexity you should dela with. Having them decalred in
XAML
help you and other developers in the group to avoid creating a mess.Good luck.