简体   繁体   中英

MVVMCross Visibility Value Converter for UWP

I'm using the MvvmCross.Plugins.Visibility.

I have the "xmlns:valueConverters="using:App.UWP.Converters"

<views:MvxWindowsPage.Resources>
    <valueConverters:VisibilityConverter x:Key="Visibility" />
    <valueConverters:InverseVisibilityConverter x:Key="InvertedVisibility" />
</views:MvxWindowsPage.Resources>

I have the following bits of layout

<Button Content="Log In" Command="{Binding LoginCommand}" Visibility="{Binding IsBusy, Converter{StaticResource InvertedVisibility}}" />
<ProgressRing Visibility="{Binding IsBusy, Converter={StaticResource Visibility}}" />

My ViewModel has the IsBusy property

bool _isBusy;
public bool IsBusy
{
    get { return _isBusy; }
    set
    {
        _isBusy = value;
        RaisePropertyChanged(() => IsBusy);
    }
}

When I change the IsBusy property, I get an exception thrown saying "Specified cast is not valid."

What do I need to do to get this to work?

EDIT for stack trace

   at System.ComponentModel.PropertyChangedEventHandler.Invoke(Object sender, PropertyChangedEventArgs e)
  at MvvmCross.Core.ViewModels.MvxNotifyPropertyChanged.<>c__DisplayClass11_0.<RaisePropertyChanged>b__0()
   at MvvmCross.Uwp.Views.MvxWindowsMainThreadDispatcher.RequestMainThreadAction(Action action, Boolean maskExceptions)
   at MvvmCross.Core.ViewModels.MvxNotifyPropertyChanged.RaisePropertyChanged(PropertyChangedEventArgs changedArgs)
   at MvvmCross.Core.ViewModels.MvxNotifyPropertyChanged.RaisePropertyChanged[T](Expression`1 property)
   at intelliSPEC.Core.ViewModels.LoginViewModel.set_IsBusy(Boolean value)
   at intelliSPEC.Core.ViewModels.LoginViewModel.<Login>d__33.MoveNext()
   --- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MvvmCross.Core.ViewModels.MvxAsyncCommandBase.<ExecuteConcurrentAsync>d__17.MoveNext()
   --- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MvvmCross.Core.ViewModels.MvxAsyncCommandBase.<ExecuteAsync>d__16.MoveNext()
   --- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MvvmCross.Core.ViewModels.MvxAsyncCommandBase.<Execute>d__14.MoveNext()
   --- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<>c.<ThrowAsync>b__6_0(Object state)
   at System.Threading.WinRTSynchronizationContext.Invoker.InvokeCore()

I have noticed you have a typo in the first Binding - there is a missing = sign after Converter .

However, I think the main problem might be that you are using the MvvmCross converters instead of native ones. MvvmCross Value Converter is cross-platform solution that works on all platforms if you use the Tibet binding .

However, if you want to use the native UWP Binding, you have to add a native windows wrapper around the converter, so that it implements the Windows-only IValueConverter interface, which is required by the API. To do this, first install the MvvmCross.Plugin.Visibility package in both your Core and UWP project and then you just create the following classes somewhere in your UWP project:

public class VisibilityConverter : 
   MvxNativeValueConverter<MvxVisibilityValueConverter> { }
public class InverseVisibilityConverter : 
   MvxNativeValueConverter<MvxInvertedVisibilityValueConverter> { }

I usually place all such "native" classes in one file Converters\\NativeConverters.cs , but that is just a convention. You don't have to provide any additional implementation for the classes. If you use the converters in your bindings, it should work as expected. I have successfully run your code this way.

Also make sure you create a bootstrap class in the UWP project:

public class VisibilityPluginBootstrap
    : MvxPluginBootstrapAction<MvvmCross.Plugins.Visibility.PluginLoader>
{
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM