我正在尝试构建具有一些其他属性的自定义控件:
public class EntryWithBorder : Entry
{
public static readonly BindableProperty IsCurvedCornersEnabledProperty =
BindableProperty.Create(
"IsCurvedCornersEnabled",
typeof(bool),
typeof(EntryWithBorder),
true);
public bool IsCurvedCornersEnabled
{
get { return (bool)GetValue(IsCurvedCornersEnabledProperty); }
set { SetValue(IsCurvedCornersEnabledProperty, value); }
}
}
然后我要在页面中使用自定义控件:
<?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:local="clr-namespace:App.CustomControls;assembly=App"
x:Class="App.View.LoginPage"
BackgroundColor="{StaticResource BackgroundColor}">
<ScrollView>
<Grid RowSpacing="0" ColumnSpacing="25">
<Grid.RowDefinitions>
<RowDefinition Height="AUTO"/>
<RowDefinition Height="AUTO"/>
<RowDefinition Height="AUTO"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<BoxView BackgroundColor="White" Grid.Row="0" HeightRequest="50"/>
<!--header spacing-->
<BoxView BackgroundColor="White" Grid.Row="1"/>
<Image Source="test.PNG" Aspect="AspectFit" HorizontalOptions="CenterAndExpand"/>
<!-- <Image Source="CurvedLimiter.png" VerticalOptions="End" HeightRequest="50" Aspect="Fill"/>-->
<!--header-->
<BoxView BackgroundColor="White" Grid.Row="2" HeightRequest="100"/>
<StackLayout Grid.Row="1">
<local:EntryWithBorder IsCurvedCornersEnabled="True" Placeholder="Email" Text="super@super.de" x:Name="emailEntry" Style="{StaticResource LoginEntry}"/>
<Entry IsPassword="True" Placeholder="Password" Text="super" x:Name="passwordEntry" Style="{StaticResource LoginEntry}"/>
<Switch x:Name="autoLogin" IsToggled="True" HorizontalOptions="Center"/>
<Button Text="Login" x:Name="btnLogin" Clicked="btnLogin_Clicked" Style="{StaticResource LoginButton}"/>
</StackLayout>
<!--login-->
<BoxView BackgroundColor="White" Grid.Row="3"/>
</Grid>
</ScrollView>
</ContentPage>
找到了自定义控件“ local:EntryWithBorder”,但是找不到可绑定的属性“ IsCurvedCornersEnabled”。相反,我收到一个错误XLS0413,在“ EntryWithBorder”类型内找不到该属性。
有什么想法吗?
谢谢!
编辑2018-09-16:可以通过重新启动VS解决此问题。但是,我必须为添加到代码中的每个新BindableProperty重新启动VS。
所以,我也遇到了一个新错误:将以下属性添加到代码中后,在初始化应用程序表单时就会出现异常:
public static readonly BindableProperty Corner123RadiussProperty =
BindableProperty.Create(
nameof(Corner123Radiuss),
typeof(double),
typeof(EntryWithBorder),
7);
// Gets or sets CornerRadius value
public double Corner123Radiuss
{
get { return (double)GetValue(Corner123RadiussProperty); }
set { SetValue(Corner123RadiussProperty, value); }
}
奇怪的是,我现在甚至没有从XAML代码中引用此属性。在LoginPage中的InitializeComponents()方法中引发异常:
System.TypeInitializationException: The type initializer for 'App.CustomControls.EntryWithBorder' threw an exception.
目前我没有更多信息。
我将项目包装在以下文件中:VS Project
答案 0 :(得分:0)
更改此
public static readonly BindableProperty Corner123RadiussProperty =
BindableProperty.Create(
nameof(Corner123Radiuss),
typeof(double),
typeof(EntryWithBorder),
7);
收件人
public static readonly BindableProperty Corner123RadiussProperty =
BindableProperty.Create(
nameof(Corner123Radiuss),
typeof(double),
typeof(EntryWithBorder),
7.0);
此可绑定属性为double
类型,使用7
设置默认值将作为整数处理,因此必须为7.0