【发布时间】:2019-02-20 06:47:01
【问题描述】:
我正在尝试构建具有一些附加属性的自定义控件:
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 代码中引用此属性。在 InitializeComponents() 方法中的 LoginPage 中抛出异常:
System.TypeInitializationException: The type initializer for 'App.CustomControls.EntryWithBorder' threw an exception.
目前我没有得到更多信息。
我将项目打包在一个文件中:VS Project
【问题讨论】:
-
查看该代码对我来说看起来不错,提供一个示例项目以便我们可以运行并查看实际导致此问题的原因?
-
似乎是VS的问题。退出并重新启动 VS 后,可以在 XAML 中使用这些属性。但是我必须为我创建的每个新的 BindableProperty 重新启动 VS ......我也遇到了一个新问题。我会更新上面的帖子。
-
用可下载的项目更新了帖子。提前致谢!