I have an app that is file management app like drive. I want to add sync mechanism with Xamarin Forms app.
So the sync mechanism I want is this: User will choose a folder from my app, and will choose a folder from their phone. App will sync both of them, when added a file or folder to this folders.
For example:
When a file is added to the selected folder (on the phone) from the app, this file will be uploaded to the remote server with the help of my app. Or
When a file is added to the folder (in the app) selected from the app, this file will be downloaded from the remote server to the selected folder on the phone with the help of my app.
My app will work in the background or sync will be triggered with button in my app. (Because I know ios background restricts operations. But it will be another research subject for me.)
For this reason I am looking up a folder picker. But I didn't find any plugin. When I searched, I saw that it was said that it can be done with dependency services.
So I want to get all file system structure of phone and show it with a listview for select a sync directory. I follow this documentation for get all file system structure of phone:
for ios: https://learn.microsoft.com/en-US/xamarin/ios/app-fundamentals/file-system
for android: https://learn.microsoft.com/en-US/xamarin/android/platform/files/
But I stuck in ios. Because I couldn't get all directories with this commands. For example I want to select a gallery album for sync. But I couldn't access gallery album path. I am accessing with image pickers but I don't want select a file, I want to select gallery album.
I reviewed Xamarin.Essentials Media Picker and File Picker source code in github. I also reviewed Plugin.FilePicker source code in github. All of them use UIDocumentPickerViewController or UIImagePickerController. But these for selecting file. These not selecting folder.
What can I do?
Thanks in advance.
For now, no FodlerPicker in Xamarin.Forms.
You could create your own custom folder picker like below. Use the TapGestureRecognizer to simulate the selecting operation to save to the specific location.
<ListView ItemsSource="{Binding folders}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Label Text="{Binding folder}">
<Label.GestureRecognizers>
<TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"></TapGestureRecognizer>
</Label.GestureRecognizers>
</Label>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
You could post your requirement in GitHub. https://github.com/xamarin/Xamarin.Forms/issues
Update:
You could use the code below to enumerate the subdirectories in the current directory (specified by the "./" parameter), which is the location of your application executable.
var directories = Directory.EnumerateDirectories("./");
foreach (var directory in directories) {
Console.WriteLine(directory);
}
You could check the sample code in File system access in Xamarin.iOS: https://learn.microsoft.com/en-us/xamarin/ios/app-fundamentals/file-system
For more details you could refer to the link below.
https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/AccessingFilesandDirectories/AccessingFilesandDirectories.html#//apple_ref/doc/uid/TP40010672-CH3-SW1
Related
I have a Xamarin.Forms shared project. On Android all images work fine. I wanted to add them to my iOS project, but they don't show. I've added them to Assets.xcassets:
{
"filename": "logo_launcher.png",
"scale": "1x",
"idiom": "universal"
}
And I use the following XAML:
<Image Source="logo_launcher.png" x:Name="logo" />
I've added all images except for the vector ones. I'm testing it on a iPhone 8 - 11.4 simulator.
Although it is deprecated, I tried to add the images to the "Resource" folder. But I couldn't even select the .png files. So that didn't work either.
I'm lost at what to do. Is there some option I need to enable, or something to include in the XAML to make it work?
Just select all images in resources folder in ios and right click-->build action-->bundle resource. It will work.
Check/Reproduce some steps below:
Check the build action on your image resource, in shared PCL it should be Embedded Resource, in Android the build action should be AndroidResourceand BundleResource in iOS
Try to set Copy to output directory to Always copy
Delete bin/obj folders, switch build configuration from debug to release, rebuild project and then back to debug
Try to delete the image from iOS, rebuild, re-add again, and then repeat the steps above
Also, you can try to set the ImageSource as you can see below:
<Image x:Name="logo">
<Image.Source>
<OnPlatform x:TypeArguments="ImageSource">
<On Platform="Android" Value="logo_launcher.png"/>
<On Platform="iOS" Value="logo_launcher"/>
</OnPlatform>
</Image.Source>
</Image>
I hope this can help you.
Maybe I found a possible solution using Assets.xcassets, check the steps below:
Main.xaml
<Image x:Name="iconBrain">
<Image.Source>
<OnPlatform x:TypeArguments="ImageSource">
<OnPlatform.iOS><FileImageSource File="IconBrain"/></OnPlatform.iOS>
<OnPlatform.Android><FileImageSource File="ic_brain"/></OnPlatform.Android>
</OnPlatform>
</Image.Source>
</Image>
Android: Add the images on the respective Drawable folders under Resourses directory.
iOS: Create a new Image Set under Assets.xcassets and add the Images.
Done. No extra properties, configs, or xamarin tricks.
Check out my GitHub for the full solution, also I tried to reproduce the same environment w/ Xamarin.Forms, Shared Project and iPhone 8 - 11.4 simulator.
Another point, you can't add the images under mipmap directories, because the mipmap folders are for placing your app/launcher icons (which are shown on the homescreen) in only. Any other drawable assets you use should be placed in the relevant drawable folders as before.
It’s best practice to place your app icons in mipmap- folders (not the drawable- folders) because they are used at resolutions different from the device’s current density. - Google blogpost
Finally, thank you for the information about the deprecated approach by Apple.
Prior to iOS 9, images were typically placed in the Resources folder with Build Action: BundleResource. However, this method of working with images in an iOS app has been deprecated by Apple. For more information, see Image Sizes and Filenames. - Images in Xamarin.Forms
I hope this can help you.
I tried solutions from the following links but none worked.
where-to-put-images-for-xamarin-froms-application
developer.xamarin.com/guides/xamarin-forms/user-interface/images/#Local_Images
image-not-showing-in-xamarin-forms-app.html
I used below syntax
<Image Source="lock.png" HeightRequest="20" WidthRequest="20" HorizontalOptions="Center" VerticalOptions="Center" Grid.Row="0" Grid.Column="0"></Image>
Here is the image for where i've kept image
No image displays
Edit
These are the only things in my XAML page.
In my case the issue was of assembly.
when trying all the possible solutions like
1.) Changing Build Action
2.) Adding Image to all the folders within Resources
3.) Naming Convention should not include - in File/Image Name.
4.) Changing Background Color
I also got one solution while searching for embedded Images that assembly was missing here.
This change worked for me in using Local Images.
Got that solution from this link
Right click the lock.png image and see if there is an option "include in project"
If there is, project cannot find image
In my case I need to use method Forms.Init(Context activity, Bundle bundle), in my SplashScreen OnCreate method and Activity class OnCreate method. But it also important that images build action properties is set on AndroidResource.
When I click on the ListItem, which is a .PDF, in my ListView, I want to save the file locally. Each ListItem has a different Uri.
Is there any way I can do that?
Currently, it's a bit tricky to implement from scratch a ListView onTap gesture - but you can use a nuget that gives that functionality for Xamarin Forms:
http://www.michaelridland.com/xamarin/freshessentials-for-xamarin-forms-the-must-have-nuget-for-forms/
Basically, in order to use it you can just add a property to your ListView and Bind it to a Command to your CodeBehind or ViewModel (if you're following an MVVM pattern or framework):
<ListView ItemsSource="{Binding MyCars}" fe:ListViewItemTappedAttached.Command="{Binding ItemTapCommand}">
Don't forget to add this resource as property/namespace of your ContentPage tho:
xmlns:fe="clr-namespace:FreshEssentials;assembly=FreshEssentials"
Ex.
<ContentPage xmlns:fe="clr-namespace:FreshEssentials;assembly=FreshEssentials" ... />
On Saving files - Because Xamarin.Forms runs on multiple platforms, each with its own filesystem, there is no single approach for loading and saving files created by the user.
You can follow this guide from Xamarin to know how to do that for every platform you are targeting: https://developer.xamarin.com/guides/xamarin-forms/working-with/files/#Loading_and_Saving_Files
I need the picker page from the toolkit's datepicker control to be in spanish. I tried a few approaches found here on SO, but they didn't work for me.
One thing a noticed is that when i run the sample app from the toolkit, the picker page is localized, but when i run my app which uses the same dll, it does not.
So the question is, what is doing the sample app, that mine doesn't in order for the picker page to be localized?
My DatePicker declaration:
<toolkit:DatePicker Width="280" Background="#96CA04" Foreground="White" Value=" {Binding LeagueBegDate,Mode=TwoWay}" />
Sample App DAtePicker declaration:
<toolkit:DatePicker ValueChanged="DatePicker_ValueChanged"/>
What i had to do was, to open the .csproj file of my project and add es-ES; entry under the <SupportedCultures> node.
I did try adding just es;, but it didn't work. It had to be es-ES; as in the toolkit project, under the LocalizedResources folder the resx files is called: ControlResources.es-ES.resx.
So the entry ended up looking like this:
<SupportedCultures>
es; es-ES;
</SupportedCultures>
To clarify, i did not have to add a localized resources file in my project, just added the supported cultures and the toolkit's localization kicked in.
I'm new to WP7 programming but have XP with other platforms, I am just running through all the basics so I can get to know the language but I have hit a roadblock I've entered a source for my background image:
<Grid x:Name="LayoutRoot">
<Grid.Background>
<ImageBrush ImageSource="C:/users/hypernova/pictures/Background1.jpg">
</ImageBrush>
</Grid.Background>
</Grid>
And the image shows up as a background in the design tab that is next to the XAML but when I debug and the emulator starts its just a black screen no background image, what have I missed? I have tried other ways of setting a background like:
<Grid x:Name="LayoutRoot">
<Canvas>
<Canvas.Background>
<ImageBrush ImageSource="C:/users/hypernova/pictures/Background1.jpg">
</ImageBrush>
</Canvas.Background>
</Canvas>
</Grid>
but the same thing happens I've tried a few other ways also but again nothing, I'm sure I've missed something I should have caught.
The path
C:/users/hypernova/pictures/Background1.jpg
exists on your dev machine, not on any Windows Phone 7+ device.
Remember, users will be downloading your app from the store, then running it on their phones. How would they possibly access your hard drive and get that image?
Unfortunately, since the design surface allows you to do this, it can of course be confusing to a new developer.
What you should do is add your image to your project as a Resource, then reference the resource via a pack URI (don't click on that link unless you want to scare yourself silly).
To add it as a resource, simply add the image to the root of your WP7 project, right click on it, select Properties, and then change the build action to Resource (not Embedded Resource, mind you).
Next, you have to construct a pack URI for this resource. This is ... not easy. You can use the tools in Visual Studio to do this, by editing the properties of your ImageBrush in the designer. This is the simplest, and recommended, route. All you have to do is edit the ImageSource of your ImageBrush in the Properties tool window, and select the image from the list of available images in the solution.
The other way is to manually construct the pack URI. For example, you could take the following
/[project assembly name];component/Background1.jpg
replace [project assembly name] with the name of your assembly (no extensions), and use it as your ImageSource value. You can find the correct project assembly name by looking at the Assembly Name under the Application section of the project properties.
<ImageBrush ImageSource="/MyWp7Application;component/Background1.jpg"/>
Note, depending on how your project folder structure is constructed, this URI may be different. Constructing the correct URI outside of the tools provided is a task deserving of another question.