【发布时间】:2020-04-03 02:16:35
【问题描述】:
我面临一个问题,如果我在 ListView 的项目模板内提供我的框架,当我删除一个对象时,我会得到一个 FrameRenderer 异常,特别是 Unable to activate instance of type Xamarin.Forms.Platform.Android.FrameRenderer。但是,在 iOS 上,这不是问题。如果我为拐角半径设置一个静态值,错误就会消失。
我需要让视图模型控制框架半径的原因是我们正在尝试创建圆,并且我们注意到为了实现圆,android 必须有一个角半径是其宽度的两倍和半径。所以如果 W=R=20 则圆角半径为 40。对于 iOS 则相反,圆角半径为一半。
要绑定的 XAML:
<ListView SelectedItem="{Binding SelectedUser, Mode=TwoWay}" ItemsSource="{Binding Users}" HasUnevenRows="True" SeparatorVisibility="None">
<ListView.ItemTemplate>
<DataTemplate>
<customcontrols:NoHighlightCell>
<StackLayout effects:RoundCornersEffect.CornerRadius="8" Orientation="Horizontal" HorizontalOptions="FillAndExpand" VerticalOptions="Start" Spacing="0" Margin="0, 5, 0, 5">
<BoxView HeightRequest="1" WidthRequest="5" VerticalOptions="FillAndExpand" HorizontalOptions="Start" BackgroundColor="{Binding SelectedColor}"></BoxView>
<StackLayout Opacity="{Binding Opacity}" Padding="5, 5, 0, 5" VerticalOptions="StartAndExpand" HorizontalOptions="FillAndExpand" Orientation="Horizontal" BackgroundColor="{Binding CellBackgroundColor}">
<Frame Padding="0" WidthRequest="40" HeightRequest="40" CornerRadius="{Binding CornerRadiusForPhone}" BackgroundColor="{Binding InitialCircleColor}" VerticalOptions="Start" HorizontalOptions="Start" HasShadow="False">
<Label Padding="0" Text="{Binding UserInitials}" TextColor="{Binding LetterColoring}" FontAttributes="Bold" FontSize="14" HorizontalOptions="Center" HorizontalTextAlignment="Start" VerticalTextAlignment="Center"></Label>
</Frame>
<StackLayout Spacing="0" Orientation="Horizontal" Padding="20, 0, 0, 0" HorizontalOptions="FillAndExpand">
<StackLayout Orientation="Vertical" HorizontalOptions="Fill" VerticalOptions="Fill">
<Label FontAttributes="Bold" Text="{Binding FullName}" VerticalOptions="EndAndExpand"/>
<Label Text="{Binding Location}" VerticalOptions="EndAndExpand"></Label>
</StackLayout>
<customcontrols:UnderlineLabel TextDecorations="Underline" Padding="0, 0, 14, 0" Text="Remove" TextColor="#BB3734" FontSize="13" HorizontalOptions="EndAndExpand" HorizontalTextAlignment="Start" VerticalTextAlignment="Center" IsVisible="{Binding ManagingUsers}">
<customcontrols:UnderlineLabel.GestureRecognizers>
<TapGestureRecognizer NumberOfTapsRequired="1" Command="{Binding Path=BindingContext.RemoveUserCommand, Source={x:Reference root}}" CommandParameter="{Binding .}"></TapGestureRecognizer>
</customcontrols:UnderlineLabel.GestureRecognizers>
</customcontrols:UnderlineLabel>
</StackLayout>
</StackLayout>
</StackLayout>
</customcontrols:NoHighlightCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
其视图模型中的CornerRadiusForPhone 代码:
public float CornerRadiusForPhone {
get {
if(Device.RuntimePlatform == Device.Android)
{
return 80f;
} else
{
return 20f;
}
}
}
导致崩溃的部分代码位于与 XAML 关联的视图模型中,它只是从列表的 ItemSource 的 Users ObservableCollection 中删除选定的项目。
private void RemoveUser(object obj)
{
try
{
var toRemove = (RegisteredUsersViewModel) obj;
tokenService.Logout(toRemove.UserId);
Users.Remove(toRemove);
} catch (Exception e)
{
Console.Write(e);
}
}
通常我更喜欢在 xaml 中使用 OnPlatform,但除非我弄错了,因为 CornerRadius 是一个浮点数并且没有浮点类型参数,所以我不能使用那个技巧。
【问题讨论】:
标签: c# xamarin.forms xamarin.android