在Xamarin.Forms 2.3.4.192-pre2中,我创建了一个自定义ViewCell,它使用网格作为Xamarin.Forms.ListView的DataTemplate。
当加载ListView时,它会抛出System.ArgumentException: NaN is not a valid value for width。
我已经找到了Xamarin.Forms源代码中的错误,但是我不知道为什么宽度是NaN
错误
System.ArgumentException: NaN不是宽度的有效值 Xamarin.Forms.VisualElement.GetSizeRequest(double (双宽,双高) Xamarin.Forms.StackLayout.CompressHorizontalLayout(StackLayout.LayoutInformation布局(双widthConstraint,双heightConstraint,MeasureFlags标志)Xamarin.Forms.Size.Size(双宽度,双高度) Xamarin.Forms.StackLayout.CalculateLayout(StackLayout.LayoutInformation布局,双面widthConstraint,双heightConstraint,MeasureFlags标志)Xamarin.Forms.Size.Size布局,双层widthConstraint,双heightConstraint,MeasureFlags标志)Xamarin.Forms.StackLayout.CalculateLayout(StackLayout.LayoutInformation布局,双人x,双人y,双heightConstraint,bool,EN4# x,双y,双宽,双高) Xamarin.Forms.VisualElement.SizeAllocated(double (双宽,双高) Xamarin.Forms.Layout.UpdateChildrenLayout() Xamarin.Forms.Layout.OnSizeAllocated(双宽,双高)Xamarin.Forms.VisualElement.SetSize(双宽,双高)Xamarin.Forms.VisualElement.SetSize(双宽,双高)Xamarin.Forms.VisualElement.Layout(矩形界) Xamarin.Forms.Layout.LayoutChildIntoBoundingRegion(VisualElement子区域,矩形区域)Xamarin.Forms.Grid.LayoutChildren(双x,双y,双宽度,(双高) Xamarin.Forms.VisualElement.SizeAllocated(double宽度(双高)Xamarin.Forms.Layout.OnSizeAllocated(双宽,双高)Xamarin.Forms.VisualElement.SetSize(双宽,双高)Xamarin.Forms.Layout.OnSizeAllocated值)Xamarin.Forms.VisualElement.Layout(矩形边界) Xamarin.Forms.Layout.LayoutChildIntoBoundingRegion(VisualElement子区域) Xamarin.Forms.Platform.iOS.ViewCellRenderer.ViewTableCell.LayoutSubviews() Xamarin.Forms.Platform.iOS.CellTableViewCell.GetNativeCell(UITableView tableView,Xamarin.Forms.Platform.iOS.ListViewRenderer.ListViewDataSource.GetCell(UITableView tableView,NSIndexPath indexPath) UIKit.UIApplication.UIApplicationMain(int,string[],intptr,IntPtr )(包装托管到本地) UIKit.UIApplication.Main(string[] args,IntPtr主体,IntPtr委托)UIApplication.cs:79 UIKit.UIApplication.Main(string[] args,string principalClassName,string delegateClassName)principalClassName(Args)
代码
我在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;
}
}发布于 2017-02-17 16:55:34
这似乎是Xamarin.Forms v2.3.4.192-pre2的回归。
使用Xamarin.Forms v2.3.3.180时不会引发异常。
我通过Bugzilla:bug.cgi?id=52533将错误提交给了bug.cgi?id=52533团队
更新: Xamarin.Forms 2.3.4.214-pre5中修复了此错误
发布于 2018-08-14 11:58:43
添加一个容器(例如: frame)并为这个容器指定宽度,
<Frame x:Name="AutoCompleterContainer" Grid.Row="1" WidthRequest="400">
<telerikInput:RadAutoComplete ....
</Frame>https://stackoverflow.com/questions/42254397
复制相似问题