c# - NaN 不是有效的宽度值

标签 c# xamarin xamarin.forms

在 Xamarin.Forms 2.3.4.192-pre2 中,我创建了一个自定义 ViewCell,它使用 Xamarin.Forms.ListView 的 DataTemplate 的网格.

加载ListView时,它会抛出System.ArgumentException:NaN不是有效的宽度值

我找到了error in the Xamarin.Forms source code ,但我不明白为什么宽度会是 NaN

错误

System.ArgumentException: NaN is not a valid value for width

Xamarin.Forms.Size.Size(double width, double height) Xamarin.Forms.VisualElement.GetSizeRequest(double widthConstraint, double heightConstraint) Xamarin.Forms.VisualElement.Measure(double widthConstraint, double heightConstraint, MeasureFlags flags) Xamarin.Forms.StackLayout.CompressHorizontalLayout(StackLayout.LayoutInformation layout, double widthConstraint, double heightConstraint) Xamarin.Forms.StackLayout.CompressNaiveLayout(StackLayout.LayoutInformation layout, StackOrientation orientation, double widthConstraint, double heightConstraint) Xamarin.Forms.StackLayout.CalculateLayout(StackLayout.LayoutInformation layout, double x, double y, double widthConstraint, double heightConstraint, bool processExpanders) Xamarin.Forms.StackLayout.LayoutChildren(double x, double y, double width, double height) Xamarin.Forms.Layout.UpdateChildrenLayout() Xamarin.Forms.Layout.OnSizeAllocated(double width, double height) Xamarin.Forms.VisualElement.SizeAllocated(double width, double height) Xamarin.Forms.VisualElement.SetSize(double width, double height) Xamarin.Forms.VisualElement.set_Bounds(Rectangle value) Xamarin.Forms.VisualElement.Layout(Rectangle bounds) Xamarin.Forms.Layout.LayoutChildIntoBoundingRegion(VisualElement child, Rectangle region) Xamarin.Forms.Grid.LayoutChildren(double x, double y, double width, double height) Xamarin.Forms.Layout.UpdateChildrenLayout() Xamarin.Forms.Layout.OnSizeAllocated(double width, double height) Xamarin.Forms.VisualElement.SizeAllocated(double width, double height) Xamarin.Forms.VisualElement.SetSize(double width, double height) Xamarin.Forms.VisualElement.set_Bounds(Rectangle value) Xamarin.Forms.VisualElement.Layout(Rectangle bounds) Xamarin.Forms.Layout.LayoutChildIntoBoundingRegion(VisualElement child, Rectangle region) Xamarin.Forms.Platform.iOS.ViewCellRenderer.ViewTableCell.LayoutSubviews() Xamarin.Forms.Platform.iOS.CellTableViewCell.GetNativeCell(UITableView tableView, Cell cell, bool recycleCells, string templateId) Xamarin.Forms.Platform.iOS.ListViewRenderer.ListViewDataSource.GetCell(UITableView tableView, NSIndexPath indexPath) UIKit.UIApplication.UIApplicationMain(int, string[], intptr, intptr)(wrapper managed-to-native) UIKit.UIApplication.Main(string[] args, IntPtr principal, IntPtr delegate)UIApplication.cs:79 UIKit.UIApplication.Main(string[] args, string principalClassName, string delegateClassName)UIApplication.cs:63 MondayPundayApp.iOS.Application.Main(string[] args)Main.cs:17

代码

我在 GitHub 上的这个存储库中整理了一个复制品: https://github.com/brminnick/Xamarin.Forms-NaN-is-not-a-valid-value-for-width-reproduction

ListView

var listView = new ListView(ListViewCachingStrategy.RecycleElement)
{
    BackgroundColor = Color.White,
    RowHeight = 200,
    ItemTemplate = new DataTemplate(typeof(PuzzleCellCardView)),
    SeparatorColor = Color.Transparent
};

ViewCell

