UWP 的 MVVMCross 可见性值转换器

MVVMCross Visibility Value Converter for UWP

我正在使用 MvvmCross.Plugins.Visibility。

我"xmlns:valueConverters="使用:App.UWP.Converters"

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

我有以下布局

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

我的 ViewModel 有 IsBusy 属性

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

当我更改 IsBusy 属性 时,出现异常抛出 "Specified cast is not valid."

我需要做什么才能让它工作?

编辑堆栈跟踪

   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()

我注意到您在第一个 Binding 中有错字 - Converter 后缺少一个 = 符号。

但是,我认为主要问题可能是您使用的是 MvvmCross 转换器而不是本地转换器。 MvvmCross Value Converter 是跨平台解决方案,如果您使用 Tibet binding.

,它适用于所有平台

但是,如果您想使用本机 UWP 绑定,则必须在转换器周围添加一个本机 windows 包装器,以便它实现 Windows-only IValueConverter API 需要的接口。为此,首先在您的 Core 和 UWP 项目中安装 MvvmCross.Plugin.Visibility 包,然后您只需在 UWP 项目的某处创建以下 classes:

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

我通常将所有这些 "native" class 放在一个文件中 Converters\NativeConverters.cs,但这只是一种约定。您不必为 classes 提供任何额外的实现。如果您在绑定中使用转换器,它应该会按预期工作。我已经成功地 运行 你的代码了。

还要确保在 UWP 项目中创建 bootstrap class:

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