2

我已經制作了一個渲染器來在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'的?你是如何導航到其他頁面的? –

回答

1

我說忘了大小變化事件,並嘗試更新直接

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 
     } 
    } 
} 
相關問題