Save image from webservice call in in local PCL folder Xamarin Forms - xamarin

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);
}
}

Related

Flutter Workflow for saving & retrieving images

I am trying to build an app where the user can add images from Image Picker, then save it in a Provider as well as in Firebase. For retrieving it should be the other way around: Get it from Firebase and then store it in my Provider.
(ignore the Firebase part, for now I only want to store the images in a Provider, but it should be correctly set up, so I can add the Firebase support later)
The problem is that I am quite new to Flutter and I am not how to start here, because there are so many ways to load images in Flutter. From the ImagePicker I retrieve a File. What is the correct way to store this in a Provider so I can load it in another screen?
I know this is quite a genreal question but I hope some one can shed some light in the dark for me here :D Let me know if you need any more info.
I already setup a ChangeNotifier:
class MemoryImageProvider extends ChangeNotifier {
String path = '';
setImage(String path) {
path = path;
notifyListeners();
}
}

How to access file stored in shared project in Xamarin Forms?

I have a folder called Documentation inside the shared project, named App2 in this case. How do I access the files stored inside the Documentation folder? Attached image below shows the project structure.
Visual Studio Solution Page
I have tried out following commands but they aren't working :
System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
AppDomain.CurrentDomain.BaseDirectory
System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
If it's troublesome to access the file in that folder, I'm open to hearing other alternatives.
This is how I have done it for JSON files in my shared project (using PCL). As Jason pointed out in the comments, if you are using .NET Standard, you can simply define the GetSharedFile method in your shared project instead of creating platform specific references.
Add the file to the shared project and set it as Embedded Resource
Create an IFileHelper interface in your shared project
public interface IFileHelper {
Stream GetSharedFile(string fileResourceName);
}
Create a new FileHelper class in each project (Android and iOS) with the following
public class FileHelper : IFileHelper {
public Stream GetSharedFile(string fileResourceName) {
Type type = typeof(IFileHelper); // We could use any type here, just needs to be something in your shared project so we get the correct Assembly below
return type.Assembly.GetManifestResourceStream(fileResourceName);
}
}
Add a documentation handler class in your shared project. Something like below (make sure to change the App namespace to match yours):
public class Documentation {
private const string ResourcePath = "App.Documentation.index.html"; // App would be your application's namespace, you may need to play with the Documentation path part to get it working
public string GetDocs() {
IFileHelper helper = DependencyService.Get<IFileHelper>(); // Your platform specific helper will be filled in here
Stream stream = helper.GetSharedFile(ResourcePath);
using (stream)
using (StreamReader reader = new StreamReader(stream)) {
return reader.ReadToEnd(); // This should be the file contents, you could serialize/process it further
}
}
}
I wrote this mostly by hand so let me know if something is not working. If you cannot load your file, I suggest trying to put it into the root of your shared project and then changing ResourcePath in the code above to the following (again using your app's namespace instead of App):
private const string ResourcePath = "App.index.html";

Xamarin Forms - Image doesn't show up on Lumia

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?

Xamarin.forms how to auto save user name like browser

I am developing a mobile application using Xamarin.Forms
I had the following Home page contains login info:
How can we have the application to automatically save the user name, so that they do not have to type it in each time (as in a browser)?
You can use Properties dictionary in Xamarin.Forms Application class. And let the Xamarin.Forms framework handle persisting user name between app restarts and pausing/resuming your app.
Save user name by writing it to Properties dictionary
var properties = Xamarin.Forms.App.Current.Properties;
if(!properties.ContainsKey("username")
{
properties.Add("username", username);
}
else
{
properties["username"] = username;
}
Then, when your login screen is about to appear (for example in OnAppearing method) check Properties for user name:
var properties = Xamarin.Forms.App.Current.Properties;
if(properties.ContainsKey("username")
{
var savedUsername = (string)properties["username"];
}
If it's not there, then it means that this is first time when user log in into your application.
A very similar question was posed just a few days ago - my answer on that question also applies to your question: The best way to save Configuration data in Xamarin.Forms based app?
Essentially, you want to store the information using the native settings functionality. I would advise against using Application.Properties for now. It is currently not reliable on Android, and in the past has had other problems. The nuget package referenced in my linked answer is a better approach and will save you some headache in the future.
The right way to be done is through the App settings plugin
https://github.com/jamesmontemagno/Xamarin.Plugins/tree/master/Settings
What i did in my application is.
1) Installed Plugin.Settings from nuget
2)Added to Helpers->Settings.cs (autogenerated file by plugin) the following
public static class Settings
{
private static ISettings AppSettings
{
get { return CrossSettings.Current; }
}
private const string UserNameKey = "username_key";
private static readonly string UserNameDefault = "demo";
public static string UserName
{
get { return AppSettings.GetValueOrDefault<string>(UserNameKey, UserNameDefault); }
set { AppSettings.AddOrUpdateValue<string>(UserNameKey, value); }
}
}
3)In order to keep the username in the Application Context set
Settings.UserName = ViewModel.Username;
4)When you login screen starts
string username = Settings.UserName;
The answer is simple: persistance. Servers do this by setting cookies containing the data (or reference to it) that they want you to see when rendering the form field.
In order to do this in an app (with Xamarin for instance), you need to store the user's data into a file or database somewhere. Since you're using Xamarin you can probably use some sort of ConfigurationManager to keep track of this.
Obviously you could just create a config file in the local storage you have for your app (I don't think you need permissions to create files in that space).
When you have the info stored somewhere, just retrieve it and set the input's value to it.

Handling dynamic created files in play 2

I wrote a small app that creates downloadable pdf files with play 2.0
I want to serve them to the public. On my development environment I created a folder in the
/assets/ folder and everything worked nice.
Now, when switching to production, I figured out that play always deployed those files behind my back.
Do I really have to write a own controller to serve those files or what is the way here ?
I've also had problems trying to serve files created dynamically with the assets controller. I don't know if it's for some kind of caching but I ended up writing my own controller, and now it woks perfectly.
I mean, I use the Assets controller for regular public files and for my dynamic generated files I use this one:
public class FileService extends Controller {
static String path = "/public/dynamicfiles/";
public static Result getFile(String file){
File myfile = new File (System.getenv("PWD")+path+file);
return ok(myfile);
}
}
And the routes would be like this:
GET /files/:file controllers.FileService.getFile(file: String)
GET /assets/*file controllers.Assets.at(path="/public", file)
It works great for me

Resources