public class PuzzleCellCardView : ViewCell
{
    Image _puzzleImage;
    Label _punNumberValueLabel;
    Image _questionMarkImage;
    Image _checkImage;

    public PuzzleCellCardView()
    {
        var puzzleImage = new Image
        {
            HeightRequest = 150,
            BackgroundColor = Color.White
        };

        var punNumberTextLabel = new Label
        {
            Text = " Pun Number",
            Style = StyleConstants.LabelStyle
        };

        _punNumberValueLabel = new Label
        {
            Style = StyleConstants.LabelStyle
        };

        var puzzleNumberStackLayout = new StackLayout
        {
            Children = {
                punNumberTextLabel,
                _punNumberValueLabel
            },
            Orientation = StackOrientation.Horizontal,
            BackgroundColor = Color.White
        };

        _questionMarkImage = new Image
        {
            Source = App.ImageConstants.QuestionMark,
            MinimumHeightRequest = 100,
            BackgroundColor = Color.White
        };

        _checkImage = new Image
        {
            Source = App.ImageConstants.Check,
            MinimumHeightRequest = 100,
            BackgroundColor = Color.White
        };

        var whitePuzzleNumberBackgroundBoxView = new BoxView
        {
            BackgroundColor = Color.White
        };

        var cellGridLayout = new Grid
        {
            BackgroundColor = Color.Black,
            Padding = new Thickness(2),
            RowSpacing = 2,
            ColumnSpacing = 1,
            VerticalOptions = LayoutOptions.Fill,

            RowDefinitions = {
                new RowDefinition{ Height = new GridLength (20, GridUnitType.Absolute) },
                new RowDefinition{ Height = new GridLength (150, GridUnitType.Absolute) }
            },
            ColumnDefinitions = {
                new ColumnDefinition{ Width = new GridLength (1, GridUnitType.Star) },
                new ColumnDefinition{ Width = new GridLength (1, GridUnitType.Star) }
            }
        };
        cellGridLayout.Children.Add(whitePuzzleNumberBackgroundBoxView, 0, 0);
        Grid.SetColumnSpan(whitePuzzleNumberBackgroundBoxView, 2);

        cellGridLayout.Children.Add(puzzleNumberStackLayout, 0, 0);
        Grid.SetColumnSpan(puzzleNumberStackLayout, 2);

        cellGridLayout.Children.Add(_puzzleImage, 0, 1);

        cellGridLayout.Children.Add(_checkImage, 1, 1);
        cellGridLayout.Children.Add(_questionMarkImage, 1, 1);

        if (Device.OS == TargetPlatform.Android)
            _puzzleImage.InputTransparent = true;

        View = cellGridLayout;
    }
}

最佳答案

这似乎是 Xamarin.Forms v2.3.4.192-pre2 的回归。

使用 Xamarin.Forms v2.3.3.180 时不会引发异常。

我通过 Bugzilla 向 Xamarin.Forms 团队提交了错误:https://bugzilla.xamarin.com/show_bug.cgi?id=52533

更新:此错误已在 Xamarin.Forms 2.3.4.214-pre5 中修复

关于c# - NaN 不是有效的宽度值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42254397/

相关文章:

c# - 使用 Linq 的字典列表 <Type>

c# - 我可以在不在 C# 中进行强耦合的情况下使对象的生命周期依赖于另一个对象吗?

c# - 是否可以 "launch"一个(大)文件的副本而不等待结果?

ios - 在不阻塞标签栏的情况下显示屏蔽的 BTProgressHUD?

c# - Xamarin.Forms - 如何引发 slider ValueChanged 事件?

c# - 需要覆盖导航方法

c# - WPF TextBlock 绑定(bind)不起作用

c# - Xamarin 文件处理文档 - SpecialFolder.LocalApplicationData 不存在

android - 在内部存储 ANDROID 上创建 SQLite 数据库

ios - 在 Xamarin.Forms iOS 上设置 ScrollView 的 z-index