question

SamR-2505 avatar image
0 Votes"
SamR-2505 asked ·

Save and retrieve multiple xaml files in UWP

How can I save and retrieve multiple xaml files (each file has multiple textbox user input data) in UWP

windows-uwp
· 4
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Why do you want to save and retrieve multiple xaml files directly? To save user input data, it's better to save the data into a model, then you could convert the model to json data and save it to local file.

1 Vote 1 ·

Can you provide an example for this. Thanks!

0 Votes 0 ·

I wrote a simple sample about three textbox in mainPage, here is my sample. I saved the json into local setting instead of local folder. When you want to save mutiple data from mutiple pages, you can create mutiple jsons and save them or put them into a list and save them. Can this solve your issue?


0 Votes 0 ·

I have about 20 text boxes in each of 20 xaml sheets. Does the above procedure work or there is an alternative? I have been using FileSavePicker and FileOpenPicker to save and open individual sheets. But this is not not very efficient. Thanks for your response.


0 Votes 0 ·
SteveWood-4261 avatar image
0 Votes"
SteveWood-4261 answered ·

For security reasons UWP apps cannot open or save a file unless the user selects it...that's what the FilePickers are for. The only exception is your application's local storage folder. You can retrieve a list of files from the folder and present them to the user for them to select:

 Windows.Storage.StorageFolder LocalStorageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
 IReadOnlyList theFileList = await LocalStorageFolder.GetFilesAsync(Windows.Storage.Search.CommonFileQuery.OrderByName);

An alternative is to group all of the xaml files into one file so the user just needs to select one file to retrieve all of the xaml.
You say you have 20 text boxes in each of the 20 xaml sheets...is the user creating those 20 xaml sheets themselves or with another app independent of your application? If so then I suggest that when the user selects a xaml sheet you cache a copy of it in your localfolder and when the user wants to open a xaml sheet you first present a UI containing the list of files from your localstorage plus a button to launch a FilePicker for your app to "consume" a new file.

· 3 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for your input.
Say I have 20 xaml sheets. The user enters data into the text boxes in the first sheet. Calculations are done in the first sheet when the user hits a submit button. Some of the calculated values are carried into the next sheet, where user inputs additional values and further calculations are done. The calculated values are then and transferred to the third sheet and so on. When it comes to saving the user input, in each of the say 20 xaml sheets into a local folder, I would like to have a single save button that can be used to save all the user inputs in the text boxes in each of the 20 xaml sheets. When I want to retrieve the data back, I need to us a single 'open' button to get user data back into all the text boxes in each of the 20 xaml sheets. Hope I am clear in explaining this. All data needs to be saved externally.

0 Votes 0 ·

You can first create a separate list in your App.xaml.cs and save the viewmodel bound with each page to this list after navigation is complete. When you click the save button, just save the data from the list to the local folder. And when you click the open button, read the data and assign a value to the list. Then assign the value from list to the viewmodel of each page.

0 Votes 0 ·

Thanks Fay. An example could be very helpful.

0 Votes 0 ·
FayWang-MSFT avatar image
0 Votes"
FayWang-MSFT answered ·

Hello,​

​Welcome to our Microsoft Q&A platform!

When you click the "save" button, you can convert the lists from App.xaml.cs to Json and save it to the local folder.

 private async void Button_Click(object sender, RoutedEventArgs e)
 {
     string json = Serializer.Serialize(App.lists);
     StorageFolder LocalStorageFolder = ApplicationData.Current.LocalFolder;
     StorageFile file = await LocalStorageFolder.CreateFileAsync("MyData.txt", CreationCollisionOption.OpenIfExists);
     await FileIO.WriteTextAsync(file,json);
 }

When you click the "open" button, get the Json string from local folder and convert it to the lists.

 private async void Button_Click(object sender, RoutedEventArgs e)
 {
     StorageFolder LocalStorageFolder = ApplicationData.Current.LocalFolder;
     StorageFile file = await LocalStorageFolder.CreateFileAsync("MyData.txt",CreationCollisionOption.OpenIfExists);
     string jsonValue = await FileIO.ReadTextAsync(file);
     if (jsonValue.Length>0) {
         App.lists = Serializer.Deserialize>(jsonValue.ToString());
     }
 }

In each constructor, create a viewmodel which bound with the textBox and assign a value to it from lists.

 VM = App.lists[index];

When you navigate to the next page, replace the old viewmodel with the current viewmodel in the lists. Here is a simple sample, you can download and check it.



· 6 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks Fay for the support.
I understand from the code that the data is stored in LocalSettings.
I do not want to store the data in LocalSettings.
I would like to save the data externally in a text file for example.
Also, I would like to retrieve the data from the local file and populate it in appropriate sections.
How can I accomplish this?

0 Votes 0 ·

I have updated my answer, you can check it.

0 Votes 0 ·

Hi Fay, I still see the old file. Have you posted the new file.
If so, what is the new file name. Thanks.

0 Votes 0 ·
Show more comments
PaulRyan-4569 avatar image
0 Votes"
PaulRyan-4569 answered ·

I know this was back in Jan/Feb -- so forgive me if you already know
You can use BROADFILE ACCESS -- gives you complete access You will need to go to setting --> Privacy --> FileS ystem and allow the program access to the filesystem

add the below to 'Package.appxmanifest ' file

<Package
...
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
IgnorableNamespaces="uap mp rescap">
...
<Capabilities>
<rescap:Capability Name="broadFileSystemAccess" />
</Capabilities>

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.