我制作了一个xamarin表单登录屏幕,我想在其中放置一个徽标,但是没有显示。我已按照以下链接上的说明进行操作:https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/images?tabs=windows#using-xaml
我正在使用xaml
我尝试按照上面的链接进行操作,但是显示的只是空白图像,如果我使用以下内容:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MaisonNickel.MainPage"
Title="MainPage">
<ContentPage.Content>
<Image x:Name="cat" Aspect="AspectFit"/>
</ContentPage.Content>
</ContentPage>
也没有显示错误,我检查了过滤器,这是一个vs bug,没有刷新
答案 0 :(得分:0)
您需要向Source
添加一个Image
属性,以便显示一些内容。您可以这样做(在XAML中):
<Image x:Name="cat" Aspect="AspectFit" Source="catImage.png"/>
您的“ catImage.png”必须存在于您希望您的应用能够运行的平台的资源中。因此,对于 Android ,将图像添加到 Resources / Drawable ,对于 iOS ,将其添加到 Resources 文件夹
您还可以将图像添加到您的主项目一次,并将其用作Embedded Resource
。如果您想这样做,建议阅读this。
此外,如果您在项目中使用了大量图像并且开始失去性能,则我建议使用某种方式来缓存图像,为此,我建议使用FFImageLoading。它可以处理缓存,占位符和淡入淡出的动画,以便在加载图像时将其签出!
答案 1 :(得分:0)
我不确定您是否确实遵循了所有说明。这很简单。如果要通过嵌入式资源(在Xaml中)添加图像,则要点如下:
1)将图像导入到项目中,然后在属性窗口中将其设置为 嵌入式资源 (假设您将图像称为cat.png,放置在主文件夹中,您的程序集是MaisonNickel)
2)在主文件夹中创建一个新的Xaml标记扩展名
[ContentProperty (nameof(Source))]
public class ImageResourceExtension : IMarkupExtension
{
public string Source { get; set; }
public object ProvideValue (IServiceProvider serviceProvider)
{
if (Source == null)
{
return null;
}
// Do your translation lookup here, using whatever method you require
var imageSource = ImageSource.FromResource(Source, typeof(ImageResourceExtension).GetTypeInfo().Assembly);
return imageSource;
}
}
3)然后,您必须引用该程序集并以这种方式替换图像源:
<?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:MaisonNickel;assembly=MaisonNickel"
x:Class="MaisonNickel.MainPage">
<ContentPage.Content>
<Image x:Name="cat" Source="{local:ImageResource MaisonNickel.cat.png}" Aspect="AspectFit"/>
</ContentPage.Content>
</ContentPage>
在您的代码中,没有看到imageResource的任何引用类,也没有Source
属性。
编码愉快!