A XOML data file.
CzDataFile is the XOML representation of a file (a file holder) and enables files to be created and loaded using the File XOML tag.
Its often useful to be able to load files into an app and do something with the data that they contain. Files in XOML are just like any other resource and can be declared inside a scene in which case they become local to the scene or declared outside a scene in which case they become global. They can also be loaded from local storage or from a web server, Files can also convert their data after loading from an number of different formats. We will begin by taking a look at the properties that are available for the Files tag:
- Name (string) - Name of this file resource
- Tag (string) - Resource tag (used to group resources together) (optional)
- Location (filename) - Name of the file including extension (can include web addresses)
- Preload (boolean) - If set to true then the file will be loaded immediately. By setting to false the file will be loaded when it is first used or can be loaded by an action later (default is true).
- Blocking (boolean) - Web based files take time to download from the web so its useful to allow execution of the app to continue whilst it downloads. To prevent the file download from blocking the app set Blocking="false" (default is to block).
- Condition (variable) - A condition variable that must evaluate to true for this resource to be loaded (this is an optional feature and can be used to conditionally load resources based on certain conditions such as screen size or device type etc..) (optional)
- FileType (string) – Type of file (does not affect how the file is loaded) (optional)
- Variable (variable) – Name of the variable that the contents of the file should be written into once loaded (optional)
- Script (script) - Name of the script that the contents of the file should be written into once loaded. This is used only to load a script resource with script (optional)
- Converter (type) – How to convert the loaded data to text (html, hex, urlencoded). This is useful if you have data in a common web format such as url-encoded. Passing urlencoded will cause the data to be converted from url-encoded to plain text format (optional)
- OnLoaded (actions list) - Actions list to call when the file is loaded
- OnError (actions list) - Actions list to call when an error occurs
The Files example has been provided to show how to load a file and reload its contents. Lets take a look at the XOML for this example:
<!-- Create a variable to load a file into -->
<Variable Name="FileContents" Type="string" />
<!-- Declare a file -->
<File Name="File1" Location="file1.txt" FileType="txt" Variable="FileContents"
Preload="true" />
<!-- Create a scene -->
<Scene Name="Scene1" Current="true" >
<!-- Create a label to display our files contents -->
<Label Font="serif" Size="200, 200" TextColour="255, 255, 128, 255"
Background="Button1Brush" BackgroundColour="255, 80, 80, 255"
Binding="[Text]FileContents" />
<!-- Create a group of buttons to load 3 different files -->
<Label Font="serif" Size="80, 50" Position="-100, 100" Text="Load File1"
Background="Button1Brush" BackgroundColour="80, 80, 255, 255" OnTapped="Load">
<Actions Name="Load">
<Action Method="LoadFile" Param1="File1" Param2="true" Param3="file1.txt" />
</Actions>
</Label>
<Label Font="serif" Size="80, 50" Position="0, 100" Text="Load File2"
Background="Button1Brush" BackgroundColour="80, 80, 255, 255" OnTapped="Load">
<Actions Name="Load">
<Action Method="LoadFile" Param1="File1" Param2="true" Param3="file2.txt" />
</Actions>
</Label>
<Label Font="serif" Size="80, 50" Position="100, 100" Text="Load File3"
Background="Button1Brush" BackgroundColour="80, 80, 255, 255" OnTapped="Load">
<Actions Name="Load">
<Action Method="LoadFile" Param1="File1" Param2="true" Param3="file3.txt" />
</Actions>
</Label>
</Scene>
We begin this example by creating a variable called FileContents that we will later load our files contents into. Next we create a File that loads the contents of file1.txt and writes it to the FileContents variable. Next we create a label that will sho the files contents due to the binding to the FileContents variable. Lastly we create 3 label buttons that each call the LoadFile action when tapped. Each of the LoadFile actions load different files into the FileContents variable.