Using MvxVisibilityValueConverter in WPF

I'm just starting out with MVVMCross, so forgive me if this seems like a simple question. I am trying to use the MVVMCross Visibility plugin in WPF mentioned here:

https://github.com/MvvmCross/MvvmCross/wiki/Value-Converters

I installed the plugin and try to follow these steps:

Windows - use your own wrappers or Tibetan binding, as described above:

Visibility = "{Binding VMProperty, Converter = {StaticResource Visibility}}"

When I try to do this, it cannot find the Visibility resource.

So, I realized, I can add a namespace:

xmlns:visibility="clr-namespace:Cirrious.MvvmCross.Plugins.Visibility;assembly=Cirrious.MvvmCross.Plugins.Visibility" 

... and then add the converter to my resources:

 <visibility:MvxVisibilityValueConverter x:Key="Visibility"></visibility:MvxVisibilityValueConverter> 

... but now I get:

An object of type "Cirrious.MvvmCross.Plugins.Visibility.MvxVisibilityValueConverter" cannot be applied to a property that expects a type of "System.Windows.Data.IValueConverter".

Should I make my own converter for this, for example:

 class MyVisibilityConverter : MvxNativeValueConverter<MvxVisibilityValueConverter> { } 

... or am I missing something? The docs seem to indicate less work.

+6
source share
1 answer

IValueConverter is not currently a portable interface, and it was a deliberate decision from Microsoft. I talked with one of the PCL team guys about this - it seemed very clear that they expected most value converters to be platform specific and therefore would not sit in common code.

Because of this - and because MvvmCross believes that many cost converters will be shared, we had to introduce our own IMvxValueConverter interface inside MvvmCross. This IMvx interface IMvx not be used directly with XAML and Microsoft bindings - so that is the reason why native packaging is currently needed.

You can get around this - if you want - using the Tibet MvvmCross binding platform instead of Microsoft, but I think most MS-based developers still use MS binding.


Am I missing something? The docs seem to indicate less work.

To use value converters on Windows, the wiki says the text below - if you think this could be improved, please make the changes back - we are committed to continuing to improve.

Using value converters on Windows (regular Xaml binding)

The IMvxValueConverter interface IMvxValueConverter closely related to the IValueConverter interface, which is used in the binding of Windows WPF and Silverlight Xaml. This interface is also similar (but slightly different) to the IValueConverter interface used in the Windows WinRT Xaml binding.

Since these Xaml IValueConverter interfaces are not 100% identical to each other or to the IMvxValueConverter version, the common Mvx ValueConverters cannot be used directly in the Windows Xaml binding - instead, they must be wrapped for use in Xaml.

The steps for this are similar on every Windows platform:

  • for each IMvxValueConverter class, for example. for

      public class TheTruthValueConverter : MvxValueConverter<bool, string> { public string Convert(bool value, Type targetType, CultureInfo cultureInfo, object parameter) { return value ? "Yay" : "Nay"; } } 
  • in your user interface project, create a “native” shell using the MvxNativeValueConverter class:

      public class TheNativeTruthValueConverter : MvxNativeValueConverter<TheTruthValueConverter> { } 
  • in your Xaml, include an instance of your ValueConverter as a static resource - this can be done at the Resources level at the application, page or Xaml control level, for example:

      <converters:TheNativeTruthValueConverter x:Key="TheTruth" /> 
  • Now your converter can be used - for example:

      <TextBlock Text="{Binding HasAccepted, Converter={StaticResource TheTruth}}" /> 

Using Value Converters in Windows (Tibetan Language Binding)

In addition to the “traditional” bindings, Xaml MvvmCross also allows you to bind “Tibet” on Windows - see the wiki / Databinding for more details.

When binding to the Tibetan type is used, Value Converters can be accessed by name - just like in binding to Droid and Touch - without the aforementioned native Xaml packaging.

In addition, if you use the Tibet binding, the whole value of the value converters can be registered using the Reflection sweep method, and this can be indicated at the Xaml level - this means that it can be used both in design and at runtime.

To include all value converters in an assembly at the Xaml level, use the mvx:Import block with the From internal attribute, which contains an instance of the class from this assembly.

It may seem complicated ... but it is actually quite simple.

  • Suppose you have a MyTools assembly containing FooValueConverter , BarValueConverter , etc.
  • In this assembly, add a simple, unstable open class, which we will use only for import. public class MarkerClass {}
  • Then in xaml you can enable the static resource import block, for example:

      <mvx:Import x:Key="MvxAssemblyImport0"> <mvx:Import.From> <myTools:MarkerClass /> <mvx:Import.From> </mvx:Import> 
  • After that, the values ​​of ValueConverters Foo and Bar will be available for use in Tibet bindings - for example. as:

      <TextBlock mvx:Bi.nd="Text Foo(Name)" /> 
+7
source

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


All Articles