Xamarin android renderer:按钮是第一次环绕,但是当按下后按钮时,它变成方形

问题描述:

我已经制作了一个渲染器来在Android中制作圆形按钮。当您正常进入页面时,它会毫无问题地执行。但是当您按下后退按钮返回到该页面时,该按钮将变成方形。Xamarin android renderer:按钮是第一次环绕,但是当按下后按钮时,它变成方形

我的渲​​染代码:

public class FloatingActionButtonRenderer : Xamarin.Forms.Platform.Android.AppCompat.ButtonRenderer 
{ 
    private GradientDrawable _normal, _pressed; 

    protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e) 
    { 
     base.OnElementChanged(e); 

     if (Control != null) 
     { 
      var button = (FloatingActionButton)e.NewElement; 

      button.SizeChanged += OnSizeChanged; 

     } 
    } 

    private void OnSizeChanged(object s, EventArgs e) { 
     var button = (FloatingActionButton)s; 
     var radius = (float)Math.Min(button.Width, button.Height) * Resources.DisplayMetrics.Density; 

     // Create a drawable for the button's normal state 
     _normal = new Android.Graphics.Drawables.GradientDrawable(); 

     if (button.BackgroundColor.R == -1.0 && button.BackgroundColor.G == -1.0 && button.BackgroundColor.B == -1.0) 
      _normal.SetColor(Android.Graphics.Color.ParseColor("#ff2c2e2f")); 
     else 
      _normal.SetColor(button.BackgroundColor.ToAndroid()); 

     _normal.SetCornerRadius(radius); 

     // Create a drawable for the button's pressed state 
     _pressed = new Android.Graphics.Drawables.GradientDrawable(); 
     var highlight = Context.ObtainStyledAttributes(new int[] { Android.Resource.Attribute.ColorActivatedHighlight }).GetColor(0, Android.Graphics.Color.Gray); 
     _pressed.SetColor(highlight); 
     _pressed.SetCornerRadius(radius); 

     // Add the drawables to a state list and assign the state list to the button 
     var sld = new StateListDrawable(); 
     sld.AddState(new int[] { Android.Resource.Attribute.StatePressed }, _pressed); 
     sld.AddState(new int[] { }, _normal); 
     Control.SetBackground(sld); 
     button.SizeChanged -= OnSizeChanged; 
    } 
} 

所以我创造改变大小的处理程序。它在两次都没有问题的情况下创建,但只有在正常进入页面时才会进入事件。当你按下后退按钮时它不会进入。

截图浮动操作按钮: button

编辑 一些额外的信息: 我忘了提,我重写我的MasterDetailPageOnBackButtonPressed,我这样做是因为否则它是当我按下崩溃在后退按钮:

protected override bool OnBackButtonPressed() 
{ 

    Page page = GoPrevPage(); 
    if (page.GetType() == typeof(LoginPage)) 
    { 
     App.Current.MainPage = new LoginPage(); 
    } 

    else 
    { 
     page.Parent = null; 
     Detail = page; 

     navigationDrawerList.SelectedItem = selectedMenuItems.LastOrDefault(); 

    } 
    return true; 
} 

的GoPrevPage是未来的一个静态类的函数:

public static class BackButtonHelper 
{ 
    public static List<Page> prevPages; 
    public static List<MasterPageItem> selectedMenuItems; 

    static BackButtonHelper() { 
     prevPages = new List<Page>(); 
     selectedMenuItems = new List<MasterPageItem>(); 
    } 

    public static Page GoPrevPage() { 
     prevPages.RemoveAt(prevPages.Count - 1); 
     selectedMenuItems.RemoveAt(selectedMenuItems.Count - 1); 
     return prevPages[prevPages.Count - 1]; 
    } 
    public static void AddPageToPrev(Page page, MasterPageItem masterPageItem) 
    { 

     if (!IsToegevoegd(page.ClassId)) 
     { 
      prevPages.Add(page); 
      selectedMenuItems.Add(masterPageItem); 
     } 


    } 
    private static bool IsToegevoegd(string title) { 

     return prevPages.Last().ClassId == title; 
    } 
} 

这就是当你点击动作按钮(导航出现这种情况的动作:

private void insertTaak_Clicked(object sender, EventArgs e) 
{ 
    var navPage = new DetailTaak("0") { Title = "Taak toevoegen" }; 

    var app = Application.Current as App; 

    var mainPage = (MenuPage)app.MainPage; 


    mainPage.Detail = new NavigationPageBar(navPage); 
} 
+0

尝试找到另一个像OnAppeared或类似的东西,每次显示视图时都会触发。 –

+0

我无法找到类似于出现的按钮的事件处理程序。 也似乎无法找到一种方法来覆盖与OnAppeared –

+0

相同的功能“它不会进入当您按下后退按钮。”我无法重现这个问题,你是怎么使用这个'FloatingActionButton'的?你是如何导航到其他页面的? –

我说忘了大小变化事件,并尝试更新直接

public class FloatingActionButtonRenderer : Xamarin.Forms.Platform.Android.AppCompat.ButtonRenderer 
{ 
    private GradientDrawable _normal, _pressed; 

    protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e) 
    { 
     base.OnElementChanged(e); 

     if (Control != null) 
     { 
      var button = (FloatingActionButton)e.NewElement; 
      this.UpdateStyle(button); 
     } 
    } 

    protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) 
    { 
     if (e.PropertyName == FloatingActionButton.HeightProperty.PropertyName || 
      e.PropertyName == FloatingActionButton.WidthProperty.PropertyName) 
     { 
      var button = (FloatingActionButton)sender; 
      UpdateStyle(button); 
     } 
     else 
     { 
      base.OnElementPropertyChanged(sender, e); 
     } 
    } 

    private void UpdateStyle(FloatingActionButton button) 
    { 
     try 
     { 
      var radius = (float)Math.Min(button.Width, button.Height) * Resources.DisplayMetrics.Density; 

      // Create a drawable for the button's normal state 
      _normal = new Android.Graphics.Drawables.GradientDrawable(); 

      if (button.BackgroundColor.R == -1.0 && button.BackgroundColor.G == -1.0 && button.BackgroundColor.B == -1.0) 
       _normal.SetColor(Android.Graphics.Color.ParseColor("#ff2c2e2f")); 
      else 
       _normal.SetColor(button.BackgroundColor.ToAndroid()); 

      _normal.SetCornerRadius(radius); 

      // Create a drawable for the button's pressed state 
      _pressed = new Android.Graphics.Drawables.GradientDrawable(); 
      var highlight = Context.ObtainStyledAttributes(new int[] { Android.Resource.Attribute.ColorActivatedHighlight }).GetColor(0, Android.Graphics.Color.Gray); 
      _pressed.SetColor(highlight); 
      _pressed.SetCornerRadius(radius); 

      // Add the drawables to a state list and assign the state list to the button 
      var sld = new StateListDrawable(); 
      sld.AddState(new int[] { Android.Resource.Attribute.StatePressed }, _pressed); 
      sld.AddState(new int[] { }, _normal); 
      Control.SetBackground(sld); 
     } 
     catch 
     { 
      //...No-op 
     } 
    } 
}