我正在开发一个Xamarin Forms应用程序。我想在获取数据并将其绑定到 Web API 的 ListView 控件时显示一个 ActivityIndicator。在我的代码中,活动指示器根本不显示。我正在使用Visual Studio 2017和Xamarin 4.10。
以下是我的代码。
代码隐藏
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class TestPage : ContentPage
{
private SfListView listView;
private ActivityIndicator activityIndicator;
public TestPage()
{
InitializeComponent();
StackLayout mainStackLayout = new StackLayout();
activityIndicator = new ActivityIndicator()
{
IsRunning = true,
IsEnabled = true,
IsVisible = true
};
listView = new SfListView()
{
SelectionMode = SelectionMode.None,
BackgroundColor = Color.White
};
mainStackLayout.Children.Add(activityIndicator);
mainStackLayout.Children.Add(listView);
this.Content = mainStackLayout;
this.Title = "Dashboard";
}
protected override void OnAppearing()
{
SummaryRepository viewModel = new SummaryRepository();
listView.ItemsSource = viewModel.Summary;
listView.ItemTemplate = new DataTemplate(() =>
{
var grid = new Grid();
var bookName = new Label
{
BackgroundColor = Color.White,
FontSize = 16,
TextColor = Color.FromHex("#1A237E")
};
var bookDescription = new Label
{
BackgroundColor = Color.White,
FontSize = 16,
HorizontalTextAlignment = TextAlignment.End,
TextColor = Color.FromHex("#EC407A")
};
bookName.SetBinding(Label.TextProperty, new Binding("Name"));
bookDescription.SetBinding(Label.TextProperty, new Binding("Amount"));
grid.Children.Add(bookName);
grid.Children.Add(bookDescription, 1, 0);
return grid;
});
activityIndicator.IsRunning = false;
base.OnAppearing();
}
}
XAML 文件
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="eWallet.Pages.TestPage">
<ContentPage.Content>
<StackLayout>
</StackLayout>
</ContentPage.Content>
任何帮助都非常感谢。
使用以下代码:
<ContentPage.Content>
<AbsoluteLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<StackLayout BackgroundColor="#F5F5F5" VerticalOptions="FillAndExpand" AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0,0,1,1">
//Your Listview
</StackLayout>
<StackLayout Padding="12" x:Name="DisplayLoading" IsVisible="False"
AbsoluteLayout.LayoutFlags="PositionProportional"
AbsoluteLayout.LayoutBounds="0.5,0.5,-1,-1">
<ActivityIndicator Color="#d7813e" IsRunning="True"/>
<Label Text="Loading..." TextColor="#d7813e" FontSize="Small"/>
</StackLayout>
</AbsoluteLayout>
</ContentPage.Content>
并在cs文件中使用:
protected override void OnAppearing()
{
activityIndicator.IsRunning = true;
SummaryRepository viewModel = new SummaryRepository();
listView.ItemsSource = viewModel.Summary;
listView.ItemTemplate = new DataTemplate(() =>
{
var grid = new Grid();
var bookName = new Label
{
BackgroundColor = Color.White,
FontSize = 16,
TextColor = Color.FromHex("#1A237E")
};
var bookDescription = new Label
{
BackgroundColor = Color.White,
FontSize = 16,
HorizontalTextAlignment = TextAlignment.End,
TextColor = Color.FromHex("#EC407A")
};
bookName.SetBinding(Label.TextProperty, new Binding("Name"));
bookDescription.SetBinding(Label.TextProperty, new Binding("Amount"));
grid.Children.Add(bookName);
grid.Children.Add(bookDescription, 1, 0);
return grid;
});
activityIndicator.IsRunning = false;
base.OnAppearing();
}
我的猜测是OnAppearing
方法正在获取数据并同步绑定到 listview 源中,这意味着在创建视图时,数据已经绑定,activityIndicator.IsRunning = false;
消失了指示器。所以你实际上看不到它运行。因为当显示视图时,绑定已经完成,指示器false
。
一个建议是..
Listview 已经有IsRefreshing
属性,将其设置为true
,您将看到指示器正在运行,false
消失。
您还可以使用IsPullToRefreshEnabled
进行true
,以便用户可以实际下拉列表视图以显示指示器正在运行并调用刷新命令,以便您可以在此处执行刷新方法。
希望这个帮助
也许,正在发生的事情正是Luminous所说的。您正在OnAppearing()
方法上调用activityIndicator.IsRunning = false;
,因此,如果提取速度很快,您将不会看到活动指示器。尝试使用Thread.Sleep(2000)
来检查是否是这种情况。