简体   繁体   中英

Alternative ways of roaming of app settings in uwp

Microsoft docs for unsupported features in windows describes Package State Roaming to be deprecated and will be removed in later versions. Is their any alternative way to sync settings across devices that small developers can use for free??

I would recommend you have a look on Microsoft Graph. With this you could store data on the users OneDrive. This is free and available for other platforms. There are no short and easy answer to get this running, but these are the three major things you need to do:

Integrate Graph Authorization

A good starting point to just get starting with Graph and setup login etc in UWP is this project:

https://github.com/CommunityToolkit/Graph-Controls

Setup permission

Next step is to give you application access to OneDrive. With the Files.ReadWrite.AppFolder permission you just get access to the application folder on OneDrive and nothing else. You could read about this here:

https://docs.microsoft.com/en-us/onedrive/developer/rest-api/concepts/special-folders-appfolder

Be aware that business account does not support this permission level at the moment. There user voice entry about this:

https://onedrive.uservoice.com/forums/913708-onedrive-developer-platform/suggestions/33911101-make-files-readwrite-appfolder-work-with-onedrive

If business account is necessary to support, you could use Files.ReadWrite permission instead. This gives you access to the entire OneDrive. When it comes to coding there is no difference between these two options.

Read and write files

The project mentioned above is using the Graph SDK. The rest API for using OneDrive is documented here with examples how to use it with the SDK:

https://docs.microsoft.com/en-us/graph/api/resources/driveitem?view=graph-rest-1.0

Below is some code that shows you how to do the most essential. The code is storing data in the subfolder Synchronizing1. This makes it possible to store data in a different way in the future without breaking older installations:

var provider = ProviderManager.Instance.GlobalProvider;
var client = provider.GetClient();
string path = "Synchronizing1";
Stream stream = ...;

// Upload or create a file
await client.Me.Drive.Special.AppRoot.ItemWithPath(System.IO.Path.Combine(path, fileName)).Content
        .Request()
        .PutAsync<DriveItem>(stream);

// Open a file for reading
var remoteStream = await client.Me.Drive.Special.AppRoot.ItemWithPath(System.IO.Path.Combine(path, fileName)).Content
    .Request()
    .GetAsync();

// Get all files form a folder
var childrenPage = await client.Me.Drive.Special.AppRoot.ItemWithPath(path).Children
    .Request()
    .GetAsync();

var children = childrenPage.ToList();

while (childrenPage.NextPageRequest != null)
{
    childrenPage = await childrenPage.NextPageRequest.GetAsync();
    children.AddRange(childrenPage.ToList());
}

The doc says you can use Azure App Service .

I once roam data using OneDrive across devices. You need to auth your app to use OneDrive space first, then store users' data to their own OneDrive.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM