I have a WinUI 3 app where we use Dependency-Injection from Microsoft.Extensions, and settings containing DateTime the current Date-Time formatting have been registered to the service collection as followed:
services.AddSingleton<IDateFormatService, DateFormatService>();
I'd like to just inject it into the constructor of the IValeConverter, but as it is constructed by the XAML the constructor must be empty.
Below is my current converter
public sealed class DateTimeFormatingConverter : DependencyObject, IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
if (value is not DateTime dateTime)
return value;
// return formatted dateTime based on settings formatting string
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
I know other injection libraries can allow this, as I've seen in this post and there for wondered if a likewise solution exist with the Microsoft extensions version.
I know it's a possibility to make a DependencyProperty and in XAML bind it to the dependency injected property in the viewmodel. However im looking into this as it would clean up the code substantially and while also removed multiple requirements from the converter that another developer won't easily know would be required.