How to bind WindowsRT data to a conversion type without TypeConverterAttribute

In the previous question / answer, I found out how data binding in Windows Phone makes TypeConversion for string to ImageSource using TypeConverterAttribute - see https://stackoverflow.com/a/2776161

Now I am considering the same problem in WindowsStore applications where TypeConverterAttribute does not exist .

If I use WinRT data binding, then it is clear that the correct conversion is still possible for the data binding. MSDN documentation says:

this behavior is based on a base type conversion that processes the string as a URI and invokes the equivalent of the BitmapImage (Uri) constructor.

However, I don’t see metadata anywhere about how he knows which conversion to do.

I'm trying to create my own data binding - so being able to mimic what WinRT does would be very useful.

Is there a metadata tip tooltip somewhere that tells the Xaml data binding what transformation is applied? Or is it a conversion of string to ImageSource , somehow baked at runtime, hidden from the CLR?

If it is hidden, is there a list of known automatic type conversions so that I can cache them in my data binding implementation?

+6
source share
2 answers

I discussed this issue via Twitter with one of the WinRT developers (Tim Heyer)

The main summary of this conversation:

  • WinRT has nothing like TypeConverterAttribute
  • A small number of WinRT controls have a small number of internal transformations that they will apply.
  • No controls or conversions available
+2
source

In Windows 8 applications, this is usually handled by creating a class that inherits from IValueConverter , and then is assigned along with a data binding. As indicated on the msdn website:

"Create a converter by implementing the IValueConverter interface and implementing the Convert method. This method should return an object of the same type as the dependency property that indicates the binding targets or at least a type that can be implicitly forced or converted to the target type.

Here is a link to the full document: http://msdn.microsoft.com/en-US/library/windows/apps/windows.ui.xaml.data.binding.converter.aspx

See the Examples section for more details.

To answer your question, there is no metadata hint, you just create your own converter, and then explicitly assign the converter along with the binding of custom data.

0
source

Source: https://habr.com/ru/post/947769/


All Articles