【问题标题】:Xamarin.Forms layout is empty on iOSXamarin.Forms 布局在 iOS 上为空
【发布时间】:2019-11-21 22:09:45
【问题描述】:

我有一个 Xamarin.Forms 应用程序,它使用 Alex Rainman 的 CarouselView (https://github.com/alexrainman/CarouselView) 来呈现一个轮播(惊喜!:))。轮播中的每个页面都包含一个项目列表。在 Android 上看起来像这样:

在 Windows 上类似。但是,在 iOS 上我得到了这个:

起初我认为这是 CarouselView 组件中的错误并提交了issue,但作者说它在他这边工作正常。我试图以我能想到的任何方式对其进行调试,但找不到问题。 CarouselView 被实例化并添加到视图层次结构中。如果我用显示列的 ListView 替换它,ViewModel 就会被实例化并且工作正常。现在我没有想法,非常感谢一些帮助!

这是我的代码:

MainPage.xaml:

<controls:CarouselViewControl Orientation="Horizontal" 
                                  InterPageSpacing="0"
                                  Position="0" 
                                  ItemsSource="{Binding Columns}" 
                                  VerticalOptions="FillAndExpand" 
                                  HorizontalOptions="FillAndExpand"
                                  BackgroundColor="Gray"
                              ShowIndicators="True"
                              ShowArrows="True">
    <controls:CarouselViewControl.ItemTemplate>
        <DataTemplate>
            <ContentView HorizontalOptions="FillAndExpand"
                             VerticalOptions="FillAndExpand"                                 
                             Padding="20">
                <ListView HorizontalOptions="FillAndExpand"
                              VerticalOptions="FillAndExpand"
                              BackgroundColor="LightGray"
                              Header="{Binding Name}"
                              ItemsSource="{Binding Tasks}"
                              ItemSelected="ListView_ItemSelected">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ImageCell ImageSource="{Binding Image}"
                                           Text="{Binding Title}"/>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </ContentView>
        </DataTemplate>
    </controls:CarouselViewControl.ItemTemplate>
</controls:CarouselViewControl>

(后面的代码只是在构造函数中调用InitializeComponent())

ColumnsViewModel.cs:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace ProofOfConcept
{
    public class ColumnsViewModel : BindableObject
    {
        public static readonly BindableProperty PositionProperty = BindableProperty.Create(nameof(Position), typeof(int), typeof(ColumnsViewModel), 0, BindingMode.TwoWay);

        public ObservableCollection<Column> Columns { get; set; }

        public int Position
        {
            get { return (int)this.GetValue(PositionProperty); }
            set { this.SetValue(PositionProperty, value); }
        }

        public ColumnsViewModel()
        {
            Columns = new ObservableCollection<Column>();

            Random random = new Random();
            for (int i = 0; i < 10; i++)
            {
                Column column = new Column
                {
                    Name = String.Format("Column {0}", i),
                    Background = Color.FromRgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255))
                };

                ObservableCollection<Task> tasks = new ObservableCollection<Task>();
                for (int t = 0; t < 25; t++)
                {
                    Task task = new Task
                    {
                        Title = String.Format("Column #{0}, Task #{1}", i, t),
                        Color = Color.Lavender
                    };
                    tasks.Add(task);
                }
                column.Tasks = tasks;

                Columns.Add(column);
            }
        }
    }
}

列.cs:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace ProofOfConcept
{
    public class Column
    {
        public string Name { get; set; }
        public ObservableCollection<Task> Tasks { get; set; }
        public Color Background { get; set; }
        public string Image
        {
            get
            {
                return "http://loremflickr.com/100/100";
            }
        }
    }
}

Task.cs:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace ProofOfConcept
{
    public class Task : BindableObject
    {
        public static readonly BindableProperty ColorProperty = BindableProperty.Create(nameof(Color), typeof(Color), typeof(Task), Color.White, BindingMode.TwoWay);

        public string Title { get; set; }
        public string Assignee
        {
            get
            {
                return "Ivan";
            }
        }

        public Color Color
        {
            get { return (Color)this.GetValue(ColorProperty); }
            set { this.SetValue(ColorProperty, value); }
        }

        public string Image
        {
            get
            {
                return "http://loremflickr.com/100/100";
            }
        }
    }
}

另外,整个解决方案的here is a zip (没有 Windows 项目,因为它非常大而且并不真正相关)。

【问题讨论】:

    标签: xamarin xamarin.forms


    【解决方案1】:

    Usage

    在您的 iOS 和 Android 项目中调用:

    Xamarin.Forms.Init();
    CarouselViewRenderer.Init();
    
    
    
    
    
        namespace ProofOfConcept.iOS
        {
            // The UIApplicationDelegate for the application. This class is responsible for launching the 
            // User Interface of the application, as well as listening (and optionally responding) to 
            // application events from iOS.
            [Register("AppDelegate")]
            public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
            {
                //
                // This method is invoked when the application has loaded and is ready to run. In this 
                // method you should instantiate the window, load the UI into it and then make the window
                // visible.
                //
                // You have 17 seconds to return from this method, or iOS will terminate your application.
                //
                public override bool FinishedLaunching(UIApplication app, NSDictionary options)
                {
                    global::Xamarin.Forms.Forms.Init();
                    CarouselViewRenderer.Init();
                    LoadApplication(new App());
    
                    return base.FinishedLaunching(app, options);
                }
            }
        }
    

    【讨论】:

    • 该死!我怎么会错过这个!?感谢您一直指出我眼前的一切!附言显然 Android 不需要 init 调用。
    【解决方案2】:

    如果有人在调用“init”后在 iOS 中遇到此问题。您可以尝试向 CarouselViewControl 发出高度请求。如果控件位于网格中并且您将 Auto 用于 RowDefinitin.Height,则通常会出现这种情况

    【讨论】:

      猜你喜欢
      • 2014-07-28
      • 2021-08-27
      • 1970-01-01
      • 2020-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多