Xamarin.Forms embedded image not showing - xamarin.forms

There are several posts here on stackoverflow that cover this, but none are the cause. I've tried several sources including the documentation and I can not get this to work.
I'm following a tutorial by mosh on Udemy and he's showing how to use an embedded resource file.
I am working with Visual Studio 2017.
I've added a folder in the HelloWorld folder called images and placed an image called background.jpg in it.
When you right click the image and set the Build Action to EmbeddedReesource you're supposed to get a Resource Id. None is there, not even the box shows up. I'm thinking that's a new feature that was removed.
I found out the resource path is HelloWorld.images.background.jpg.
My xaml is like this:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="HelloWorld.ImagePage"
BackgroundColor="Black">
<ContentPage.Content>
<Image x:Name="image" Aspect="AspectFill" />
</ContentPage.Content>
</ContentPage>
Next, my c# code is like this:
public ImagePage ()
{
InitializeComponent ();
image.Source = ImageSource.FromResource("HelloWorld.images.background.jpg");
}
And I've tried placing the image in the android project under Resources/Drawable, but doesn't that defeat the purpose?
Any help would be great. Thanks ahead of time.
Here is my VS Solution Explorer:

This way of embedding images is still supported. Check out this page in the documentation on how to do it. These are the basic steps:
Set Build Action of the file to EmbeddedResource, you can do this in the properties of the file
Refer to the image from your PCL with a dot delimited identifier. The filename is build up like this: AssemblyName.Folder.ImageName.ext
If your image is not in a folder, you can leave is out, if it is in multiple folders, add each one separated with a dot. An example could look like this:
var embeddedImage = new Image { Aspect = Aspect.AspectFit };
embeddedImage.Source = ImageSource.FromResource("WorkingWithImages.beach.jpg");
Here the assembly (probably your project name for your PCL) is named 'WorkingWithImages' and the file is not in a folder, the file is named 'beach.jpg'. In the case of Android I think you should also mind that you do not put in hyphens or any other characters than numeric, alphabetic and underscores.
That being said, you also say:
And I've tried placing the image in the android project under
Resources/Drawable, but doesn't that defeat the purpose?
I'm not entirely sure what you mean here, but I took it that you think it defeats the purpose of a multi-platform app from a single code-base. I must disagree with you there. Although I see how you could think that, working with images is something that is very platform specific. All platforms have a very different way of showing and scaling images and it would be nearly impossible to reconcile all that for Xamarin. Having the images in the PCL like you want to do now, will completely disable the whole scaling mechanism in place and suddenly you are responsible for making it look good on all kinds of devices, resolutions and form factors. That is why I always have the images resources per platform project.

Related

Xamarin Forms: Exactly how specify local filename for iOS WebView WebViewSource

I have a Xamarin Forms Webview that is successfully displaying a local "hard-coded" file on Android and UWP, but I can't get it to work on iOS. I have done the usual searches: there is help out there for Web URL's and for HTML embedded in the C# code, but I cannot find anything that solves my problem: exactly how do I specify the filename and where do I put the HTML files?
I have pasted the XAML below. I have the GettingStartedWizardWebPage folder both (temporarily, until I figure out what works) in the main iOS project folder and also under iOS Resources. I have tried specifying the file name both with and without a preceding ///. Is there some other prefix I should be using, like file: for Android or ms-appx-web: for UWP? I have the HTML files with BuildAction BundleResource and I have tried all of the Copy variations (but there are a lot of combinations of name prefix, Copy options and file location and I have probably not tried every combination).
Specifically, I have read https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/webview?tabs=windows#local-html-content and followed the directions there to the best of my ability. For Android I coded Value="file:///folder/page" and it works. For UWP I coded Value="ms-appx-web:///folder/page" and it works. What do I code for Value= for iOS? What is the "base URL"? Is the tutorial trying to say that I need code to determine the Base URL?
It doesn't crash or give any errors; it just displays a blank window. The second row of the grid shows up and displays where I would expect it to be.
I have read the discussions of UIWebView and WKWebView but I don't get what exactly I should be doing. AFAIK I would be willing to use either.
VS 2022 Community 17.4.0 Preview on Windows 64 Pro, with both Cloud iPhone 13 Simulator and real iPhone 5.
<Grid RowDefinitions="*,AUTO" Margin="6, 6, 6, 6" x:Name="MainContent">
<WebView WidthRequest="1000" HeightRequest="1000" Grid.Row="0" >
<WebView.Source>
<OnPlatform x:TypeArguments="WebViewSource">
<OnPlatform.Platforms>
<On Platform="Android" Value="file:///android_asset/GettingStartedWizardWebPage/WebPage.html" />
<On Platform="UWP" Value="ms-appx-web:///GettingStartedWizardWebPage/WebPage.html" />
<On Platform="iOS" Value="///GettingStartedWizardWebPage/WebPage.html" />
</OnPlatform.Platforms>
</OnPlatform>
</WebView.Source>
</WebView>
...
Okay. I solved this. The linked tutorial is less than forthcoming on one point, and wrong on the details of others.
I. You just cannot do this for iOS in XAML. For UWP and Android there is a fixed, short "prefix" (base URL) that goes on the front of the filename as discussed in the article and as shown in my XAML above. For iOS it is a 100-or-so character string that is very application and device dependent, so you can't hard-code it in the XAML. You have to use C# code with a dependency routine -- as shown in the tutorial, but not emphasized as explicitly as I just did.
II. The prefixes shown in the tutorial are wrong, or at least won't work correctly in device-independent code. NSBundle.MainBundle.BundlePath on iOS returns a pathname without a terminating slash, so (a.) you want a leading slash on the hard-coded "low-order" part of your filename; and (b.) therefore the Android routine wants to return "file:///android_asset" (without the trailing slash shown in the tutorial) and the UWP routine wants to return "ms-appx-web://" with two trailing slashes, not the three shown in the tutorial -- which seems odd, but your hard-coded slash will make for three total.
III. Contrary to what the tutorial says, you do NOT have to read the file yourself and then deal with special considerations so that the HTML page can access linked objects. Just put BaseUrl + "Your hard-coded filename starting with a slash" into WebView.Source and WebView will read the file (on all three platforms) including any linked images and so forth.
IV. Put the HTML pages in the main iOS application folder, not under Resources. That is the path that NSBundle.MainBundle.BundlePath returns.

