본문 바로가기

카테고리 없음

MAUI 로딩바 샘플 동적 Dim 처리

반응형

1. 구현

 public static class CommonLoader
 {
     private static ContentPage Page { get; set; }

     private static Grid DynamicGrid { get; set; }

     private static ActivityIndicator ActivityIndicator { get; set; }

     public static void Show(this ContentPage page)
     {
         Page = page;

         if (DynamicGrid == null && ActivityIndicator == null)
         {
             DynamicGrid = new Grid
             {
                 IsVisible = true,
                 RowSpacing = 0
             };

             Frame frame = new Frame
             {
                 BackgroundColor = Colors.Black,
                 Opacity = 0.6,
                 BorderColor = Colors.Transparent,
                 CornerRadius = 0,
                 Padding = new Thickness(0),
                 Margin = new Thickness(-2)
             };

             ActivityIndicator = new ActivityIndicator
             {
                 IsRunning = true,
                 WidthRequest = 200,
                 HeightRequest = 100
             };

             DynamicGrid.Children.Add(frame);
             DynamicGrid.Children.Add(ActivityIndicator);

             if (Page.Content is Grid layout)
             {
                 layout.Add(DynamicGrid);
                 layout.SetRowSpan(DynamicGrid, layout.Children.Count);
             }
         }
     }

     public static void Close(this ContentPage page)
     {
         Page = page;

         DynamicGrid?.Remove(ActivityIndicator);

         DynamicGrid = null;
         ActivityIndicator = null;
     } 
 }

 

 

2. 적용

try
{
    //로딩바 열기
    CommonLoader.Show(this);

    await Task.Delay(5000);
}
catch (Exception ex)
{
     
}
finally
{
    //로딩바 닫기
    CommonLoader.Close(this);
}