ActivityIndicator 未以 xamarin 形式显示
ActivityIndicator not shown in xamarin form
///ActivityIndicator defined in xmal
<ActivityIndicator x:Name="popupView" VerticalOptions="Center" HorizontalOptions="Center" Color="DarkBlue" IsRunning="True" IsVisible="false" IsEnabled="True" />
在我的 C# 代码中:
popupView.IsVisible = true;
for (int i = 0; i < item.Count; i++)
{
dataGrid.Children.Add(new Label
{
Text = item[i].Value,
TextTransform = TextTransform.None,
HorizontalTextAlignment = TextAlignment.Center,
TextColor = Color.Black,
Padding = 1,
WidthRequest = 200,
BackgroundColor = Color.LightGray
}, i, j + 1);
}
popupView.IsVisible = false;
ActivityIndicator is not shown. What should I do?
尝试使用主线程中的代码。
Device.BeginInvokeOnMainThread(() =>
{ popupView.IsVisible = true;
for (int i = 0; i < item.Count; i++)
{
dataGrid.Children.Add(new Label
{
Text = item[i].Value,
TextTransform = TextTransform.None,
HorizontalTextAlignment = TextAlignment.Center,
TextColor = Color.Black,
Padding = 1,
WidthRequest = 200,
BackgroundColor = Color.LightGray
}, i, j + 1);
}
popupView.IsVisible = false;
});
为了看到 activity 指标,需要在某处进行延迟。
大概这是您正在做的测试,您的“真实”代码将“需要足够的时间”才能看到 activity 指标。
这是一种有效的方法。使用“睡眠”模拟您的代码需要时间。
xaml:
<!-- give appropriate name to your indicator. "popupView" sounds like a different type of element. -->
<!-- Use "IsRunning" to control visibility of ActivityIndicator, not "IsVisible". -->
<ActivityIndicator x:Name="indicator" VerticalOptions="Center" HorizontalOptions="Center" Color="DarkBlue" />
cs:
// Use "IsRunning" to control visibility of ActivityIndicator.
indicator.IsRunning = true;
Task.Run( () =>
{
// This code runs on background thread.
for (int i = 0; i < item.Count; i++)
{
// Simulate an operation that takes time.
System.Threading.Thread.Sleep(500);
...
}
Device.BeginInvokeOnMainThread( () =>
{
// This code runs on MainThread.
indicator.IsRunning = false;
});
});
注意说明哪些代码在后台线程上运行,哪些代码在 MainThread 上运行的注释。
根据你的代码,我做了一个简单的demo,运行正常。
您可以参考以下代码:
MainPage.xaml
<StackLayout>
<ActivityIndicator x:Name="popupView" VerticalOptions="Start" HorizontalOptions="FillAndExpand" IsRunning="True" IsEnabled="True" IsVisible="false" Color="Red" />
<Grid x:Name="dataGrid" ></Grid>
<Button Text="test" Clicked="Button_Clicked" HorizontalOptions="FillAndExpand" HeightRequest="50"/>
</StackLayout>
MainPage.xaml.cs
private void Button_Clicked(object sender, EventArgs e)
{
popupView.IsVisible = true;
Task.Run(() => {
Device.BeginInvokeOnMainThread(() => {
int j = 0;
for (int i = 0; i < 30; i++)
{
dataGrid.Children.Add(new Label
{
Text = "test:" + i,
TextTransform = TextTransform.None,
HorizontalTextAlignment = TextAlignment.Center,
TextColor = Color.Black,
Padding = 1,
WidthRequest = 200,
BackgroundColor = Color.LightGray
}, i, j + 1);
j = 0;
}
popupView.IsVisible = false;
});
});
}
///ActivityIndicator defined in xmal
<ActivityIndicator x:Name="popupView" VerticalOptions="Center" HorizontalOptions="Center" Color="DarkBlue" IsRunning="True" IsVisible="false" IsEnabled="True" />
在我的 C# 代码中:
popupView.IsVisible = true;
for (int i = 0; i < item.Count; i++)
{
dataGrid.Children.Add(new Label
{
Text = item[i].Value,
TextTransform = TextTransform.None,
HorizontalTextAlignment = TextAlignment.Center,
TextColor = Color.Black,
Padding = 1,
WidthRequest = 200,
BackgroundColor = Color.LightGray
}, i, j + 1);
}
popupView.IsVisible = false;
ActivityIndicator is not shown. What should I do?
尝试使用主线程中的代码。
Device.BeginInvokeOnMainThread(() =>
{ popupView.IsVisible = true;
for (int i = 0; i < item.Count; i++)
{
dataGrid.Children.Add(new Label
{
Text = item[i].Value,
TextTransform = TextTransform.None,
HorizontalTextAlignment = TextAlignment.Center,
TextColor = Color.Black,
Padding = 1,
WidthRequest = 200,
BackgroundColor = Color.LightGray
}, i, j + 1);
}
popupView.IsVisible = false;
});
为了看到 activity 指标,需要在某处进行延迟。
大概这是您正在做的测试,您的“真实”代码将“需要足够的时间”才能看到 activity 指标。
这是一种有效的方法。使用“睡眠”模拟您的代码需要时间。
xaml:
<!-- give appropriate name to your indicator. "popupView" sounds like a different type of element. -->
<!-- Use "IsRunning" to control visibility of ActivityIndicator, not "IsVisible". -->
<ActivityIndicator x:Name="indicator" VerticalOptions="Center" HorizontalOptions="Center" Color="DarkBlue" />
cs:
// Use "IsRunning" to control visibility of ActivityIndicator.
indicator.IsRunning = true;
Task.Run( () =>
{
// This code runs on background thread.
for (int i = 0; i < item.Count; i++)
{
// Simulate an operation that takes time.
System.Threading.Thread.Sleep(500);
...
}
Device.BeginInvokeOnMainThread( () =>
{
// This code runs on MainThread.
indicator.IsRunning = false;
});
});
注意说明哪些代码在后台线程上运行,哪些代码在 MainThread 上运行的注释。
根据你的代码,我做了一个简单的demo,运行正常。
您可以参考以下代码:
MainPage.xaml
<StackLayout>
<ActivityIndicator x:Name="popupView" VerticalOptions="Start" HorizontalOptions="FillAndExpand" IsRunning="True" IsEnabled="True" IsVisible="false" Color="Red" />
<Grid x:Name="dataGrid" ></Grid>
<Button Text="test" Clicked="Button_Clicked" HorizontalOptions="FillAndExpand" HeightRequest="50"/>
</StackLayout>
MainPage.xaml.cs
private void Button_Clicked(object sender, EventArgs e)
{
popupView.IsVisible = true;
Task.Run(() => {
Device.BeginInvokeOnMainThread(() => {
int j = 0;
for (int i = 0; i < 30; i++)
{
dataGrid.Children.Add(new Label
{
Text = "test:" + i,
TextTransform = TextTransform.None,
HorizontalTextAlignment = TextAlignment.Center,
TextColor = Color.Black,
Padding = 1,
WidthRequest = 200,
BackgroundColor = Color.LightGray
}, i, j + 1);
j = 0;
}
popupView.IsVisible = false;
});
});
}