I implemented a Xamarin App with UWP Platform as target.
I added 4 images in the Assets of PCL and set Build Action = Embedded Resource on each. No image has been added on UWP project.
If I run the app on my laptop everything works great, instead when I run the same app on my Lumia no image shows up. I don't understand why, since the app is the same with the only difference of Target Platform, x64/x86 for my laptop and ARM for my Lumia.
I have also tried to set Copy to output Directory = Copy Always for each image, but without success.
Have you tried in another plaftorm (Droid, iOs?)
Like mentionned in the documentation: https://developer.xamarin.com/guides/xamarin-forms/user-interface/images/, you need to:
1) Use a custom MarkupExtension to load image from EmbeddedResource:
[ContentProperty ("Source")]
public class ImageResourceExtension : IMarkupExtension
{
public string Source { get; set; }
public object ProvideValue (IServiceProvider serviceProvider)
{
if (Source == null)
{
return null;
}
// Do your translation lookup here, using whatever method you require
var imageSource = ImageSource.FromResource(Source);
return imageSource;
}
}
2) Use this Custom Markup Extension and provide the fully qualified name of your image (namespace + name)
<Image Source="{local:ImageResource MyProject.Assets.Images.beach.jpg}" />
Just found the solution, disable "Compile with .NET Native tool chain".
https://stackoverflow.com/a/37352400/2297037
Do you know something about possible side effects?
Related
I have looked for many examples of this and I believe they are trying to do something different than what want to do.
I have a set of images stored a folder in my pcl. See Image attached.
PCL Image
Currently I have those images set to embedded resource and they all display in my List View when their name is called. However if an update is made on the server like an image is changed or replaced or added I would like the new image to get saved in the AllImages folder as shown in the image. Is this possible? I've seen a lot of people doing separate dependencies to store the images in the platforms resources/ drawable folder but I don't want I don't want to do that. If i have to change where i am storing my images I can do that as well, but I do need to be able to store the images from the server on my app locally.
I currently have a class dedicated to embedded images, so it would be nice to do it this, but I am not married to the idea.
[ContentProperty("ResourceID")]
public class EmbeddedImage : IMarkupExtension
{
public string ResourceID { get; set; }
public object ProvideValue(IServiceProvider serviceProvider)
{
if (String.IsNullOrWhiteSpace(ResourceID))
return null;
return ImageSource.FromResource(ResourceID);
}
}
I just want to log to console and to a log file, using a standard TraceSource, in my Xamarin app that will run on UWP, Mac OS X, iOS and Android. I'm developing/debugging on UWP.
TraceSource, TraceListener, and TextWriterTraceListener are indeed all available in .Net Standard library, so perhaps I'm setting it up incorrectly? Most places on the Internet insist on setting up trace listeners in an app.config file, but this is not applicable nor possible for Xamarin apps. So here is my logging initialization code, mostly based on an example in Microsoft docs:
private void SetupLogging()
{
Trace.Listeners.Add(new TextWriterTraceListener(Console.Out, "consoleTraceListener"));
string logFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Application.log");
if (!File.Exists(logFilePath)) File.Create(logFilePath);
var logFileTraceListener = new TextWriterTraceListener(logFilePath, "logFileTraceListener");
Trace.Listeners.Add(logFileTraceListener);
Trace.Write("Test");
Trace.TraceInformation("Logging Initialized. Log file location: " + logFilePath);
Trace.Flush();
}
When I run this in a Xamarin UWP app, a file is created but nothing is written to it, nor can I find anything in the Output of the program (there is no ConsoleTraceListener so I'm trying to write a TextWriterTraceListener to Console.Out). Can someone provide a working example for Xamarin? (I haven't tried the Android or iOS apps yet; want to get UWP on the local machine working first.)
The problem is that you passed wrong string parameter to TextWriterTraceListener method. Please try to pass Stream parameter. You could use following code directly. by the way, you'd better use LocalApplicationData SpecialFolder that could be accessed successfully within uwp.
private void SetupLogging()
{
Trace.Listeners.Add(new TextWriterTraceListener(Console.Out, "consoleTraceListener"));
string logFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Application.log");
if (!File.Exists(logFilePath))
{
File.Create(logFilePath);
}
var logFileTraceListener = new TextWriterTraceListener(File.Open(logFilePath,FileMode.Open), "logFileTraceListener");
Trace.Listeners.Add(logFileTraceListener);
Trace.Write("Test");
Trace.TraceInformation("Logging Initialized. Log file location: " + logFilePath);
Trace.Flush();
}
I am developing an Xamarin.forms application, I need to catch the imei number of mobile that is using the application. I am aware of how it is done in Android. But how can I do the same in Xamarin.forms. Please educate me.
There is no 'Forms-way' of doing this. If you know how on Android you can make use of the DependencyService. Which means in your shared project create an interface like:
public interface IImeiService
{
string GetImei();
}
Now in your Android project implement this interface, so it would be something like:
public class ImeiService : IImeiService
{
public string GetImei()
{
// ... Implement your Android code here
}
}
Register your Android code with an attribute on the class above the namespace
[assembly: Xamarin.Forms.Dependency (typeof (ImeiService))]
namespace ImeiApp.Droid {
You can now access it, back in your shared code, with:
var imei = DependencyService.Get<IImeiService>().GetImei();
If you would have an iOS implementation you could repeat the same steps, although you can, of course, use the same interface and call in shared code, so you will only need a iOS specific implementation.
However, since iOS 7 Apple disallows access to this kind of information programmatically, so you cannot get the IMEI number. And if you can, you will use code that will not be allowed through the App Store review process.
I'm using WebView to display HTML from a string. This HTML references a stylesheet that I have locally on the device. I'm following the instructions from the official docs so the local CSS file can be accessed. It works fine on Android and iOS but I can't make it work on Windows Phone 8.1. I placed the CSS file in the root of the project and set the build action to Content, then set the BaseUrl to "" (I tried ms-appx-web:/// as well, with no success).
Does anyone know if the docs are missing some instructions? Or is it a bug in Forms? Any help would be appreciated.
Here's the code for populating the WebView with data (in a Forms PCL):
private async Task LoadData()
{
string html = await _dataProvider.GetHtmlAsync("index");
WebView.Source = new HtmlWebViewSource
{
Html = html,
BaseUrl = _baseUrlProvider.Get()
};
}
_baseUrlProvider is an instance of my WebViewBaseUrlProvider that is defined as follows:
internal class WebViewBaseUrlProvider : IWebViewBaseUrlProvider
{
public string Get() => "";
}
EDIT:
Seeing this question leads me to believe that there indeed is a bug in Xamarin Forms. When the link in the HTML contains the whole ms-appx-web:///style.css instead of just style.css, it suddenly starts to work. Obviously, this is not quite cross-platform...
I just found the answer - Xamarin simply ignores the BaseUrl property: https://github.com/xamarin/Xamarin.Forms/blob/2d9288eee6e6f197364a64308183725e7bd561f9/Xamarin.Forms.Platform.WinRT/WebViewRenderer.cs#L26
I'm trying to use https://developer.xamarin.com/api/member/Xamarin.Forms.Application.OnAppLinkRequestReceived/p/System.Uri/ but can't seem to get it to be called in any way.
Did anyone ever tried to implement this new method?
I've tried several ways to implement different deeplinks, all of them open the app fine so they're correctly configured but that method never gets called.
Thanks.
This is how I set it up in Android.
Put this above your MainActivity.cs
[IntentFilter(new[] { Android.Content.Intent.ActionView },
Categories = new[]
{
Android.Content.Intent.CategoryDefault,
Android.Content.Intent.CategoryBrowsable
},
DataScheme = "http",
DataPathPrefix = "/tesla/",
DataHost = "exrin.net")]
This registers the activity when the app is installed. Change the URL to your desired URL.
For Android only (no need to do this with iOS) you need to also install the Nuget Xamarin Forms AppLinks
In your OnCreate make sure you do this after your Xamarin Forms Init
AndroidAppLinks.Init(this);
Then when you load the URL in a browser (in my example http://exrin.net/tesla) you will get this:
Then if you open the app it will enter here with the full URL as the URI parameter. This is in the App.cs (Xamarin.Forms.Application)
protected override void OnAppLinkRequestReceived(Uri uri)
{
base.OnAppLinkRequestReceived(uri);
}
You can then decode the URI as you see fit to move to the specific page in your app that the URL relates to/
More details at Xamarin Forms AppLinks
Also, your MainActivity must be derived from FormsAppCompatActivity for it to work. Once I changed it to that from FormsApplicationActivity, it started working.