Include files with your app

You may need to include additional (non-code) files for your app. Such resources commonly include things like images, fonts, or other visual elements for styling the app. For an ArcGIS Maps SDK for .NET app, you might also need to provide things like mobile map/scene packages (.mmpk, .mspk), tile packages (.tpk, .vtpk), geodatabases (.geodatabase), locators (.loc/.loz), and so on.

Include resources for your .NET MAUI app

In your .NET MAUI project, you can include resources (files) in a centralized location for consumption on each platform your app supports. Files you want to include with an app are typically placed in the Resources folder of your .NET MAUI app project. The Resources folder contains subfolders for various types of files, like images, fonts, and styles. You should add raw assets like the data files mentioned above to the raw subfolder with a build action of MauiAsset.

Add a raw asset to your project

These files are deployed with your package and are accessed using code like this:

Use dark colors for code blocksCopy
1
    var stream = await FileSystem.OpenAppPackageFileAsync("ChulaVista.mmpk");

Accessing raw resources in the app package

How files are packaged varies a lot from platform to platform. For instance, files on Android are available as a stream that you can read rather than as a file on disk. For iOS, they are read-only files. A good technique when working with these files is to read a file from the package and write it to a local file in the app's data folder. Working with the file in the data folder avoids having to handle cross-platform package differences.

The following function reads a resource from the package and writes it to a file in the app's data folder (a folder created for your app when it is installed). The location of the app's data directory varies across platforms, but can consistently be referenced by the AppDataDirectory File system helper. The function checks if the file exists, and if it doesn't, the resource is written to a new file in the app's data directory.

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
        private async Task<string> GetAppDataDirectoryFilePath(string resourceFilePath, string fileName)
        {
            // Check to see if the mobile map package is already present in the AppDataDirectory.
            if (File.Exists(Path.Combine(FileSystem.Current.AppDataDirectory, fileName)))
            {
                // If the mobile map package is present in the AppDataDirectory, return the file path.
                return Path.Combine(FileSystem.Current.AppDataDirectory, fileName);
            }

            // Open the source file.
            using Stream inputStream = await FileSystem.OpenAppPackageFileAsync(resourceFilePath);

            // Create an output filename
            string targetFile = Path.Combine(FileSystem.Current.AppDataDirectory, fileName);

            // Copy the file to the AppDataDirectory
            using FileStream outputStream = File.Create(targetFile);
            await inputStream.CopyToAsync(outputStream);

            return targetFile;
        }

The following code shows how to use this function to copy a mobile map package from the package to the app's data folder. The code then creates a MobileMapPackage from the file in the app's data folder. In this example, the "ChulaVista.mmpk" file was added to the project's Resources/raw folder with a build action of MauiAsset.

Use dark colors for code blocksCopy
1
2
3
4
5
    // Get the path to the mobile map package.
    string mmpkPath = await GetAppDataDirectoryFilePath("ChulaVista.mmpk", "ChulaVista.mmpk");

    // Create the mobile map package.
    MobileMapPackage mobileMapPackage = await MobileMapPackage.OpenAsync(mmpkPath);

Storing resources in a different folder

You may want to use a different folder in your project to store the raw resources used by your app. For example, you may want to create a new folder called "Assets" (at the same level as the "Resources" folder) and use it to store your app's resource files.

Add a raw asset to your project

Your code will work nearly the same as described above as long as you set the Build Action to MauiAsset. The one difference will be how you reference the resource when using FileSystem.OpenAppPackageFileAsync(). In this case, you must prefix the resource name with the name of the folder. For example:

Use dark colors for code blocksCopy
1
var inputStream = await FileSystem.OpenAppPackageFileAsync("Assets/ChulaVista.mmpk");

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.