【问题标题】:Xamarin Forms Activity Indicator not showingXamarin Forms 活动指示器未显示
【发布时间】:2019-02-04 22:38:45
【问题描述】:

我正在开发一个 Xamarin Forms 应用程序。我想在获取数据并将其绑定到 Web API 的 ListView 控件时显示 ActivityIndi​​cator。在我的代码中,ActivityIndi​​cator 根本不显示。我正在使用带有 Xamarin 4.10 的 Visual Studio 2017。

以下是我的代码。

代码背后

[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>

非常感谢任何帮助。

【问题讨论】:

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


    【解决方案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();
        }
    

    【讨论】:

      【解决方案2】:

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

      一个建议是......

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

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

      希望有帮助

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          猜你喜欢
          • 2012-01-25
          • 1970-01-01
          • 1970-01-01
          • 2016-12-12
          • 1970-01-01
          • 1970-01-01
          • 2016-06-15
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多