0

I'm trying to save a file to the device.

The problem is that I'm hardcoding the filename from code behind.

enter image description here

My requirement is to ask the user to save a file with a user defined filename. How can I ask the user to save a file by opening a filesave dialog in Xamarin.Forms?

Snostorp
  • 544
  • 1
  • 8
  • 14
Rohan
  • 11
  • 2
  • Actually im generating a pdf and im saving the file with some hardcoded name using ISave interface with dependency service but my requirement is to save the file with user defined filename like it should open a save dialog and access the folders from device.. – Rohan May 04 '17 at 06:06
  • https://googleweblight.com/i?u=https://msdn.microsoft.com/en-us/library/windows/desktop/ms646960(v%3Dvs.85).aspx&grqid=_aSRcGLW&hl=en-IN – Rohan May 05 '17 at 07:24
  • Above link shows that savedialog in windows but i want to save the file like that format in mobile.Can you please help me. Thanks in advance – Rohan May 05 '17 at 07:28
  • 1
    There is no native file save picker in android platform, we need to implement it our self, you can check the xamarin.android project [Directory Selection Sample](https://github.com/xamarin/monodroid-samples/tree/master/android5.0/DirectorySelection), since you're using Xamarin.Forms, you may also need to use custom renderer to create your own view... – Grace Feng May 05 '17 at 09:12

3 Answers3

3

Have you solved it? Can you post your solution in case?
This question could fit your requirements, it uses this plugin: Xam.plugin.filepicker Xam.Plugin.FilePicker Works fine but can't get file

Here is the easiest popup page using Rg.Plugins.Popup
cs:

public partial class PromptPopup : PopupPage
{
    public event EventHandler Oked;
    public event EventHandler Canceled;
    public PromptPopup(string title, string text)
    {
        InitializeComponent();
        PopupText.Text = text;
        PopupTitle.Text = title;
    }

    private void OnCancel(object sender, EventArgs e)
    {
        this.Canceled(sender, e);
        PopupNavigation.PopAsync(false);
    }
    private void OnOk(object sender, EventArgs e)
    {
        this.Oked(sender, e);
        PopupNavigation.PopAsync(false);
    }
}

its xaml:

<StackLayout VerticalOptions="Center" HorizontalOptions="FillAndExpand" Padding="20, 20, 20, 20">
<StackLayout BackgroundColor="White" Padding="10, 10, 10, 10">
  <Label x:Name="PopupTitle" Text="PromptPopupTitle" TextColor="Gray" FontSize="20" HorizontalOptions="Center" />
  <ScrollView>
    <StackLayout>
      <StackLayout Orientation="Horizontal">
        <Label x:Name="PopupText"
               HorizontalOptions="FillAndExpand"
               HorizontalTextAlignment="Center"
               TextColor="Gray"></Label>
      </StackLayout>
      <StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand">
        <Button Text="{i18n:Translate PopupButtonCancel}" Clicked="OnCancel" HorizontalOptions="FillAndExpand"></Button>
        <Button Text="{i18n:Translate PopupButtonOk}" Clicked="OnOk" HorizontalOptions="FillAndExpand"></Button>
      </StackLayout>
    </StackLayout>
  </ScrollView>
</StackLayout>

(this page comes from a PopupPage instead of ContentPage)

and you can call it like this

var page = new PromptPopup("title", "text");
page.Oked += Page_OkedLogout;
page.Canceled += Page_CanceledLogout;
await PopupNavigation.PushAsync(page);

further deep support: https://github.com/rotorgames/Rg.Plugins.Popup

0

You have to implement the file picker yourself, there's no built in way to do that, best to go out to GitHub and look around if that's what you're after.

It sounds to me like you could easily achieve what you're after by just popping a text box, having a user enter a file name, validate the filename with a regex or something and then just saving the file with that file name, you could even just save the file and give them the option to rename the file by listing them all out, but that's all too broad for me to give you an exact implementation, so the best I can do for you is tell you the simple way to do what you're wanting to do.

Trevor Hart
  • 993
  • 7
  • 26
0

After selecting file you just open Popup with "Entry" and save file using Entry Text. you can open popup using "Rg.Plugins.Popup"

Pratik
  • 720
  • 4
  • 20