首页 > 解决方案 > 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>

我究竟做错了什么?

标签: xamarinxamarin.formsxamarin.androidxamarin.ios

解决方案


这个库不是用来改变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中的行为相同。


推荐阅读