Xamarin.Forms中的ActivityIndicator [英] ActivityIndicator in Xamarin.Forms
本文介绍了Xamarin.Forms中的ActivityIndicator的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当前,我正在一个项目中,我必须从一个页面(第1页)导航到另一个页面(第2页).由于page2的加载时间很长,因此我想使用ActivityIndicator
,但是它根本无效.
Currently I am working on a project in which I have to navigate from one page (page1) to another (page2). As page2 is taking a long time to load so I was thinking to use ActivityIndicator
, but it is not effective at all.
请给我完整的代码来解决我的问题.
Please give me complete code to resolve my problem.
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;
}
推荐答案
这只是示例代码,但这是正确的逻辑,对我来说相当不错. :)
This is only a sample code, But it's logic is right and pretty good works for me. :)
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;
您可以参考链接,其中 ChaseFlorell 在Github上进行记录.
You can refer this Link, which give a clear note on Github by ChaseFlorell.
工作示例: Github
这篇关于Xamarin.Forms中的ActivityIndicator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文