2017-08-08 21 views
0

我在整個應用程序中都有活動指標,但我覺得這是重複的。我想知道我是否可以定義單個活動指標並在整個應用程序中使用它?是否有可能爲整個應用程序提供一個活動指示器?

下面是我在應用程序中的五個活動指標之一的代碼。除標籤文字改變外,其他人都完全一樣。

<AbsoluteLayout x:Name="ActivityInd" IsVisible="False" BackgroundColor="Black" Opacity=".75" AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0,0,1,1"> 
     <StackLayout AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0,0,1,1" VerticalOptions="Center" HorizontalOptions="Center"> 
      <ActivityIndicator x:Name="Activityblocker" Color="White"/> 
      <Label Text="Processing Request" TextColor="White"/> 
     </StackLayout> 
</AbsoluteLayout> 

如果可能的話,最好放在哪裏,以便它可以從應用程序的任何位置調用?

編輯:

得到的一切工作,並更換了所有與我創建的控制活動的指標。以下是代碼,希望這可以幫助未來的人。綁定的

控制XAML

<?xml version="1.0" encoding="UTF-8"?> 
<AbsoluteLayout xmlns="http://xamarin.com/schemas/2014/forms" 
      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
      x:Class="Myapp.ActivityBlocker" 
      x:Name="ActivityIndAL" IsVisible="{Binding IsBusy, Source={x:Reference ActivityIndAL}}" BackgroundColor="Black" Opacity=".75" AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0,0,1,1"> 
    <StackLayout AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0,0,1,1" VerticalOptions="Center" HorizontalOptions="Center"> 
     <ActivityIndicator x:Name="Activityblocker" Color="White" IsEnabled="{Binding IsBusy, Source={x:Reference ActivityIndAL}}" IsRunning="{Binding IsBusy, Source={x:Reference ActivityIndAL}}"/> 
     <Label Text="{Binding Text, Source={x:Reference ActivityIndAL}}" x:Name="ActivityIndLabel" TextColor="White"/> 
    </StackLayout> 
</AbsoluteLayout> 

控制xaml.cs

public partial class ActivityBlocker : AbsoluteLayout 
    { 
     public ActivityBlocker() 
     { 
      InitializeComponent(); 
     } 

     public static readonly BindableProperty TextProperty = BindableProperty.Create(nameof(Text), typeof(string), typeof(ActivityBlocker)); 
     public static readonly BindableProperty Running = BindableProperty.Create(nameof(IsBusy), typeof(bool), typeof(ActivityBlocker), false); 

     public string Text 
     { 
      get 
      { 
       return (string)GetValue(TextProperty); 
      } 
      set 
      { 
       SetValue(TextProperty, value); 
      } 
     } 

     public bool IsBusy 
     { 
      get 
      { 
       return (bool)GetValue(Running); 
      } 
      set 
      { 
       SetValue(Running, value); 
      } 
     } 

    } 

如何使用控制:您將添加到xmlns:control="clr-namespace:MyApp"要使用該控件的contentpage。現在添加控件和文本。注意:因爲在BindableProperty.Create中默認值爲false,所以不需要IsBusy =「False」。 IsBusy可以訪問和通過這樣indicator.IsBusy = true;

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
      xmlns:control="clr-namespace:MyApp" 
.... 
<ContentPage.Content> 
     <AbsoluteLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"> 
<control:ActivityBlocker x:Name="indicator" Text="Processing Request" IsBusy="False"/> 
     </AbsoluteLayout> 
</ContentPage.Content> 
.... 
+0

您可以創建自定義控件 –

+0

