Xamarin: How do I set a placeholder image in my Image element in XAML? - xaml

In Android, it is quite easy to set a image placeholder for development purposes in an imageview.
All one would do is use the tools:src for the placeholder image during development so you could see it in your layout design preview and your actual image you would put into your android:src
. Frequently, you don't set the android:src put would populate that imageview with images that you download off the internet once you connect your app to the net:
<ImageView
android:layout_width="28dp"
android:layout_height="28dp"
android:src=#drawable/activated_icon"
tools:src="#mipmap/white_activated_icon" />
In Xamarin, you have this to display images:
<Image Source="http://lorempixel.com/1920/1080/nature/3" />
Once the app connects, the image is downloaded from the internet and displayed. How do I put in a placeholder image for development purposes (similar to the way you would do it through tools:src in android) so I can see it within the layout design previews?

I'm assuming you want to achieve this with Xamarin Forms, as you referring to XAML in the question title.
Or possibly you are referring to the Xamarin Forms Previewer? In that case, disregard the following answer. You can only use local images in the Previewer as far as I can tell.
Answer in case the question is related to runtime Xamarin Forms
There is no standard option to do this in Xamarin Forms. Take a look at this example suggested by Stephane Delcroix from the Xamarin Forums. The important part of this approach is to stack 2 images on top of each other in a grid. 1 is the placeholder that is placed beneath the image that is being loaded, like this:
var grid = new Grid();
grid.RowDefinitions.Add (new RowDefinition());
grid.Children.Add (image);
grid.Children.Add (placeholderImage);
As an alternative option, WebImage of Xamarin Forms Labs allows an option for a DefaultImage, but I'm not sure if that shows immediately. See this class for the code of the Web Image. Keep in mind that Xamarin Forms Labs is no longer under active maintenance.
Of course you can also write a custom renderer. Maybe based on the Xamarin Forms Labs example.

I use FFImageLoading for this Case.
Just install the Nuget Package and Use the "CachedImage" in XAML.
<ff:CachedImage Source="your url" Placeholder="placeholder while loading"/>
You donĀ“t need to download (and cache) the Image manually then. It will automatically displayed when the download is done.
You can use local files (from Resources or Drawable) and files from HTTP/HTTPS.

Related

I'm trying to implement a Xamarin local image on my App, but it's not working

I'm trying to add a local image in my Xamarin app, but it's not working. Do you know what I could've have missed?
I have added the XAML and c# code but that still does not work.
XAML - <Image x:Name="Background_Image" Source="backgroundimage.png"/>
C# - var image = new Image { Source = "backgroundimage.png" };
I have followed the proper practices to a tee from the Xamarin Images documentation, but it's still not working. Link to Xamarin Documentation on Images - (https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/images?tabs=vswin#Local_Images)
I have made sure the file name is
All in lowercase,
The build action is AndroidResource of the image
The image has been placed in the Resources/drawable folder.
Which are all the conventions you must follow, but it doesn't seem to work.
The image's file size is 1.28mb and resolution is 1920x1080 if that matters. I have tested with a much smaller 4KB image but does not work either.
I am using the Xamarin Live Player to show the app, the phone uses API 19, Android 4.4
I apologise if this is a duplicate, but I have read many topics here but I have not been able to fix it.
It does seem to be a issue or limitation with Xamarin's own Live player. I have launched the App with the Android Emulator and the Image is displaying, I'll report it as a bug.

Browsing images like in gallery (by swiping them)

I need to show images downloaded from the Internet in a convenient way (number of jpgs is not fixed, it is usually between 10 and 30). I thought I could use swiping like in gallery app.
On Android I could just use ViewPager. What is Windows equivalent for this?
In earlier version I used controls:MediaViewer but it doesn't seem to be supported in Windows 10.
What is the right way to achieve what I want?
Use flip view control for displaying images in a collection. You create a DataTemplate to specify the look of the items. Further details and an example can be found at this link: https://msdn.microsoft.com/en-us/windows/uwp/controls-and-patterns/flipview

Changing the chrome of a Windows 10 Universal XAML app

I would like to modify the design of the chrome and top menu panel of my app where the close and minify icons are.
How do I do this and in what XAML file? Should it be done in App.xaml, or in my main page's xaml file? I can find instructions on how to do this in a WPF app, but the concepts don't seem to carry over to Univeral App XAML.
As far as I know, you can change some settings from the chrome bar in the uwp, but not fully customize it by default.
The starting point for doing so would be the ApplicationView's TitleBar property.
This allows you to change the colors of the buttons and the bar itself:
var titleBar = ApplicationView.GetForCurrentView().TitleBar;
if (titleBar != null)
{
titleBar.ButtonBackgroundColor = Colors.DarkBlue;
titleBar.ButtonForegroundColor = Colors.White;
titleBar.BackgroundColor = Colors.Blue;
titleBar.ForegroundColor = Colors.White;
}
It is worth mentioning that when running the application on a PC, the control is called TitleBar but on a mobile device, it is called StatusBar. Full example for handling both devices can be found here.
If you need more personalization options, creating your own TitleBar component should be possible, look for the ExtendViewIntoTitleBar property of the CoreApplicationViewTitleBar object.
More info can be found here.
You need to use a resource dictionary with the custom styles. You can import these themes via App.Xaml to apply it.
These links might be of help. If you have any specific questions feel free to ask.
ResourceDictionary and XAML resource references
XAML theme resources

Setting ApplicationBarButton.IconUri with icon from Isolated Storage

I am working on windows phone application in which i need to change ApplicationBarButton icon dinamically, for example to show current date like in default Windows Phone Calendar app. Regarding to this article, we can use only png images previously added to solution and reference to them with relative Uri.
It is not a problem for me to generate correct png image (this way and using ToolStack C# PNG Writer Library) and save it to isolated storage, but when i'm initializing AppBarButton with absolute Uri to this image and trying to add it to ApplicationBar, my app throws ArgumentException.
Am i doing something wrong or i just have to draw 366 icons and add them to my project? :)
Thanks in advance!

How to display a Windows 8 metro style tile within an app?

Windows 8 metro style app tiles are created based on pre-defined xml templates (found here).
Is there any way to hook up to Windows' rendering of the tile to allow a tile preview within an app?
In my app I would like to offer the user a subset of the tile templates listed in the above link and let the user customize the tile content. A live preview of the customized tile rendered within my app would greatly improve the user experience.
you should look into HubTile.
I don't know any free control's right now, but Telerik and Syncfusion have HubTile control
You can't get access to real Windows tile rendering, but you can simulate it since all templates are known. It is very unlikely that Microsoft would change the way tiles are rendered and even if they do you should be able to issue app update to keep up with Microsoft.