Reputation: 327
I have a UWP project as well as a WPF project. Both project needing the same converter. But the Interface IValueConverter
is in UWP in another namespace as in WPF, furthermore is the last parameter not the same (in WPF it is CultureInfo in UWP it is a string).
So what is the best practice to create a converter for both projects?
My solution is I have a netstandard project which has a class ValueConverterBase which defines each two methods from the WPF IValueConverter and the IValueConverter from UWP. It also defines two abstract methods which a sepcific converter has to implement :
public abstract class ValueConverterBase
{
public object Convert(object value, Type targetType, object parameter, string language)
{
return this.OnConvert(value, targetType, parameter, language);
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
return this.OnConvertBack(value, targetType, parameter, language);
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return this.OnConvert(value, targetType, parameter, culture.TwoLetterISOLanguageName);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return this.OnConvertBack(value, targetType, parameter, culture.TwoLetterISOLanguageName);
}
public abstract object OnConvert(object value, Type targetType, object parameter, string language);
public abstract object OnConvertBack(object value, Type targetType, object parameter, string language);
}
Now I can create a converter in the netstandard project which does the logic. To be able to use this converter in UWP and WPF each project has to create a class which derives from the converter and the specific IValueConverter:
public class WpfConverter : CommonConverter, IValueConverter
{
}
public class UwpConverter : CommonConverter, IValueConverter
{
}
The problem with this solution I have to add for each converter three new classes. Also every developer has to know about this workaround. Another point is that I am unable to use WPF or UWP specific logic in the converter. Does anybody has another solution?
New solution
I combined my first solution with the answers from @Anton Tykhyy and @Heinzi. I created a Shared project which will contain my converter and added there my ValueConverterBase:
public abstract class ValueConverterBase
#if UWP
: Windows.UI.Xaml.Data.IValueConverter
#endif
#if WPF
: System.Windows.Data.IValueConverter
#endif
{ ...
Upvotes: 2
Views: 1352
Reputation: 20076
I'd just use the preprocessor and share source rather than the assembly. If you want to put converters into a separate assembly, you can build a WPF and an UWP assembly out the same source with different project configurations. #if
is not the most elegant tool in the world, but it does the job and you have one file and one class. Make a sample converter and have all your developers copy that.
#if UWP
using Windows.UI.Xaml.Data ;
#else
using System.Windows.Data ;
#endif
public class SampleValueConverter : IValueConverter
{
public object Convert (object value, Type targetType, object parameter,
#if UWP
string language)
{
#else
CultureInfo culture)
{
// or vice-versa if you need culture rather than language code
var language = culture.TwoLetterISOLanguageName ;
#endif
...
}
}
Upvotes: 4