@YuriS這是完美的。不知道你可以在Xamarin中創建一個自定義控件。所以[這](https://xamarinhelp.com/xamarin-forms-user-control/)幫助指導的內容幾乎就是我所要做的。正確?如果是這樣,一旦我找到工作,我會放棄併發布代碼。謝謝。 – 11011

+0

是的,這是一篇很棒的文章。他正在做Grid而不是ContentView。如果你想要的佈局,但你會做AbsoluteLayout,但Grid也適合你。我也會質疑你是否需要StackLayout。您將創建例如BusyText的可綁定屬性並將其綁定到您的標籤文本 –

回答

1

這是覆蓋整個頁面從後端代碼改變?如果是這樣的話,你可以使用一個DependencyService顯示/隱藏加載指示燈您必須下載此的NuGet包爲Android/iOS版:AndHUD/BTProgressHUD這樣的:

using XXXX.Helpers; 

namespace XXXX 
{ 
    public interface IHudService 
    { 
     /// <summary> 
     /// Shows hud in the secreen 
     /// </summary> 
     /// <param name="ProgressText">Set progress</param> 
     void ShowHud(string ProgressText = StaticData.Loading); 

     /// <summary> 
     /// Hides hud. 
     /// </summary> 
     void HideHud(); 

     /// <summary> 
     /// Set text. 
     /// </summary> 
     /// <param name="Text">Set text to hub.</param> 
     void SetText(string Text); 

     /// <summary> 
     /// Set progress. 
     /// </summary> 
     /// <param name="Progress">Set progress.</param> 
     /// <param name="ProgressText">Set Text.</param> 
     void SetProgress(double Progress, string ProgressText = ""); 
    } 
} 

安卓:

using AndroidHUD; 
using Android.Views; 
using Xamarin.Forms; 
using XXXX.Droid; 
using XXXX.DependencyServices; 
using XXXX.Helpers; 

[assembly: Dependency(typeof(DroidHudService))] 

namespace XXXX.Droid 
{ 
    public class DroidHudService : IHudService 
    { 

     #region IHudManager implementation 

     bool isHudVisible; 

     public void ShowHud(string ProgressText = StaticData.Loading) 
     { 
      Device.BeginInvokeOnMainThread(() => 
      { 
       AndHUD.Shared.Show(Forms.Context, ProgressText, maskType: MaskType.Black); 
       isHudVisible = true; 

      }); 
     } 

     public void HideHud() 
     { 
      Device.BeginInvokeOnMainThread(() => 
      { 
       AndHUD.Shared.Dismiss(Forms.Context); 
       isHudVisible = false; 
      }); 
     } 

     public void SetProgress(double Progress, string ProgressText = "") 
     { 
      if (!isHudVisible) 
       return; 
      Device.BeginInvokeOnMainThread(() => 
      { 
       int progress = (int)(Progress * 100); 
       AndHUD.Shared.Show(Forms.Context, ProgressText + progress + "%", progress, MaskType.Black); 
      }); 
     } 
     public void SetText(string Text) 
     { 
      if (!isHudVisible) 
       return; 
      Device.BeginInvokeOnMainThread(() => 
      { 
       AndHUD.Shared.Show(Forms.Context, Text, maskType: MaskType.Black); 
      }); 
     } 

     Android.Views.View CustomLoadingView(string ProgressText) 
     { 
      Android.Views.View loadingView = new Android.Views.View(Forms.Context); 

      return loadingView; 
     } 

     #endregion 
    } 

} 

的iOS

using System; 
using BigTed; 
using CoreAnimation; 
using CoreGraphics; 
using XXXX.DependencyServices; 
using XXXX.Helpers; 
using XXXX.iOS; 
using Foundation; 
using UIKit; 
using Xamarin.Forms; 

[assembly: Dependency(typeof(IosHudService))] 

namespace XXXX.iOS 
{ 
    /// <summary> 
    /// Manages loading indicators in the app. 
    /// </summary> 
    public class IosHudService : IHudService 
    { 

     UIView _load; 

     bool isHudVisible; 

     #region IHudManager implementation 

     /// <summary> 
     /// Show loading indicator. 
     /// </summary> 
     /// <param name="ProgressText">Progress to set.</param> 
     public void ShowHud(string ProgressText = StaticData.Loading) 
     { 
      isHudVisible = true; 
      SetText(ProgressText); 
     } 

     /// <summary> 
     /// Hide loading indicator. 
     /// </summary> 
     public void HideHud() 
     { 
      Device.BeginInvokeOnMainThread(() => 
      { 
       BTProgressHUD.Dismiss(); 
       if (_load != null) 
        _load.Hidden = true; 
       isHudVisible = false; 
      }); 
     } 


     /// <summary> 
     /// Method to change Progress Text and show custom loader with Challenge Logo 
     /// </summary> 
     /// <param name="ProgressText">Progress text.</param> 
     public void SetProgress(double Progress, string ProgressText = "") 
     { 
      int progress = (int)(Progress * 100); 
      string text = ProgressText + progress + "%"; 
      SetText(text); 
     } 

     /// <summary> 
     /// Set text to loading indicator. 
     /// </summary> 
     /// <param name="text">Text to display.</param> 
     public void SetText(string text) 
     { 
      if (!isHudVisible) 
       return; 
      Device.BeginInvokeOnMainThread(() => 
      { 
       BTProgressHUD.Show(status: text, maskType: ProgressHUD.MaskType.Black); 

       try 
       { 
        //if (_load == null) { 
        // _load = CustomLoadingView (text); 
        // ProgressHUD.Shared.AddSubview (_load); 
        //} 
        lblTitle.Text = text; 

        UIView[] subView = ProgressHUD.Shared.Subviews; 
        for (int i = 0; i < subView.Length; i++) 
        { 
         subView[i].Hidden = true; 
        } 
        _load.Hidden = false; 
        ProgressHUD.Shared.BringSubviewToFront(_load); 
       } 
       catch (Exception ex) 
       { 
        #if DEBUG 
        Console.WriteLine("IosHudService.cs - SetText() " + ex.Message); 
        #endif 
       } 
      }); 
     } 

     UILabel lblTitle; 
     /// <summary> 
     /// Customs the loading view. 
     /// </summary> 
     /// <returns>The loading view.</returns> 
     /// <param name="ProgressText">Progress text.</param> 
     UIView CustomLoadingView(string ProgressText) 
     { 
      UIView loadingView = new UIView(); 
      loadingView.Frame = new CGRect(0, 0, UIScreen.MainScreen.Bounds.Width, UIScreen.MainScreen.Bounds.Height); 

      UIImageView imgBg = new UIImageView(); 
      imgBg.Image = UIImage.FromFile("load_bg.png"); 
      imgBg.Frame = new CGRect((loadingView.Frame.Width/2) - 65, (loadingView.Frame.Height/2) - 70, 130, 140); 
      loadingView.Add(imgBg); 

      UIImageView someImageView = new UIImageView(); 
      someImageView.Frame = new CGRect((loadingView.Frame.Width/2) - 40, (loadingView.Frame.Height/2) - 50, 75, 75); 
      someImageView.AnimationImages = new UIImage[] 
      { 
       UIImage.FromBundle("spinner.png"), 
      }; 
      someImageView.AnimationRepeatCount = nint.MaxValue; // Repeat forever. 
      someImageView.AnimationDuration = 1.0; // Every 1s. 
      someImageView.StartAnimating(); 


      CABasicAnimation rotationAnimation = new CABasicAnimation(); 
      rotationAnimation.KeyPath = "transform.rotation.z"; 
      rotationAnimation.To = new NSNumber(Math.PI * 2); 
      rotationAnimation.Duration = 1; 
      rotationAnimation.Cumulative = true; 
      rotationAnimation.RepeatCount = float.PositiveInfinity; 
      someImageView.Layer.AddAnimation(rotationAnimation, "rotationAnimation"); 
      loadingView.Add(someImageView); 


      lblTitle = new UILabel(); 
      lblTitle.Text = ProgressText; 
      lblTitle.Frame = new CGRect(imgBg.Frame.X, someImageView.Frame.Y + someImageView.Frame.Height + 15, 130, 20); 
      lblTitle.TextAlignment = UITextAlignment.Center; 
      lblTitle.TextColor = UIColor.White; 
      lblTitle.AdjustsFontSizeToFitWidth = true; 
      loadingView.Add(lblTitle); 
      return loadingView; 
     } 

     #endregion 
    } 

} 

當你需要展示或隱藏它時,你可以使用:

public static void ShowLoadingIndicator(string progressText = "Loading...") 
{ 
    Device.BeginInvokeOnMainThread(() => 
    { 
     DependencyService.Get<IHudService>().ShowHud(progressText); 
    }); 
} 

public static void HideLoadingIndicator() 
{ 
    Device.BeginInvokeOnMainThread(() => 
    { 
     DependencyService.Get<IHudService>().HideHud(); 
    }); 
} 

希望這有助於。

相關問題