【问题标题】:how to pause activity when dialog box is open in xamarin在xamarin中打开对话框时如何暂停活动
【发布时间】:2016-06-01 08:18:37
【问题描述】:

我在我的活动上运行了一个动画,一个正在旋转的徽标....

我调用函数 onBackPressed() 显示警告对话框“你确定要退出吗?”有两个按钮“是”或“否”

.....我希望我的活动暂停,以便在对话框打开时徽标不会旋转...

这是我的代码

public override void OnBackPressed()
    {
        AlertDialog.Builder exit = new AlertDialog.Builder(this);
        exit.SetMessage("Are you sure You want to Exit");
        exit.SetCancelable(false);
        exit.SetPositiveButton("yes", (object sender, DialogClickEventArgs e) =>
         {
             this.Finish();
         });
        exit.SetNegativeButton("No", (object sender, DialogClickEventArgs e) =>
         {
         });
        exit.Show();}

【问题讨论】:

    标签: c# android xamarin visual-studio-2015 xamarin.android


    【解决方案1】:

    保持对动画的引用,并在显示对话框时对其调用暂停:

    public override void OnBackPressed()
    {
        AlertDialog.Builder exit = new AlertDialog.Builder(this);
        exit.SetMessage("Are you sure You want to Exit");
        exit.SetCancelable(false);
        exit.SetPositiveButton("yes", (object sender, DialogClickEventArgs e) =>
         {
             this.Finish();
         });
        exit.SetNegativeButton("No", (object sender, DialogClickEventArgs e) =>
         {
           myAnimation.resume();
         });
        myAnimation.pause();
        exit.Show();
    }
    

    您可以使用这个很好的示例/答案来了解如何暂停和恢复动画:https://stackoverflow.com/a/10028575/4706693 或者,如果您正在使用 Animator 类,则使用简单的暂停和恢复

    【讨论】:

      【解决方案2】:

      can't 暂停了活动,但活动的lifecycle 方法OnPause() 将在您显示对话框时被调用。

      您可以在您活动的OnPause() 方法中只stop/pause 动画。

      【讨论】:

      • public override void OnBackPressed() { OnPause(); AlertDialog.Builder exit = new AlertDialog.Builder(this); exit.SetMessage("Are you sure You want to Exit"); exit.SetCancelable(false); exit.SetPositiveButton("yes", (object sender, DialogClickEventArgs e) => { this.Finish(); }); exit.SetNegativeButton("No", (object sender, DialogClickEventArgs e) => { }); exit.Show(); } protected override void OnPause(){ base.OnPause(); }
      • 你不要调用OnPause()之类的方法,android会在需要的时候调用它们
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-03
      • 2019-02-26
      相关资源
      最近更新 更多