I am using <Image Source="flower.png" /> code in Xamarin form in Visual Studio 2017 to show image in Tizen Mobile but it does not show up. I put image file in shared\res folder in Tizen project.
Copy the Image into '.TizenMobile' Project 'res' directory (not in 'shared>res' only 'res' (root)). Then In Solution Explorer:Right click over 'res' folder and Add > Existing Item > png Image
In your ProjectCode (Portable) C# file:
MainPage = new ContentPage
{
Content = new StackLayout
{
VerticalOptions = LayoutOptions.Center,
Children = {
new Image {
Aspect = Aspect.AspectFit,
Source = ImageSource.FromFile("flower.png")
}
}
}
};
Or you can even try the XAML.
Related
I want to save images to gallery which is in the drawable folder. I built an app to take pictures and save it in the gallery in xamarin forms by using the following article.
https://xamarinhelp.com/use-camera-take-photo-xamarin-forms/
private async void SaveButton_Clicked(object sender, EventArgs e){
var photo = await Plugin.Media.CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions()
{
PhotoSize = PhotoSize.Medium,
CompressionQuality = 92,
SaveToAlbum = true, //saves to image gallery
Directory = "MyFolder2",
Name = $"{DateTime.Now}_documenttypeXYZ.jpg"
});
}
the above article is using media plugin https://github.com/jamesmontemagno/MediaPlugin/blob/master/README.md#saving-photovideo-to-camera-rollgallery to take pictures and save it.
But i don't know how to save the images to gallery which are in the drawable folder.
I'm working on a Xamarin.Forms project utilizing PCL (not the shared project).
I have a few images in my Resources folders in both Android and iOS project.
This works and the icons show in buttons as they're supposed to:
<Button Image="speaker.png" Grid.Row="0" Grid.Column="0" />
I also have a folder in my PCL project with some images: images/icons/speaker.png
I've tried this:
<Button Image="{local:EmbeddedImage TestThree.images.icons.speaker.png}" />
...but that didn't work...
I would like those buttons to show images from my images folder in my PCL project.
So my question would be...
<Button WHAT GOES HERE? Grid.Row="0" Grid.Column="0" />
When Button.Image wants FileImageStream, I give it to him. But as images in the project I still use embedded resources PNG files in PCL (or .NET standard 2.0) library (project).
For example the PCL project name is "MyProject" and I have an image placed in its subfolder "Images\Icons\ok.png". Then the resource name (e.g. for ImageSource.FromResource) is "MyProject.Images.Icons.ok.png".
This method copies embedded resource file into the file in application local storage (only first time).
public static async Task<string> CopyIcon(string fileName)
{
if (String.IsNullOrEmpty(fileName)) return "";
try
{
// Create (or open if already exists) subfolder "icons" in application local storage
var fld = await FileSystem.Current.LocalStorage.CreateFolderAsync("icons", CreationCollisionOption.OpenIfExists);
if (fld == null) return ""; // Failed to create folder
// Check if the file has not been copied earlier
if (await fld.CheckExistsAsync(fileName) == ExistenceCheckResult.FileExists)
return (await fld.GetFileAsync(fileName))?.Path; // Image copy already exists
// Source assembly and embedded resource path
string imageSrcPath = $"MyProject.Images.Icons.{fileName}"; // Full resource name
var assembly = typeof(FileUtils).GetTypeInfo().Assembly;
// Copy image from resources to the file in application local storage
var file = await fld.CreateFileAsync(fileName, CreationCollisionOption.OpenIfExists);
using (var target = await file.OpenAsync(PCLStorage.FileAccess.ReadAndWrite))
using (Stream source = assembly.GetManifestResourceStream(imageSrcPath))
await source.CopyToAsync(target); // Copy file stream
return file.Path; // Result is the path to the new file
}
catch
{
return ""; // No image displayed when error
}
}
When I have a regular file, I can use it for the FileImageStream (Button.Image).
The first option is use it from the code.
public partial class MainPage : ContentPage
{
protected async override void OnAppearing()
{
base.OnAppearing();
btnOk.Image = await FileUtils.CopyIcon("ok.png");
}
}
Also I can use it in the XAML, but I must create an implementation of IMarkupExtension interface.
[ContentProperty("Source")]
public class ImageFileEx : IMarkupExtension
{
public string Source { get; set; }
public object ProvideValue(IServiceProvider serviceProvider)
{
return Task.Run(async () => await FileUtils.CopyIcon(Source)).Result;
}
}
Now I can assign the image direct in the XAML.
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MyProject"
x:Class="MyProject.MainPage"
xmlns:lcl="clr-namespace:MyProject;assembly=MyProject">
<Grid VerticalOptions="Fill" HorizontalOptions="Fill">
<Button Image="{lcl:ImageFileEx Source='ok.png'}" Text="OK" />
</Grid>
</ContentPage>
For this solution the NuGet PCLStorage is needed.
The reason that does not work is because the properties are bound to different types.
Button's Image property takes a "FileImageSource" - Github
Image's Source property takes a "ImageSource" - Github
From the local:EmbeddedImage im guessing you were using the extension from Xamarin forms image docs
That would not work because it loads a "StreamImageSource" instead of "FileImageSource".
In practice you should not do this as it would not load from different dimension images(#2x, xhdpi etc) and would give bad quality images and not support multiple resolutions.
You could use a view container(Stack layout etc) with a TapGestureRecognizer and an image centered inside it or create a custom renderer which really is more effort than its worth. None of these still would still obviously not handle multiple images though.
The correct solution would be to generate the correct size images from the base(Which I would assume would be MDPI Android or 1X for iOS) and put them in the correct folders and reference them in your working Button example.
(using PCL project)
I am using version 2.1 of the SignaturePad and works perfectly on Android, but on UWP, when I call the GetImageStreamAsync method, I get a 'Microsoft.Graphics.Canvas.CanvasDevice is not registered' error. I have tried both PNG and JPEG and both give the same error.
I was thinking something needed to be included in App.xaml.cs but looking at the samples, I don't see anything. I did find a post where they added the assembly
List<Assembly> assembliesToInclude = new List<Assembly>();
assembliesToInclude.Add(typeof(SignaturePad.Forms.SignaturePadCanvasRenderer).GetTypeInfo().Assembly);
but I still get the same error.
Here is the code that I am using:
SignaturePad.Forms.SignaturePadView SignPad = new SignaturePad.Forms.SignaturePadView()
{
StrokeWidth = 3f,
StrokeColor = Color.Black,
BackgroundColor = Color.White,
HeightRequest = 200,
WidthRequest = 400
};
// This gives error in UWP, but not Android:
var image = await SignPad.GetImageStreamAsync(SignaturePad.Forms.SignatureImageFormat.Png);
I'm very very new to using Xamarin forms. So new in fact I have just watched the "microsoft xamarin forms for absolute beginners".
I'm trying to include a webpage into the Xamarin form but the only info I can find is
var browser = new WebView {
Source = "http://xamarin.com"
};
This might sound silly but has anyone got a full tutorial on how to include one?
thanks
just create a WebView and assign it to a ContentPage. Then you can either make this page the main page of your app (in the App class) or you can navigate to it from another page.
ContentPage page = new ContentPage {
Content = new WebView { Source = "http://xamarin.com" }
};
To display a website from the internet, set the WebView's Source property to a string URL:
var browser = new WebView
{
Source = "http://xamarin.com"
};
see this:
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/webview?tabs=windows#performance
I'm using Visual Studio 2015 and made a Xamarin project to support iOS, Android and UWP.
I want rebrand the toolbar, and on iOS and Android its possible to set a background color and a picture in the toolbar.
But for Universal Windows Platform this seems impossible.
So I want to set my own TopAppBar with a picture, and hide the current toolbar for UWP;
In my MainPage.xaml.cs I've;
#if __ANDROID__ || __IOS__
ToolbarItems.Add(new ToolbarItem("+", "", () => App.Navigation.PushAsync(new AddAccount())));
#endif
So for UWP there would be no items on the toolbar. But it still appears.
I cannot find any documentation on how to;
-customize the toolbar for UWP
-hide the toolbar for UWP
I've tried to add a toolbar like so;
var _globalAppBar = new AppBar();
_globalAppBar.Height = 128;
_globalAppBar.Background = new SolidColorBrush(Colors.Green);
BitmapImage bmI = new BitmapImage();
bmI = new BitmapImage(new Uri("ms-appx:///Assets/logo.png", UriKind.RelativeOrAbsolute));
var imageBrush = new ImageBrush();
imageBrush.ImageSource = bmI;
_globalAppBar.Background = imageBrush;
AppBarButton abbtn = new AppBarButton();
abbtn.Label = "Add";
_globalAppBar.Content = abbtn;
this.BottomAppBar = _globalAppBar;
But that results in having two toolbars at the top...
So it's better to modify the existing toolbar created by Xamarin, but I don't know how to access it from the 'public MainPage()' of the UWP project.
I just tried to redo your problem. I can hide the toolbar when I clear the toolbaritems.
Also I have to call
NavigationPage.SetHasNavigationBar(this, false);
on the page.