2017-09-27 15 views
0

MvxAppCompatDialogFragmentを閉じるボタンで閉じても完全に機能していないようです。ダイアログを閉じるためにクリックしたボタンは、ダイアログが閉じられた後も無効のままです。ほとんどTaskのようなものです。 MvxDialogFragmentに変更すると、[戻る]ボタンがダイアログを正常に閉じ、ダイアログを閉じるためにクリックしたボタンが、ダイアログが閉じられた後に再び有効になります。私はMvxAppCompatActivityを使用しているのでMvxAppCompatDialogFragmentを使用しようとしています。私は何か間違っているのですか、これはMvvmCross 5.2.1のバグですか?ここでMvxAppCompatDialogFragment戻るボタンによるキャンセルが完全に機能していない

はViewModelにある:ここでは

public class ConfirmationViewModel : MvxViewModel<ConfirmationConfiguration, bool?>, IMvxLocalizedTextSourceOwner 
{ 
    private readonly IMvxNavigationService _mvxNavigationService; 

    public ConfirmationViewModel(IMvxNavigationService mvxNavigationService) 
    { 
     _mvxNavigationService = mvxNavigationService; 
    } 

    public override void Prepare([NotNull] ConfirmationConfiguration parameter) 
    { 
     if (parameter == null) throw new ArgumentNullException(nameof(parameter)); 
     Title = parameter.Title; 
     Body = parameter.Body; 
     PositiveCommandText = !string.IsNullOrEmpty(parameter.YesCommandText) 
      ? parameter.YesCommandText 
      : LocalizedTextSource.GetText("Yes"); 
     NegativeCommandText = !string.IsNullOrEmpty(parameter.NoCommandText) 
      ? parameter.NoCommandText 
      : LocalizedTextSource.GetText("No"); 
    } 

    private bool? _confirmationResult; 
    public bool? ConfirmationResult 
    { 
     get => _confirmationResult; 
     private set => SetProperty(ref _confirmationResult, value); 
    } 

    private string _title; 
    public string Title 
    { 
     get => _title; 
     set => SetProperty(ref _title, value); 
    } 

    private string _body; 
    public string Body 
    { 
     get => _body; 
     set => SetProperty(ref _body, value); 
    } 

    private string _positiveCommandText; 
    public string PositiveCommandText 
    { 
     get => _positiveCommandText; 
     set => SetProperty(ref _positiveCommandText, value); 
    } 

    private string _negativeCommandText; 
    public string NegativeCommandText 
    { 
     get => _negativeCommandText; 
     set => SetProperty(ref _negativeCommandText, value); 
    } 

    private IMvxAsyncCommand _yesCommand; 
    public IMvxAsyncCommand PositiveCommand => _yesCommand ?? (_yesCommand = new MvxAsyncCommand(OnPositiveCommandAsync)); 

    private async Task OnPositiveCommandAsync() 
    { 
     ConfirmationResult = true; 
     await _mvxNavigationService.Close(this, ConfirmationResult); 
    } 

    private IMvxAsyncCommand _noCommand; 
    public IMvxAsyncCommand NegativeCommand => _noCommand ?? (_noCommand = new MvxAsyncCommand(OnNegativeCommandAsync)); 

    private async Task OnNegativeCommandAsync() 
    { 
     ConfirmationResult = false; 
     await _mvxNavigationService.Close(this, ConfirmationResult); 
    } 

    public IMvxLanguageBinder LocalizedTextSource => new MvxLanguageBinder("", GetType().Name); 

    public IMvxLanguageBinder TextSource => LocalizedTextSource; 
} 

public class ConfirmationConfiguration 
{ 
    public string Title { get; set; } 
    public string Body { get; set; } 
    public string YesCommandText { get; set; } 
    public string NoCommandText { get; set; } 
} 

ビューです:

[MvxDialogFragmentPresentation(Cancelable = true)] 
[Register(nameof(ConfirmationFragment))] 
public class ConfirmationFragment : MvxAppCompatDialogFragment<ConfirmationViewModel> 
{ 
    public ConfirmationFragment() 
    { 
     RetainInstance = true; 
    } 

    public ConfirmationFragment(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer) 
    { 
     RetainInstance = true; 
    } 

    public override Dialog OnCreateDialog(Bundle savedInstanceState) 
    { 
     var builder = new AlertDialog.Builder(Activity) 
      .SetTitle(ViewModel.Title) 
      .SetMessage(ViewModel.Body) 
      .SetPositiveButton(ViewModel.PositiveCommandText, OnPositiveButton) 
      .SetNegativeButton(ViewModel.NegativeCommandText, OnNegativeButton); 
     return builder.Create(); 
    } 

    private async void OnNegativeButton(object sender, DialogClickEventArgs e) 
    { 
     if (ViewModel.NegativeCommand.CanExecute()) 
     { 
      await ViewModel.NegativeCommand.ExecuteAsync(); 
     } 
    } 

    private async void OnPositiveButton(object sender, DialogClickEventArgs e) 
    { 
     if (ViewModel.PositiveCommand.CanExecute()) 
     { 
      await ViewModel.PositiveCommand.ExecuteAsync(); 
     } 
    } 
} 

私はこのようなダイアログボックスに移動している:私はベースを変更した場合

 var confirmation = await Mvx.Resolve<IMvxNavigationService>().Navigate<ConfirmationViewModel, ConfirmationConfiguration, bool?>(
      new ConfirmationConfiguration() 
      { 
       Body = "Hello, World!", 
       Title = "Testing" 
      }); 

クラスをMvxAppCompatDialogFragmentからMvxDialogFragmentに変更すると、すべて正常に動作します。

+0

私はあなたにも 'MvxAppCompatPresenter'を使用すると思いますか?テストプロジェクトでも 'MvxDialogFragment'を使用しているので、これを調べる必要があります。https://github.com/MvvmCross/MvvmCross/blob/develop/TestProjects/Playground/Playground.Droid/Views/ModalView.csを参照してください。 – Martijn00

+0

はい、私は 'MvxAppCompatPresenter'を使用しています。これは 'MvxAppCompatSetup'によって取り込まれています。 –

答えて

1

これは本当にMvvmCross v5.2.1の問題でした(報告のおかげで!)。今、あなたはDialogFragmentクラスでこのコードを追加することができますのための回避策として:

public override void OnCancel(IDialogInterface dialog) 
{ 
    base.OnCancel(dialog); 
    ViewModel?.ViewDestroy(); 
} 

public override void DismissAllowingStateLoss() 
{ 
    base.DismissAllowingStateLoss(); 
    ViewModel?.ViewDestroy(); 
} 

public override void Dismiss() 
{ 
    base.Dismiss(); 
    ViewModel?.ViewDestroy(); 
} 
1

これを調べました。何が起こるかは、戻るボタンを押すと、NavigationSerivce.Close()を呼び出さずにビューを閉じます。これにより、結果Taskが呼び出され、アクションが設定または取り消されます。これがバグかどうかはわかりません。回避策は、CloseConfirmationViewModelViewDissapearingから電話するか、Taskをキャンセルすることです。

+0

私はそれが良い解決策だとは思わない。 'ViewDisappearing'から' Close'を呼び出すと、ダイアログが画面の回転を生かさないようになります。ダイアログを上にしてデバイスを回転させると、 'ViewDisappearing'が呼び出されます。これは私がフレームワークが扱うことを期待した別のものですが、そうではありません。私は、 'RetainInstance'が' true'に設定されている 'DialogFragment'を持っていますが、デバイスを回転させると閉じられます。それは起こるべきではありません。 –

+0

その問題を開くことができますか? – Martijn00

関連する問題