【问题标题】:ActivityIndicator in Xamarin.FormsXamarin.Forms 中的 ActivityIndi​​cator
【发布时间】:2018-08-07 13:01:29
【问题描述】:

目前我正在做一个项目,我必须从一个页面 (page1) 导航到另一个页面 (page2)。由于 page2 需要很长时间才能加载,所以我想使用ActivityIndicator,但它根本没有效果。

请给我完整的代码来解决我的问题。

    Page(1)
    //code for tapping a label and to navigate to page2 
void onpage1tapped(Object sender,EventArgs args)
{  label1.Opacity=0.5;
   Device.StartTimer(TimeSpan.FromMilliseconds(100),()=>{label1.Opacity=1;return false;   })
   This.Navigate.PushAsync(new Page2());
} 

   Page(2)
 //constructor for page2
 public page2
 {   InitializeComponent();
      ActivityIndicator indicator=new ActivityIndicator{HorizontalOptions=LayoutOptions.CenterAndExpand,Color=Color.Black,IsVisible=false};
      StackLayout stk=new StackLayout();
      stk.Children.Add(indicator);
    //Enabling activityindicator  
      indicator.IsRunning=true;
      indicator.IsVisible=true;

   //Below long time taking task
      Assembly assembly = GetType().GetTypeInfo().Assembly;
        string resource = String.Format("ks.texts.BST.txt");
        //string resource = "advjava.texts.insertion.txt";
        using (Stream s = assembly.GetManifestResourceStream(resource))
        {
            using (StreamReader sr = new StreamReader(s))
            {
                string line;
                while (null != (line = sr.ReadLine()))
                {

                    Label l = new Label { Text = line, TextColor = Color.FromRgb(110, 139, 69), FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label)) };
                    stk.Children.Add(l);

                }
            }
        }           

       //Disabling Activity indicator as the long task is complete
    indicator.IsVisible=false;
    indicator.IsRunning=false;
 }

【问题讨论】:

  • 我已经推荐了关注但没有帮助forums.xamarin.com/discussion/22309/…
  • 请给我这个问题的完整代码而不是建议链接,因为我已经看过所有在线帮助。
  • 基本上这都是关于页面加载的正确指示
  • 向我们展示您的代码? :-)

标签: xamarin xamarin.forms


【解决方案1】:

使用Acr.XamForms.UserDialogs Package。看看它..希望它有帮助

Here 是关于如何实现 Acr.XamForms.UserDialogs 的文档。 希望对你有帮助

【讨论】:

  • 先生,看看我的代码,如果可能的话更正一下。
【解决方案2】:

这只是一个示例代码,但它的逻辑是正确的,对我来说效果很好。 :)

        var indicator = new ActivityIndicator() {
            HorizontalOptions = LayoutOptions.CenterAndExpand
        };
        indicator.SetBinding(ActivityIndicator.IsVisibleProperty, "IsBusy", BindingMode.OneWay);
        indicator.SetBinding(ActivityIndicator.IsRunningProperty, "IsBusy", BindingMode.OneWay);

        var button = new Button() {
            HorizontalOptions = LayoutOptions.Fill,
            Text = "BUTTON"
        };

        button.Clicked += (sender, e) => IsBusy = !IsBusy;

        var root = new StackLayout() {
            HorizontalOptions = LayoutOptions.FillAndExpand,
            VerticalOptions = LayoutOptions.FillAndExpand,
            Children = {
                button, 
                indicator
            }
        };

        Content = root;

你可以参考这个Link,它在 Github 上由 ChaseFlorell 给出了明确的说明。

工作示例:Github

【讨论】:

  • 不,这行不通。这不仅是显示活动指示器,而且显示它以隐藏一些耗时的过程。请先看我的代码。
【解决方案3】:

问题在于您的代码是如何被读取的。它会完成,然后执行,但如果您使用 Task.Run,​​您将能够在单独的线程上运行,并保持该活动指示器启动并运行,以显示您的页面正在加载并且您的机器很忙。

void onpage1tapped(Object sender,EventArgs args) {  
    activityIndicator.IsVisible= true;
    Task.Run( () =>  {
        label1.Opacity=0.5;
        Device.StartTimer(TimeSpan.FromMilliseconds(100),()= {
            label1.Opacity=1;returnfalse;   
        })
        This.Navigate.PushAsync(new Page2());
    });
}     

【讨论】:

    猜你喜欢
    • 2015-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多