【发布时间】:2019-11-17 09:15:12
【问题描述】:
尝试将 double 转换为星单位的 GridLength 时出错。转换后的本身工作正常,但运行时出现错误
"System.ArgumentException: NaN 不是 D:\a\1\s 中 Xamarin.Forms.Size..ctor (System.Double width, System.Double height) [0x00008] 处宽度的有效值\n \Xamarin.Forms.Core\Size.cs:19 \n 在 D:\a\1 中的 Xamarin.Forms.Grid.ContractColumnsIfNeeded(System.Double 宽度,System.Func`2[T,TResult] 谓词)[0x00075] \s\Xamarin.Forms.Core\GridCalc.cs:213 \n 在 D:\a\1 中的 Xamarin.Forms.Grid.MeasureAndContractStarredColumns(System.Double 宽度、System.Double 高度、System.Double totalStarsWidth)[0x000bb] \s\Xamarin.Forms.Core\GridCalc.cs:397 \n 在 D:\a\1 中的 Xamarin.Forms.Grid.MeasureGrid(System.Double 宽度,System.Double 高度,System.Boolean requestSize)[0x0010e] \s\Xamarin.Forms.Core\GridCalc.cs:509 \n 在 D:\a\1\s\Xamarin 中的 Xamarin.Forms.Grid.OnSizeRequest (System.Double widthConstraint, System.Double heightConstraint) [0x0002a]。 Forms.Core\GridCalc.cs:58 \n 在 Xamarin.Forms.VisualElement.OnMeasure (System.Double widthConstraint, System.Double heightConstraint) [0x00000] 在 D:\一种\ \1\s\Xamarin.Forms.Core\VisualElement.cs:641 \n 在 Xamarin.Forms.VisualElement.GetSizeRequest (System.Double widthConstraint, System.Double heightConstraint) [0x00053] 在 D:\a\1\s\ Xamarin.Forms.Core\VisualElement.cs:523 \n 在 Xamarin.Forms.Layout.GetSizeRequest (System.Double widthConstraint, System.Double heightConstraint) [0x00000] 在 D:\a\1\s\Xamarin.Forms.Core \Layout.cs:131 \n 在 D:\a\1\s\Xamarin.Forms 中的 Xamarin.Forms.VisualElement.Measure(System.Double widthConstraint、System.Double heightConstraint、Xamarin.Forms.MeasureFlags 标志)[0x00054] .Core\VisualElement.cs:581 \n 在 Xamarin.Forms.Grid.MeasureStarredRows () [0x000c3] 在 D:\a\1\s\Xamarin.Forms.Core\GridCalc.cs:548 \n 在 Xamarin.Forms .Grid.MeasureAndContractStarredRows (System.Double width, System.Double height, System.Double totalStarsHeight) [0x00000] 在 D:\a\1\s\Xamarin.Forms.Core\GridCalc.cs:403 \n 在 Xamarin.Forms .Grid.MeasureGrid(System.Double 宽度,System.Double 高度,System.Boolean requestSize)[0x0011 7] 在 D:\a\1\s\Xamarin.Forms.Core\GridCalc.cs:510 \n 在 Xamarin.Forms.Grid.OnSizeRequest (System.Double widthConstraint, System.Double heightConstraint) [0x0002a] 在 D: \a\1\s\Xamarin.Forms.Core\GridCalc.cs:58 \n 在 Xamarin.Forms.VisualElement.OnMeasure (System.Double widthConstraint, System.Double heightConstraint) [0x00000] 在 D:\a\1\ s\Xamarin.Forms.Core\VisualElement.cs:641 \n 在 Xamarin.Forms.VisualElement.GetSizeRequest (System.Double widthConstraint, System.Double heightConstraint) [0x00053] 在 D:\a\1\s\Xamarin.Forms .Core\VisualElement.cs:523\n 在 D:\a\1\s\Xamarin.Forms.Core\Layout 中的 Xamarin.Forms.Layout.GetSizeRequest (System.Double widthConstraint, System.Double heightConstraint) [0x00000]。 cs:131 \n 在 D:\a\1\s\Xamarin.Forms.Core\ 中的 Xamarin.Forms.VisualElement.Measure (System.Double widthConstraint, System.Double heightConstraint, Xamarin.Forms.MeasureFlags flags) [0x00054] VisualElement.cs:581 \n 在 Xamarin.Forms.StackLayout.CalculateNaiveLayout (Xamarin.Forms.Stac kLayout+LayoutInformation 布局,Xamarin.Forms.StackOrientation 方向,System.Double x,System.Double y,System.Double widthConstraint,System.Double heightConstraint) [0x000a8] 在 D:\a\1\s\Xamarin.Forms.Core \StackLayout.cs:163 \n 在 Xamarin.Forms.StackLayout.CalculateLayout(Xamarin.Forms.StackLayout+LayoutInformation 布局,System.Double x,System.Double y,System.Double widthConstraint,System.Double heightConstraint,System.Boolean processExpanders ) [0x00058] 在 D:\a\1\s\Xamarin.Forms.Core\StackLayout.cs:123 \n 在 Xamarin.Forms.StackLayout.OnSizeRequest (System.Double widthConstraint, System.Double heightConstraint) [0x00019] 在D:\a\1\s\Xamarin.Forms.Core\StackLayout.cs:80 \n 在 Xamarin.Forms.VisualElement.OnMeasure (System.Double widthConstraint, System.Double heightConstraint) [0x00000] 在 D:\a\ 1\s\Xamarin.Forms.Core\VisualElement.cs:641 \n 在 Xamarin.Forms.VisualElement.GetSizeRequest (System.Double widthConstraint, System.Double heightConstraint) [0x00053] 在 D:\a\ 1\ s\Xamarin.Forms.Core\VisualElement.cs:523 \n 在 Xamarin.Forms.Layout.GetSizeRequest (System.Double widthConstraint, System.Double heightConstraint) [0x00000] 在 D:\a\1\s\Xamarin.Forms .Core\Layout.cs:131 \n 在 Xamarin.Forms.VisualElement.Measure (System.Double widthConstraint, System.Double heightConstraint, Xamarin.Forms.MeasureFlags flags) [0x00054] 在 D:\a\1\s\Xamarin .Forms.Core\VisualElement.cs:581 \n 在 D:\a\1\s\Xamarin.Forms.Core\ 中的 Xamarin.Forms.Grid.CalculateAutoCells(System.Double 宽度,System.Double 高度)[0x000e5] GridCalc.cs:131 \n 在 Xamarin.Forms.Grid.MeasureGrid (System.Double width, System.Double height, System.Boolean requestSize) [0x0000c] 在 D:\a\1\s\Xamarin.Forms.Core\ GridCalc.cs:483 \n 在 Xamarin.Forms.Grid.OnSizeRequest (System.Double widthConstraint, System.Double heightConstraint) [0x0002a] 在 D:\a\1\s\Xamarin.Forms.Core\GridCalc.cs:58 \n 在 Xamarin.Forms.VisualElement.OnMeasure (System.Double widthConstraint, System.Double heightConstraint) [0x00000] 在 D:\a\1\s\Xamarin.Forms.Core\VisualElement.cs:641 \n 在 Xamarin.Forms.VisualElement.GetSizeRequest (System.Double widthConstraint, System.Double heightConstraint) [0x00053] 在 D:\a \1\s\Xamarin.Forms.Core\VisualElement.cs:523 \n 在 Xamarin.Forms.Layout.GetSizeRequest (System.Double widthConstraint, System.Double heightConstraint) [0x00000] 在 D:\a\1\s\ Xamarin.Forms.Core\Layout.cs:131 \n 在 D:\a\1\ 中的 Xamarin.Forms.VisualElement.Measure (System.Double widthConstraint, System.Double heightConstraint, Xamarin.Forms.MeasureFlags flags) [0x00054] s\Xamarin.Forms.Core\VisualElement.cs:581 \n 在 Xamarin.Forms.ScrollView.OnSizeRequest (System.Double widthConstraint, System.Double heightConstraint) [0x0005d] 在 D:\a\1\s\Xamarin.Forms .Core\ScrollView.cs:233 \n 在 Xamarin.Forms.VisualElement.OnMeasure (System.Double widthConstraint, System.Double heightConstraint) [0x00000] 在 D:\a\1\s\Xamarin.Forms.Core\VisualElement。 cs:641 \n 在 Xamarin.Forms.VisualElement.GetSizeRequest (System.Dou ble widthConstraint, System.Double heightConstraint) [0x00053] 在 D:\a\1\s\Xamarin.Forms.Core\VisualElement.cs:523 \n 在 Xamarin.Forms.Layout.GetSizeRequest (System.Double widthConstraint, System. Double heightConstraint) [0x00000] 在 D:\a\1\s\Xamarin.Forms.Core\Layout.cs:131 \n 在 Xamarin.Forms.VisualElement.Measure (System.Double widthConstraint, System.Double heightConstraint, Xamarin. Forms.MeasureFlags 标志)[0x00054] 在 D:\a\1\s\Xamarin.Forms.Core\VisualElement.cs:581\n 在 Xamarin.Forms.StackLayout.CalculateNaiveLayout (Xamarin.Forms.StackLayout+LayoutInformation 布局,Xamarin .Forms.StackOrientation 方向, System.Double x, System.Double y, System.Double widthConstraint, System.Double heightConstraint) [0x000a8] 在 D:\a\1\s\Xamarin.Forms.Core\StackLayout.cs:163 \n 在 Xamarin.Forms.StackLayout.CalculateLayout(Xamarin.Forms.StackLayout+LayoutInformation 布局,System.Double x,System.Double y,System.Double widthConstraint,System.Double heightConstraint,System.Boolea亲 cessExpanders) [0x00058] 在 D:\a\1\s\Xamarin.Forms.Core\StackLayout.cs:123 \n 在 Xamarin.Forms.StackLayout.LayoutChildren (System.Double x, System.Double y, System.Double width, System.Double height) [0x0005b] 在 D:\a\1\s\Xamarin.Forms.Core\StackLayout.cs:57 \n 在 Xamarin.Forms.Layout.UpdateChildrenLayout () [0x0014b] 在 D:\ a\1\s\Xamarin.Forms.Core\Layout.cs:263 \n 在 Xamarin.Forms.Layout.OnSizeAllocated (System.Double width, System.Double height) [0x0000f] 在 D:\a\1\s \Xamarin.Forms.Core\Layout.cs:223\n 在 D:\a\1\s\Xamarin.Forms 中的 Xamarin.Forms.VisualElement.SizeAllocated(System.Double 宽度,System.Double 高度)[0x00000]。 Core\VisualElement.cs:679 \n 在 D:\a\1\s\Xamarin.Forms.Core\VisualElement.cs 中的 Xamarin.Forms.VisualElement.SetSize (System.Double width, System.Double height) [0x00021] :908 \n 在 Xamarin.Forms.VisualElement.set_Bounds (Xamarin.Forms.Rectangle 值) [0x0005d] 在 D:\a\1\s\Xamarin.Forms.Core\VisualElement.cs:186 \n 在 Xamarin.Forms .VisualElement.L ayout (Xamarin.Forms.Rectangle bounds) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\VisualElement.cs:565 \n 在 Xamarin.Forms.Layout.LayoutChildIntoBoundingRegion (Xamarin.Forms.VisualElement child , Xamarin.Forms.Rectangle 区域) [0x001da] 在 D:\a\1\s\Xamarin.Forms.Core\Layout.cs:177\n 在 Xamarin.Forms.Page.LayoutChildren (System.Double x, System. Double y, System.Double width, System.Double height) [0x0010d] in D:\a\1\s\Xamarin.Forms.Core\Page.cs:187 \n 在 Xamarin.Forms.Page.UpdateChildrenLayout () [ 0x000c6] 在 D:\a\1\s\Xamarin.Forms.Core\Page.cs:259 \n 在 Xamarin.Forms.Page.OnSizeAllocated (System.Double width, System.Double height) [0x0000f] 在 D: \a\1\s\Xamarin.Forms.Core\Page.cs:240 \n 在 Xamarin.Forms.VisualElement.SizeAllocated (System.Double width, System.Double height) [0x00000] 在 D:\a\1\ s\Xamarin.Forms.Core\VisualElement.cs:679 \n 在 D:\a\1\s\Xamarin.Forms 中的 Xamarin.Forms.VisualElement.SetSize (System.Double width, System.Double height) [0x00021] .Core\VisualEleme nt.cs:908 \n 在 Xamarin.Forms.VisualElement.set_Bounds (Xamarin.Forms.Rectangle 值) [0x0005d] 在 D:\a\1\s\Xamarin.Forms.Core\VisualElement.cs:186 \n 在Xamarin.Forms.VisualElement.Layout (Xamarin.Forms.Rectangle 边界) [0x00000] 在 D:\a\1\s\Xamarin.Forms.Core\VisualElement.cs:565\n 在 Xamarin.Forms.Layout.LayoutChildIntoBoundingRegion ( Xamarin.Forms.VisualElement 孩子,Xamarin.Forms.Rectangle 区域)[0x0005f] 在 D:\a\1\s\Xamarin.Forms.Core\Layout.cs:146\n 在 Xamarin.Forms.Page.LayoutChildren(系统.Double x, System.Double y, System.Double width, System.Double height) [0x0010d] 在 D:\a\1\s\Xamarin.Forms.Core\Page.cs:187 \n 在 Xamarin.Forms。 Page.UpdateChildrenLayout () [0x000c6] 在 D:\a\1\s\Xamarin.Forms.Core\Page.cs:259 \n 在 Xamarin.Forms.Page.OnSizeAllocated (System.Double 宽度,System.Double 高度) [0x0000f] 在 D:\a\1\s\Xamarin.Forms.Core\Page.cs:240 \n 在 Xamarin.Forms.VisualElement.SizeAllocated (System.Double width, System.Double height) [0x00000] 在 D :\一种\ 1\s\Xamarin.Forms.Core\VisualElement.cs:679 \n 在 Xamarin.Forms.VisualElement.SetSize (System.Double width, System.Double height) [0x00021] 在 D:\a\1\s\Xamarin .Forms.Core\VisualElement.cs:908 \n 在 D:\a\1\s\Xamarin.Forms.Core\VisualElement.cs 中的 Xamarin.Forms.VisualElement.set_Bounds (Xamarin.Forms.Rectangle 值)[0x0005d] :186 \n 在 Xamarin.Forms.VisualElement.Layout (Xamarin.Forms.Rectangle 边界) [0x00000] 在 D:\a\1\s\Xamarin.Forms.Core\VisualElement.cs:565 \n 在 Xamarin.Forms .Layout.LayoutChildIntoBoundingRegion(Xamarin.Forms.VisualElement 子,Xamarin.Forms.Rectangle 区域)[0x0005f] 在 D:\a\1\s\Xamarin.Forms.Core\Layout.cs:146\n 在 Xamarin.Forms。 Page.LayoutChildren (System.Double x, System.Double y, System.Double width, System.Double height) [0x0010d] 在 D:\a\1\s\Xamarin.Forms.Core\Page.cs:187 \n在 Xamarin.Forms.Page.UpdateChildrenLayout () [0x000c6] 在 D:\a\1\s\Xamarin.Forms.Core\Page.cs:259 \n 在 Xamarin.Forms.Page.OnSizeAllocated (System.Double 宽度,西 stem.Double height) [0x0000f] in D:\a\1\s\Xamarin.Forms.Core\Page.cs:240 \n 在 Xamarin.Forms.VisualElement.SizeAllocated (System.Double width, System.Double height) [0x00000] 在 D:\a\1\s\Xamarin.Forms.Core\VisualElement.cs:679 \n 在 Xamarin.Forms.VisualElement.SetSize (System.Double width, System.Double height) [0x00021] 在 D :\a\1\s\Xamarin.Forms.Core\VisualElement.cs:908 \n 在 D:\a\1\s\ 中的 Xamarin.Forms.VisualElement.set_Bounds (Xamarin.Forms.Rectangle 值)[0x0005d] Xamarin.Forms.Core\VisualElement.cs:186\n 在 D:\a\1\s\Xamarin.Forms.Core\VisualElement 中的 Xamarin.Forms.VisualElement.Layout(Xamarin.Forms.Rectangle 边界)[0x00000]。 cs:565 \n 在 D:\a\1\s\Xamarin.Forms.Core\Layout.cs 中的 Xamarin.Forms.Layout.LayoutChildIntoBoundingRegion(Xamarin.Forms.VisualElement 子,Xamarin.Forms.Rectangle 区域)[0x0005f] :146 \n 在 D:\a\1\s\Xamarin.Forms 中的 Xamarin.Forms.Page.LayoutChildren(System.Double x、System.Double y、System.Double 宽度、System.Double 高度)[0x00103]。 C ore\Page.cs:185 \n 在 Xamarin.Forms.Page.UpdateChildrenLayout () [0x000c6] 在 D:\a\1\s\Xamarin.Forms.Core\Page.cs:259 \n 在 Xamarin.Forms。 Page.OnSizeAllocated (System.Double width, System.Double height) [0x0000f] in D:\a\1\s\Xamarin.Forms.Core\Page.cs:240 \n 在 Xamarin.Forms.VisualElement.SizeAllocated (System .Double width, System.Double height) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\VisualElement.cs:679 \n 在 Xamarin.Forms.VisualElement.SetSize (System.Double width, System .Double height) [0x00021] in D:\a\1\s\Xamarin.Forms.Core\VisualElement.cs:908 \n 在 Xamarin.Forms.VisualElement.set_Bounds (Xamarin.Forms.Rectangle 值) [0x0005d] in D:\a\1\s\Xamarin.Forms.Core\VisualElement.cs:186 \n 在 D:\a\1\s 中的 Xamarin.Forms.VisualElement.Layout (Xamarin.Forms.Rectangle 边界)[0x00000] \Xamarin.Forms.Core\VisualElement.cs:565 \n 在 D:\a\1\s\Xamarin.Forms.Platform.iOS\Renderers 中的 Xamarin.Forms.Platform.iOS.NavigationRenderer.ViewDidLayoutSubviews () [0x00172] \导航重新 nderer.cs:182 \n at (wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain(int,string[],intptr,intptr)\n at UIKit.UIApplication.Main (System.String[] args, System. IntPtr 主体,System.IntPtr 委托)[0x00005] 在 /Library/Frameworks/Xamarin.iOS.framework/Versions/12.4.0.64/src/Xamarin.iOS/UIKit/UIApplication.cs:79 \n 在 UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0002c] 在 /Library/Frameworks/Xamarin.iOS.framework/Versions/12.4.0.64/src/Xamarin.iOS/UIKit/UIApplication.cs :63 \n 在 PrenCare.iOS.Application.Main (System.String[] args) [0x00002] 在 C:\Users\ondre\source\repos\PrenCare\src\Version02\PrenCare\PrenCare.iOS\Main.cs :19"
。奇怪的是,当我使用绝对单位时,转换器工作得很好。
转换器
public class NumberToGridLengthConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (!(value is double numberValue))
{
throw new Exception($"Type {value.GetType().FullName} cannot be converted to GridLenght");
}
return new GridLength(numberValue, GridUnitType.Star); // Star makes it crash
//return GridLength.Star; // Star makes it crash as well
//return new GridLength(numberValue); // This works fine
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (!(value is GridLength gridLength))
{
throw new Exception($"Type {value.GetType().FullName} is not GridLenght");
}
return gridLength.Value;
}
}
XAML
<ContentPage.Resources>
<ResourceDictionary>
<conv:NumberToGridLengthConverter x:Key="numberToGridLengthConverter" />
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
<StackLayout>
<Grid Grid.Column="1" ColumnSpacing="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{Binding Path=MovementCalm, Mode=TwoWay, Converter={StaticResource numberToGridLengthConverter}}" />
<ColumnDefinition Width="{Binding Path=MovementRapid, Mode=TwoWay, Converter={StaticResource numberToGridLengthConverter}}" />
</Grid.ColumnDefinitions>
<BoxView Grid.Column="0" BackgroundColor="LightGreen" HorizontalOptions="FillAndExpand" HeightRequest="20" />
<BoxView Grid.Column="1" BackgroundColor="Orange" HorizontalOptions="FillAndExpand" HeightRequest="20" />
</Grid>
</StackLayout>
</ContentPage.Content>
视图模型
public class StatisticsViewModel : BaseViewModel
{
private double _movementCalm;
public double MovementCalm
{
get => _movementCalm;
set => SetProperty(ref _movementCalm, value);
}
private double _movementRapid;
public double MovementRapid
{
get => _movementRapid;
set => SetProperty(ref _movementRapid, value);
}
public StatisticsViewModel()
{
}
public override void OnAppearing()
{
base.OnAppearing();
SetMovement();
}
private void SetMovement()
{
MovementCalm = 90f;
MovementRapid = 10f;
}
}
public abstract class BaseViewModel : INotifyPropertyChanged
{
public virtual void OnAppearing()
{
}
public virtual void OnBindingContextChanged()
{
}
public virtual void OnDisappearing()
{
}
#region INotifyPropertyChanged
protected bool SetProperty<T>(
ref T backingStore,
T value,
[CallerMemberName]string propertyName = "",
Action onChanged = null)
{
if (EqualityComparer<T>.Default.Equals(backingStore, value))
{
return false;
}
backingStore = value;
onChanged?.Invoke();
OnPropertyChanged(propertyName);
return true;
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
var changed = PropertyChanged;
if (changed == null)
{
return;
}
changed.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
知道如何让它与 Star 单位一起使用吗?
【问题讨论】:
-
“我在运行时出错”——具体的错误是什么,是哪一行导致的?
标签: c# xamarin xamarin.forms data-binding converters