我收到错误:
默认值与返回类型
不匹配参数名称:defaultValue
我创建了minimal, complete and verifiable example。
CustomCell.cs:
class CustomCell : ViewCell
{
private readonly Label _label;
public static readonly BindableProperty DecimalDataProperty = BindableProperty.Create("DecimalData", typeof(decimal), typeof(CustomCell), "DecimalData");
public decimal DecimalData
{
get { return (decimal)GetValue(DecimalDataProperty); }
set { SetValue(DecimalDataProperty, value); }
}
public CustomCell()
{
_label = new Label { HorizontalOptions = LayoutOptions.Start, VerticalOptions = LayoutOptions.Center, FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label)) };
_label.SetBinding(Label.TextProperty, new Binding(path: "DecimalData", stringFormat: "{0}"));
var horizontalStack = new StackLayout
{
Orientation = StackOrientation.Horizontal,
HeightRequest = 35,
Padding = new Thickness(10, 0, 10, 0)
};
horizontalStack.Children.Add(_label);
View = horizontalStack;
}
}
的Page1.xaml:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:app8="clr-namespace:App8;assembly=App8"
x:Class="App8.Page1">
<ListView x:Name="listView">
<ListView.ItemTemplate>
<DataTemplate>
<app8:CustomCell DecimalData="{Binding DecimalData}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage>
Page1.xaml.cs:
public class Model
{
public decimal DecimalData { set; get; }
}
public partial class Page1 : ContentPage
{
public Page1()
{
try
{
InitializeComponent();
var list = new List<Model>
{
new Model {DecimalData = 565},
new Model {DecimalData = 876},
new Model {DecimalData = 123}
};
listView.ItemsSource = list;
}
catch (Exception ex)
{
}
}
}
问题很简单:如何将十进制值绑定到自定义ViewCell?
这可能是个错误吗?
答案 0 :(得分:1)
在Xamarin developer site for BindableProperty
列出的两个Create方法中,第四个参数是属性的默认值。
对于decimal
,这应该是一个数值(可能是0),但是你要传递属性的名称(&#34; DecimalData&#34;)。
替换第二个&#34; DecimalData&#34;使用0
或(因为参数是一个对象,所以你必须更明确)0m