In this lesson, you will learn about the fundamental problems in managing files and resources in cross-platform mobile development such as distribution, synchronization, duplication and address consistency.
You will also learn about the Zebble innovation to bring a consistent, simplified and clean API for managing files and folders with no compromise in flexibility.
In all mobile platforms other than Zebble the process of transferring files from a mobile app source code to the run-time (simulator or device) for installation and testing is very difficult. In general you can either include the files as embedded resources or as bundle contents. But both options are problematic:
- Content Files don't get uploaded to the runtime each time you compile and test. You have to go through a lengthy reinstallation process to make it work, and have to apply special settings for each file in your Visual Studio project. It's inconvenient, error-prone and unreliable, leading to hard-to-debug problems. Specially when you update or replace a file, it gets really tricky to consistently cascade the change runtime.
- Embedded Resources are easier to transfer to the device or simulator consistently, but they come with their own problems. Firstly the API to retrieve them is awkward, error-prone and ugly. Secondly you can't at runtime modify such files, delete or update them. So it's not possible to use it for many scenarios.
Zebble introduces a clever new model to solve the mobile file problem.
Although the files in your Visual Studio source will be Embedded Resources, but you can then deal with them in your app code simply as files and not embedded resources using the familior System.IO API in .NET such as FileInfo or DirectoryInfo.
The physical location of the files will be different in every platform. So you cannot reference any file with its absolute path. To find the actual path of any file or directory, use the following:
DirectoryInfo myDirectory = Device.IO.Directory(relativePath);
var imagesDirectory = Device.IO.Directory("Images");
// This returns the actual file on the device, corresponding to the [App.UI]\Resources\Images\Something.png in your Visual Studio source.
var myFile = Device.IO.File("Images/Something.png");
Some views such as ImageView or Video have a Path property to specify their source. The value that you specify for them should be simply the relative path of the file inside the Resources folder. For example:
However, they also support an absolute file path. This is handy becuase sometimes your source file may be outside of the Resources directory, such as a file in the device's Cache folder.
For example, if you have a FileInfo object in your code, and you want to set as as the source of an ImageView, you don't have to convert it back to the relative path format. Instead simply use the absolute form. For example:
myImageView.Path = myFile.FullName; // This is the absolute path, but it's fine.