xamarin - Xamarin Forms Image 未显示
问题描述
我有个问题。我想使用 IconView 来更改图像的颜色,所以我使用了这个项目:https ://github.com/andreinitescu/IconApp 。
现在,我在 IOS 和 Android 中都添加了渲染器,并将 IconView.cs 类添加到项目中。在我的 xaml 中,我尝试使用以下代码:
<controls:IconView Source="ledstrip" Grid.RowSpan="2" Grid.Column="0" Margin="5" Foreground="Red" />
但是当我运行我的应用程序时,什么都没有出现,我不知道我做错了什么?如果我创建以下图像:
<Image Source="ledstrip" Grid.RowSpan="2" Grid.Column="0" Margin="5" />
图像被显示!
这是我的完整 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:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:controls="clr-namespace:Bluepixel.Tools"
mc:Ignorable="d"
x:Class="Bluepixel.Pages.Devices">
<StackLayout Orientation="Vertical">
<ListView ItemsSource="{Binding knownDeviceList}" SelectionMode="None" RowHeight="90" ItemTapped="device_Clicked" x:Name="MyListView">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.ContextActions>
<MenuItem Command="{Binding Path=BindingContext.DeleteDevice, Source={x:Reference MyListView}}}"
CommandParameter="{Binding Id}"
Text="Delete" IsDestructive="True" />
</ViewCell.ContextActions>
<AbsoluteLayout HeightRequest="70" Margin="20,10,20,10">
<StackLayout Opacity="0.3" BackgroundColor="White"
AbsoluteLayout.LayoutBounds="0,0,1,1"
AbsoluteLayout.LayoutFlags="All" />
<StackLayout AbsoluteLayout.LayoutBounds="0,0,1,1"
AbsoluteLayout.LayoutFlags="All">
<Grid RowSpacing="0">
<Grid.RowDefinitions>
<RowDefinition Height="35" />
<RowDefinition Height="35" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="70" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="70" />
</Grid.ColumnDefinitions>
<controls:IconView Source="ledstrip" Grid.RowSpan="2" Grid.Column="0" Margin="5" Foreground="Red" />
</Grid>
</StackLayout>
</AbsoluteLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage>
我究竟做错了什么?
解决方案
这个库不是用来改变Image的颜色的,如果你在iOS
项目的renderer中看到源代码,它会改变iOS中UIImage的tintColor :
var uiImage = new UIImage(Element.Source);
uiImage = uiImage.ImageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate);
Control.TintColor = Element.Foreground.ToUIColor();
Control.Image = uiImage;
您可以查看文档和此线程以了解色调颜色的工作原理。
经过一些测试,我发现如果您使用背景透明的图像,则使用此插件可以成功显示图像。
我在这里上传了一个示例项目,您可以在资源文件夹下测试几张图片。我认为这与Android中的行为相同。
推荐阅读
- angular - 使用打字稿更改动态选择的值
- asp.net - SignalR - RedisMessageBus 过多的内存消耗
- shell - 在 shell 脚本中使用 sftp 协议从 SFTP 服务器中删除文件
- python - 如何删除双重列表?
- javascript - 有没有办法为 svelte 中的绝对路径创建配置文件(指定编译器选项),就像我们可以为 javascript 项目使用 jsconfig.json
- dpdk - tespmd 应用程序中“TX-dropped”的原因是什么?
- python - 添加命令
- kdb - wj 忽略连接中的 sym (ric) 列,导致所有股票的报价都包含在窗口中
- python - 提高我的 XOR 程序的时间和空间效率
- python - 我得到了ValueError:运行以下代码时尝试获取空序列的argmax