UWP Development, Applying Local Resources

In UWP (Universal Windows Platform) development, the ability for apps to access and utilize local resources is very important. Local resources include data provided by users to the app, and methods for effectively utilizing this data are presented. In this article, we will explain how to apply local resources in UWP apps and provide code implementations through several examples.

1. Understanding Local Resources

Local resources may include files, images, videos, audio files, etc., on the user’s device. These resources can be applied in UWP apps in various ways. Local resources can be classified into the following types:

  • File System Resources: Access to files stored in the user’s local storage.
  • Resources from Other Apps: For example, utilizing clipboard or data from other apps.
  • Settings and User Data: Includes app settings or user-defined data.

2. Setting Access to Local Resources

For a UWP app to access the local file system, the necessary features must be set in the app manifest. Open the Package.appxmanifest file and add the required permissions such as Private Networks (Client & Server), Music Library, Pictures Library in the Capabilities tab.

3. Accessing the File System

The most basic way for a UWP app to access the local file system is to use the StorageFile class. Below is an example of how to read and write files.

3.1. Writing a File


using Windows.Storage;

async Task WriteToFile(string content)
{
    StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
    StorageFile sampleFile = await storageFolder.CreateFileAsync("sample.txt", CreationCollisionOption.ReplaceExisting);
    await FileIO.WriteTextAsync(sampleFile, content);
}
    

The method above creates a sample.txt file in the application’s local folder and writes the provided content to that file.

3.2. Reading a File


async Task ReadFromFile()
{
    StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
    StorageFile sampleFile = await storageFolder.GetFileAsync("sample.txt");
    string content = await FileIO.ReadTextAsync(sampleFile);
    return content;
}
    

This method reads the contents of the sample.txt file in the local folder and returns it.

4. Accessing the Pictures Library

To access the Pictures Library in a UWP app, you need to add the Pictures Library permission. Here’s an example of how to select and display images from the user’s picture library.

4.1. Selecting an Image


async Task PickImageAsync()
{
    var picker = new FileOpenPicker();
    picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
    picker.FileTypeFilter.Add(".jpg");
    picker.FileTypeFilter.Add(".png");

    return await picker.PickSingleFileAsync();
}
    

4.2. Displaying the Selected Image


async void DisplayImage(StorageFile file)
{
    if (file != null)
    {
        var stream = await file.OpenAsync(FileAccessMode.Read);
        var bitmapImage = new BitmapImage();
        bitmapImage.SetSource(stream);

        Image imageControl = new Image();
        imageControl.Source = bitmapImage;

        ContentGrid.Children.Add(imageControl); // ContentGrid is the Grid defined in XAML
    }
}
    

This code uses a file picker to allow the user to select a photo and displays the selected image in the app’s UI.

5. Saving and Reading Data

Saving and reading data in JSON format in UWP apps is a common requirement. Below is an example of using JSON files.

5.1. JSON Data Model


public class UserSettings
{
    public string Username { get; set; }
    public int Age { get; set; }
}
    

5.2. Writing JSON Data


async Task SaveUserSettings(UserSettings settings)
{
    var json = JsonConvert.SerializeObject(settings);
    StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
    StorageFile settingsFile = await storageFolder.CreateFileAsync("settings.json", CreationCollisionOption.ReplaceExisting);
    
    await FileIO.WriteTextAsync(settingsFile, json);
}
    

5.3. Reading JSON Data


async Task LoadUserSettings()
{
    StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
    StorageFile settingsFile = await storageFolder.GetFileAsync("settings.json");
    string json = await FileIO.ReadTextAsync(settingsFile);

    return JsonConvert.DeserializeObject(json);
}
    

6. Conclusion

This article explained how to utilize local resources in UWP development through various examples. Access to local resources is an important factor in enhancing user experience in apps, so it is essential to set the required permissions and provide a user-friendly interface. Accessing local resources is a fundamental part of UWP app development, enabling the diversity and functionality of the app.

7. References