I'm following a tutorial but when I use ImageSource.FromResource to put in the source path for the image I get an error:
ImageSource doesn't contain definition for FromResource.
I guess that you using an old version of xamarin.forms, is declared here and you can find a temporarily solution
Edit :
after talking the problem was naming , you have created a class or property with the same name as ImageSource so using Xamarin.Forms.ImageSource instead solve the problem.
Try this
Image kjdb = new Image();
kjdb.Source = ImageSource.FromResource()
Related
I have a table data and images to assets folder.
I need to bind image by name from assets to TableCell
How can I do it?
I've found old solutions for MVVMCross.Binding 5.7.0 with MvxImageViewLoader, but when I added it to solutions I got references duplicate.
I realized that I can use converter for that, but I want to try avoid converter usage.
Also I can use ffimageloading package, but I didn't find any solution for it.
There is more simple that I've thought.
Only 3 steps:
The first one:
set custom class for UIImageView
The second one:
Create an outlet for UIImageView. Pay attention to type.
And
The third one:
bind URL
have you tried that in your cell?
_imageLoader = new MvxImageViewLoader(() => ImageView)
{
DefaultImagePath = "res:" + NSBundle.MainBundle.PathForResource(imageName, "png")
};
I am working on a Xamarin.Forms application, In which I have added a custom renderer to set the background of native edit text. Following Code shows error.
var shape = new ShapeDrawable(new Android.Graphics.Drawables.Shapes.RectShape());
The issue is that your application uses the namespace Project.Android and the type system will think Android.Grapics.Drawables.Shapes.RectShape belongs in Project.Android.
You some options:
Change your namespace for the project and in all your classes
Prefix Android.Graphics.Drawables.Shapes.RectShape with global:: like: global::Android.Graphics.Drawables.Shapes.RectShape
Add a using at the top of the file telling where RectShape comes from: using RectShape = Android.Graphics.Drawables.Shapes.RectShape and just use new RectShape()
I have a webview and would like to show an image in the webview (html)
My HTML :
hello !img src="myimage.png" alt="myimage" height="42" width="42"!
(I used ! as tagend and tagstart, because I don't know how to add this here without be interpreted as HTML, even I pasted as code)
The myimage.png should be stored in app itself and not be loaded from a websource.
I don't know how to do that in a best practice way. Any help ?
UPDATE
I tried with referenced Article, but still not succeeded:
My Code for this:
let path:NSString = NSBundle.mainBundle().bundlePath;
var baseURL:NSURL=NSURL(fileURLWithPath: path as String)!;
var htmlString:String! = texttemp
myWebView.loadHTMLString(htmlString, baseURL: baseURL)
The same Image I can already load like the following -> works:
var image = UIImage(named: "myimage.png");
Your updated code isn't right. You are creating a path to the bundle, not to the specific file. You need to use the NSBundle method pathForResource:ofType (or one of its variants) to build a path to your file. Then use that to create the URL.
The pathForResource:ofType family of methods return nil if the file can't be found, so you should check that you are getting back a path.
EDIT:
Looking at it more closely, I see that you are using the URL as the base URL for a call to loadHTMLString. This does look like a sound approach. What is your HTML string, and where is the image in your bundle?
I've tried adding a image via the following however it is still not working. The image type is a content.
Image image = new Image();
image.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("myimage.png", UriKind.Relative));
//Define the image display properties
image.Opacity = 1.0;
image.Stretch = Stretch.Fill;
image.Width = 40;
image.Height = 40;
// Center the image around the location specified
//Add the image to the defined map layer
phoneDetailsLayer.AddChild(image, e.Position.Location);
mapViewAll.Children.Remove(phoneDetailsLayer);
mapViewAll.Children.Add(phoneDetailsLayer);
Make sure that your image is the correct resource type and is loaded optimally (ie once if being used multiple times). There are multiple approaches to loading images for WPF (same as WP7) which are described here: WPF image resources
This post here: Visual Studio: How to store an image resource as an Embedded Resource? discusses the different image resource types you should/shouldn't use.
I think you should have a look at both as its a good thing to understand, as it can help you to avoid issues in the future that could pop up.
I can't add a comment to your question, however I'll ask here when you say content, have you added the image directly to the project containing your code or to a separate content project?
Assuming that you have added it directly:
If you had set the "Build Action" to "Resource" then you should use the GetResourceStream method:
Image image = new Image();
StreamResourceInfo resource = Application.GetResourceStream(new Uri("/myimage.png", UriKind.Relative));
var bmp = new System.Windows.Media.Imaging.BitmapImage();
bmp.SetSource(resource.Stream);
image.Source = bmp;
However if you have set the "Build Action" to "Content" you should use the GetContentStream method
Image image = new Image();
StreamResourceInfo resource = Application.GetContentStream(new Uri("/myimage.png", UriKind.Relative));
var bmp = new System.Windows.Media.Imaging.BitmapImage();
bmp.SetSource(resource.Stream);
image.Source = bmp;
Just to clarify the answer to this questions. The problem was not in the resource type, the problem was related to the way relative Uri's work. Just like any well structured project ericlee used different folders within his project like this (relative to the project root):
/pages - Contains the actual pages and therefore also the page containing the above code
/images - Contains the actual PNG images that have to be referenced
In the original code a reference is made to "myimage.png" as a relative uri. The app will now look at "/pages/myimage.png" and therefore won't find the image. The trick here is to use the correct relative URI. It can be constructed as follows:
1. First go up to the project root by using two points -> .. (one for the current dir, one extra to go up one level)
2. Now reference /images -> ../images
3. Now add the actual file reference -> ../images/myimage.png
If you use the correct URI the problem is solved.
The main question seems to be how to get true uri.
For me, the following table helps me in this case (I only have it in German):
http://msdn.microsoft.com/de-de/library/aa970069.aspx
example:
// Absolute URI (default)
Uri absoluteUri = new Uri("pack://application:,,,/File.xaml", UriKind.Absolute);
// Relative URI
Uri relativeUri = new Uri("/File.xaml", UriKind.Relative);
example 2:
Uri uri = new Uri("pack://application:,,,/File.xaml");
or Codebehind:
'Image compiling is set to "content"
MyImage1.Source = New BitmapImage(New Uri("/Images/MyFile.png", Relative))'only example
/projectname;component/images/menu/lost.png
Is the correct way, the rest of your answer is really not working
I'm developing a Windows Phone app.
I'm using an image and when I select a picture using Properties panel I get the following XAML:
<Image x:Name="GameImage" Margin="8" Source="/MyApp;component/Assets/Icons/GameImage.png"/>
Why am I getting "/MyApp;component/..."? (Is there any better way?)
If I try to do Image.Source="Assets/Icons/GameImage.png" why does it not work?
This is because your image has it's build action set to Resource (Which is the default). If you switch it to Content you can set the source in your XAML like this:
<Image x:Name="GameImage" Margin="8" Source="/Assets/Icons/GameImage.png"/>
To set it in code you can do this:
BitmapImage tn = new BitmapImage();
tn.SetSource(Application.GetResourceStream(new Uri(#"Assets/Icons/GameImage.png", UriKind.Relative)).Stream);
Image.Source = tn;
You should be using Content for performance reasons. See this article for more detail: http://www.windowsphonegeek.com/tips/wp7-working-with-images-content-vs-resource-build-action
Don't forget to add:
using System.Windows.Media.Imaging;
BitmapImage tn = new BitmapImage();
tn.SetSource(Application.GetResourceStream(new Uri(#"Assets/Icons/GameImage.png", UriKind.Relative)).Stream);
Image.Source = tn;
You can use :
BitmapImage obj = new BitmapImage();
obj.UriSource = new Uri(mera_image.BaseUri,file.Path);
Any content marked as an embedded resource is loaded from an assembly. Thus, usage sites need to know specify the assembly a resource is embedded in. In your case, this is MyApp.