簡體   English   中英

用於UWP的MVVMCross可見性值轉換器

[英]MVVMCross Visibility Value Converter for UWP

我正在使用MvvmCross.Plugins.Visibility。

我有“ xmlns:valueConverters =“ using: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屬性時,會拋出一個異常,指出“指定的轉換無效”。

我需要做什么才能使它正常工作?

編輯堆棧跟蹤

   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綁定 ,則可以在所有平台上使用。

但是,如果要使用本機UWP綁定,則必須在轉換器周圍添加本機Windows包裝程序,以便它實現API所需的僅Windows的IValueConverter接口。 為此,請先在您的Core和UWP項目中安裝MvvmCross.Plugin.Visibility軟件包,然后在UWP項目中的某個位置創建以下類:

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

我通常將所有這些“本機”類放在一個Converters\\NativeConverters.cs文件中,但這只是一個約定。 您不必為這些類提供任何其他實現。 如果您在綁定中使用轉換器,則它將按預期工作。 我已經以這種方式成功運行了您的代碼。

還要確保在UWP項目中創建引導類:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM