I have a xamarin forms application. With the following code I try to show an image:
var image = new Image { Source = "launcher_foreground.png" };
The problem is that it doesn't show the image. The image is included under Sources in the Android specific project. Also, even the icon.png isn't showing, the default image included in the Xamarin forms app. The images are also set to AndroidResource.
What might be the problem here?
1.Please make sure your image has been placed under Resources/drawable directory with Build Action: AndroidResource.
2.The code you using is right.
public MainPage()
{
InitializeComponent();
var image = new Image { Source = "launcher_foreground.png" };
Content = image;
}
3.You can't load the icon.png as it is not in the Resources/drawable directory and the icon is configured in the Android project.
4.High- and low-DPI versions of an image can also be supplied (in appropriately named Resources subdirectories such as drawable-ldpi, drawable-hdpi, and drawable-xhdpi).
There are examples in the document. Also try to clean/rebuild your solution if the image still does not show.
Refer: local-images
try this code snippet , may be help you.
var image = new Image { Source = ImageSource.FromFile("launcher_foreground.png")}
Related
I'm using an older version of Xamarin.Forms (3.6) and I'm trying to save a WebView as a PNG so I can view it offline. I found a way to save the image using Forms9Patch (1.3.3).
public void saveWebLicense()
{
var htmlLicenseSource = LicenseSource;
var fileName = $"{CurrentLicense.customerId}WebViewPNG";
Forms9Patch.HtmlStringExtensions.ToPng(htmlLicenseSource.Html, fileName, async (string path) =>
{
if (path != null)
{
System.Diagnostics.Debug.WriteLine("A PNG of your HTML is here: " + path);
}
});
}
Which does fine in saving the png to a filename that looks like this.
/Users/firstname.lastname/Library/Developer/CoreSimulator/Devices/8C7D30F5-F30E-48F6-9584-357193BD5CBB/data/Containers/Data/Application/BE627F14-1EC2-46C8-B015-8F57929111C3/tmp/12345WebViewPNG.png
I can navigate to this folder and see the PNG just fine. The issue is it's stored in a tmp folder and as soon as I exit the app it's gone, so I'm unable to load it. I can't change the save location that Forms9Patch gives me, so my thought was to try and move the image to a different local non-temporary folder or copy it. I need something that works for iOS and Android, and I won't be able to upgrade Xamarin.Forms or Forms9Patch at this time, so I can't use any of their new features. I've tried using File.Move but that function isn't even recognized even when adding System and SystemIO.
I'm also open to other suggestions if they work with older versions of Xamarin Forms.
I have a WebView that shows the terms and conditions when the user starts the app. That WebView contains a few links. Currently, I have the terms and conditions as a html file that I store in resources.
In the code-behind I fill the WebView via:
public partial class Agb : ContentPage
{
public Agb()
{
InitializeComponent();
BindingContext = new ViewModel.AgbViewModel();
Web.Source = new HtmlWebViewSource
{
Html = Properties.Resources.Agb
};
Web.Navigating += WebViewNavigating;
}
XML:
<WebView
x:Name="Web"
VerticalOptions="FillAndExpand"
Margin="50,0"/>
Most of the time this works well. On UWP this WebView sometimes fails to show. It always shows on my own computer but randomly fails in production.
Is there anything I can do to get the WebView to work reliably?
Option 1:
I've made a pull request to fix this bug in Xamarin.Forms, so simply update Xamarin.Forms to a version, where it will be merged.
However, while the fix is not yet there and while the fix may not be available for earlier versions of Xamarin.Forms before 4.4.0, here's how to fix it in your project:
Option 2
Add the WebViewRenderer_fix.cs file to your project. This file is based on v4.4.0, but if you see build errors, you may need to apply the fix yourself to earlier version of this file.
And don't forget to register it as a custom renderer for your WebView
[assembly: ExportRenderer(typeof(WebView), typeof(WebViewRenderer_fix))]
I have the following Image asset defined (FSDirectButton)...
How do I specify a button to use this as it's image in the Xamarin xib designer?
When I choose the Image option in the properties window all I see are images that are stored in my Images folder.
I inherited this app which was originally done in a older version of Xamarin iOS, so not sure if that is affecting things or not...
However I want to use the image asset library for my buttons, but I am not sure how to specify in the designer which image to use? Or do I have to do it runtime in code?
Look at this video. It can help you, but raise some new bug. Enjoy
Also you can set the Image from Asset catalog in your ViewDidLoad() method.
For example:
//get image set from asset catalog
var imageFromBunde = UIImage.FromBundle("ImageSet"); //ImageSet - name of your image set in Asset catalog
myImage.Image = imageFromBundle;
The image is not showing with the below code in Xamarin. Its in images folder.
var image = new Image { Aspect = Aspect.AspectFit };
image.Source = "images/image1.png";
Content = image;
How can I fix this ?
Note : I'm using a Forms application.
You have to place the images in the right folder of each platform-project.
iOS - Place images in the Resources folder with Build Action: BundleResource
Android - Place images in the Resources/drawable directory with Build Action: AndroidResource.
Windows Phone / Windows / UWP - Place images in the application's
root directory with Build Action: Content
You can find more Infos here.
Edit (images in PCL-project)
To embed an image in a project, right-click to add new items and
select the image/s you wish to add. By default the image will have
Build Action: None; this needs to be set to Build Action:
EmbeddedResource.
And then load the Image FromResource(..):
var embeddedImage = new Image { Aspect = Aspect.AspectFit };
embeddedImage.Source = ImageSource.FromResource("test.jpg");
To load images from the PCL-Project -> look at this page.
Is there any kind of restriction when accessing image through widgets extension?
I have an image within Widget extension Supporting Files group called pocket.png
I want to use this image through by Widget custom view controller and as usual i wrote this code
var pocketImage:UIImage = UIImage(named: "pocket")
To my surprise this code returns blank. Tried with pocket.png same problem.
Now i tried to do with image with path and even the below code returns nil
let pathx = NSBundle.mainBundle().pathForResource("pocket", ofType: "png")
So how do i access local available image?
Note: The same code works fine when i use it in the main application with its local image.
There was an issue with xcode which didn't copy the images properly. I had to delete and copy them again to fix the issue.