Xamarin表单活动指示器未显示

时间:2018-08-30 04:40:05

标签: c# xamarin.forms visual-studio-2017 activity-indicator

我正在开发Xamarin Forms应用程序。我想在获取数据并将其从Web API绑定到ListView控件时显示一个ActivityIndi​​cator。在我的代码中,ActivityIndi​​cator完全不显示。我正在将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>

我们非常感谢您的帮助。

3 个答案:

答案 0 :(得分:1)

使用此代码:

<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();
    }

答案 1 :(得分:0)

我的猜测是,OnAppearing方法正在获取数据并同步绑定到列表视图源中,这意味着在创建视图时,数据已经被绑定,activityIndicator.IsRunning = false;消失了。因此,您实际上并没有看到它正在运行。因为当显示视图时,绑定已经完成,指示符为false

一个建议是..

Listview已经具有IsRefreshing属性,将其设置为true,您将看到指示器运行,false消失。

您还可以使用IsPullToRefreshEnabledtrue,以便用户实际上可以下拉列表视图以显示指示器正在运行并调用refresh命令,以便可以在此处执行刷新方法。

希望获得帮助

答案 2 :(得分:0)

可能发生的事情正是Luminous所说的。您正在通过activityIndicator.IsRunning = false;方法调用OnAppearing(),因此,如果抓取速度很快,您将不会看到活动指示器。尝试使用Thread.Sleep(2000)检查是否是这种情况。