NaN不是宽度的有效值

时间:2017-02-15 16:05:54

标签: c# xamarin xamarin.forms

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

加载ListView后,会抛出System.ArgumentException: NaN is not a valid value for width

我找到了error in the Xamarin.Forms source code,但我无法找出宽度为NaN的原因

错误

  

System.ArgumentException:NaN不是width

的有效值      

Xamarin.Forms.Size.Size(双倍宽度,双倍高度)   Xamarin.Forms.VisualElement.GetSizeRequest(double widthConstraint,double heightConstraint)   Xamarin.Forms.VisualElement.Measure(double widthConstraint,double heightConstraint,MeasureFlags flags)   Xamarin.Forms.StackLayout.CompressHorizo​​ntalLayout(StackLayout.LayoutInformation布局,double widthConstraint,double heightConstraint)   Xamarin.Forms.StackLayout.CompressNaiveLayout(StackLayout.LayoutInformation布局,StackOrientation方向,double widthConstraint,double heightConstraint)   Xamarin.Forms.StackLayout.CalculateLayout(StackLayout.LayoutInformation布局,双x,双y,双widthConstraint,double heightConstraint,bool processExpanders)   Xamarin.Forms.StackLayout.LayoutChildren(双x,双y,双宽,双高)   Xamarin.Forms.Layout.UpdateChildrenLayout()   Xamarin.Forms.Layout.OnSizeAllocated(双倍宽度,双倍高度)   Xamarin.Forms.VisualElement.SizeAllocated(双倍宽度,双倍高度)   Xamarin.Forms.VisualElement.SetSize(双倍宽度,双倍高度)   Xamarin.Forms.VisualElement.set_Bounds(矩形值)   Xamarin.Forms.VisualElement.Layout(矩形边界)   Xamarin.Forms.Layout.LayoutChildIntoBoundingRegion(VisualElement child,Rectangle region)   Xamarin.Forms.Grid.LayoutChildren(双x,双y,双宽,双高)   Xamarin.Forms.Layout.UpdateChildrenLayout()   Xamarin.Forms.Layout.OnSizeAllocated(双倍宽度,双倍高度)   Xamarin.Forms.VisualElement.SizeAllocated(双倍宽度,双倍高度)   Xamarin.Forms.VisualElement.SetSize(双倍宽度,双倍高度)   Xamarin.Forms.VisualElement.set_Bounds(矩形值)   Xamarin.Forms.VisualElement.Layout(矩形边界)   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)(托管管理到本机)   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;
    }
}

2 个答案:

答案 0 :(得分:2)

这似乎是与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

中修复

答案 1 :(得分:0)

添加一个容器(例如:框架)并指定此提示框的宽度,

<Frame x:Name="AutoCompleterContainer" Grid.Row="1" WidthRequest="400">
    <telerikInput:RadAutoComplete ....
</Frame>