Provide WebView with Url to Asset

In my Uno UWP project, I can view an html Asset file (w/ Content BuildAction), stored in my shared project, using the following:
var myAssetUri = new Uri("ms-appx-web:///Assets/Html/index.html");
myWebView.Navigate(myAssetUri);
However, this does not work with Android or WASM (not yet tried on other platforms). On WASM, I get a blank page. On Android, I get a page with the message:
The webpage at ms-appx-web:///Assets/Html/index.html could not be loaded because:
net::ERR_UNKNOWN_URL_SCHEME
When I look at the package folders for both platforms, I do find my html file:
WASM: bin/Debug/netstardard2.0/dist/package_.../Assets/Html/index.html
Android: (unziping my app's apk) assets/Html/index.html
so, I am guessing I'm not doing something right ... but I don't know what that might be.
The cause is simple: WebView not implemented yet on Wasm nor Skia.
This is the solution after hours investing!!
For you proyect write this line:
var myAssetUri = new Uri("file:///android_asset/Assets/Assets/Html/index.html");
This work 100%

Unable to add an image in the storyboard editor in Visual Studio to create a splash screen for Xamarin.iOS

I'm trying to add a splash screen to an iOS app built using Xamarin.Forms. I'm able to get the splash screen itself to display, but I'm unable to add an image to an ImageView in the storyboard editor.
I can replicate this behavior in a brand-new "Cross Platform" solution, and I'm following the instructions here. Specifically, at step 10 in the instructions, you're supposed to be able to select the image from a dropdown.
If you actually click on the arrow of what only appears to be a dropdown, however, nothing actually "drops down"; instead VS opens an OpenFileDialog entitled "Add Existing Item".
No matter what file I choose, VS only adds it to the project's Resource folder (as a BundleResource), but doesn't change the image in the storyboard at all.
I've tried adding the images to the Asset Catalog as both a Launch Image and a regular Image Set. I've tried typing either a filename or an asset set name into the editable part of the "dropdown"; no luck. I've also tried editing the storyboard XML to add an image= attribute (to the ImageView tag), again using either a filename or an asset set name, but it doesn't work, and subsequent saves from the storyboard editor actually remove the attribute. I also tried explicitly adding the entire "Assets.xcassets" folder to the project, as suggested by one of the answers here.
FWIW, the property labeled "Highlighted", directly under "Image", also has the same behavior. I'm assuming this is a VS bug of some sort; the behavior of the property is different not only from what's described in the docs, but conflicts with the appearance of the control.
Has anyone come across a workaround?
Windows 10 Pro
VS 2017 15.8.3
Xamarin 4.11.0.756
Xamarin Designer 4.14.221
Xamarin.iOS and Xamarin.Mac SDK 11.14.0.13
It is bug, you can voite to this problem https://developercommunity.visualstudio.com/content/problem/319294/xamarinios-cant-select-image-asset-for-image-view.html

Xamarin.Forms: Organize images in subfolders

Is it possible to organize my images in subfolders? Something like this example.
Android Project:
·Drawable
·navbarIcons
-user.png
-stats.png
·statsImages
-goals.png
-assists.png
iOS Project:
·Resources
·navbarIcons
-user.png
-stats.png
·statsImages
-goals.png
-assists.png
Or is it mandatory to let them on the Drawable/Resources folder?
Its definitely possible to create subfolders on iOS. Also ensure that your casing is correct because they are case-sensitive and that your Build Actions are set up correctly.
UPDATE:
As you can see its possible to add them in subfolders. Don't forget to add your images in the correct sizes such as 2x and 3x for iOS as I did below.
UPDATE 2:
Another thing you could do is put the images in your Shared PCL project and go the embedded images route. I believe this route doesn't give you as much flexibility when it comes to DPIs though:
https://developer.xamarin.com/guides/xamarin-forms/user-interface/images/#Embedded_Images

Flex ItemRenderer Images not appearing at runtime

in datagrid I have an ItemRenderer (which extends UIComponent) which loads images based on a data value, eg:
_img.source = "assets/images/flags/" + value[dglistData.dataField] + ".gif";
When I run my application the images (flags) are appearing in the ItemRenderer. When I build and deploy the application into BlazeDS as an SWF file - the images do not appear. Instead there is the standard "missing icon" link that appears in Internet explorer.
As far as I can tell, my ANT build script includes the above directory "assets/images/flags/" and all the images into the SWF file.
Is this a relative path issue? Not sure, any help would be appreaciated.
Thanks
Mike
When you are assigning a path as source Flash Player is trying to load the images via HTTP requests. Therefore, embedding your images in the SWF has no effect.
There are two possible solutions. Either deploy the folder containing your images to the server and use the corresponding path as your source or embed all your images into the SWF.
A simple example for the second solution. The code assumes that value[dglistData.dataField] corresponds to one of the variables with the [Embed] metadata.
[Embed(source='assets/images/flags/Image1.png')]
private var image1:Class;
[Embed(source='assets/images/flags/Image2.png')]
private var image2:Class;
[Embed(source='assets/images/flags/Image3.png')]
private var image3:Class;
// access the image like this...
_img.source = this[value[dglistData.dataField]];

Resources