Using Local Storage with C# and XAML in Windows 8 Metro Apps
As you dive into development of Windows 8 Metro apps you will most likely run into the need to use local storage. While you may be thinking it's just Windows, I already know how to connect to a database and access the filesystem, unfortunately, the APIs you are used too will not work with Metro apps. Metro apps work on a very different model, whereby each app is restricted to a sandbox.
Working within a sandbox allows for much better control and prevents one app from affecting another. Unfortunately, this also means that you will not be able to use the System.Data namespace to connect to a local and/or a SQL Server on your network. Instead you will need to provide a set of Web Services or other means of accessing remote data as if it were coming from a server on the web. While this may seem counter-intuitive to traditional Windows apps, it makes sense for Metro apps working within a sandbox model.
Local and Roaming Storage
Nonetheless, your app will have the need to store data locally on the machine. Metro supports three different types of storage, which are specific to your app, including Local, Roaming and Temporary. Files stored within the Local folder will only be stored on the machine in which they were created. Unlike the Local folder, the Roaming folder allows for data to be synced between Windows 8 machines running your Metro app. The Temporary folder works as you would expect whereby data stored within the folder will be deleted periodically by Windows. In addition to accessing the file system, you also have the ability to store Local Settings and Roaming Settings, which allow you to store a key/value pair information for your app.
To jump in and start using local storage, you will be using the Windows.Storage.ApplicationData class. The following code snippet is a very simple example of how to create a new file in local storage and write text to it.
async void save_myFile() {//Get the Local Foldervar localFolder = Windows.Storage.ApplicationData.Current.LocalFolder; //Create the File StorageFile myFile = await localFolder.CreateFileAsync("myFile.txt", CreationCollisionOption.ReplaceExisting); //Write File to the file await FileIO.WriteTextAsync(myFile, "Write Test Data!");}
Right off the bat the first line async modifier in the method declaration is new. This modifier is used to tell Metro to allow this method to be executed asynchronously, which is used to help keep the main UI thread responsive while background code is executing. Next we create a local variable, which points to the Local Folder for this application. Then using the local folder we create a new file called myFile.txt. Notice the await expression, which allows the method to pause while the create file is executing. Once complete, we use the FileIO class and the WriteTextAsync to write a single line of text to the file. Using this method allows you to write text to the file without the need to perform open/close operations. If instead you want to use the Roaming Folder the same way, you could replace LocalFolder in the first line with RoamingFolder.
Settings
Taking advantage of storage works a little bit differently; however, it is very intuitive. The following line of code shows how you can add a setting called test and provide a value to it and retrieve the setting.
//Set test setting ApplicationData.Current.LocalSettings.Values["test"] = "Setting Value"; //Read test setting string t = (string)ApplicationData.Current.LocalSettings.Values["test"];
Again you can take advantage of RoamingSettings by switching from LocalSettings to RoamingSettings.
Conclusion
As you can see, it is very easy to take advantage of Local and Roaming storage and settings. At this point, you may be thinking that your app is going to need a small local database such as SQL CE, SQLite, etc. Unfortunately, out of the box Metro does not include a local database; however, due to the popularity of SQLite, several different projects are currently underway to port SQLite over to Metro. In addition, you may be able to get away without the need for a high-level query language such as SQL. Depending on your app requirements you may be able to use a local XML file and/or a JSON (JavaScript Object Notation) formatted file. Nonetheless, Metro does include the basic tools necessary to store local files and settings.
Comments
ugg boots hjktmd
Posted by Suttonfxb on 02/19/2013 04:34amburberry sale rwxvuwfr burberry outlet gapychmc burberry bags xfzxriib burberry handbags wwlhitrs
Replyghd australia vvqixx
Posted by Suttonfsy on 02/13/2013 03:49am1sHyv christian louboutin zUyy longchamp outlet kPpl michael kors outlet 0qMtv 6iOpa chi 3dQod michael kors outlet 9cXlz cheap nfl jerseys 9cZhq nike uk 1lFzx ghd 9aZrs ugg 5cQks toms outlet 1wHfc Tory Burch Yellow Pearl Lace Ballerina Flats Cheap 7gCsc hollister soldes 8rFyy ghd 9fIan ugg boots
Replyghd australia ylsjcs
Posted by Suttonqjh on 02/03/2013 08:34am5aEgg christian louboutin zXcd longchamp outlet uWud michael kors outlet 0bIlt 9gUrw chi 6iNmo michael kors outlet 6cNjs cheap nfl jerseys 7mJhs nike uk 3gVnm ghd 1hVdw ugg 0uIgc toms outlet 1bJnw discount Tory Burch shoes 8jHta hollister france 4dVmy ghd 1fJyu ugg sale
Replyghd australia kxnclr
Posted by Mandyqez on 02/03/2013 08:27am1kTbc ugg france vSga ¥åê©`¥é`¥à µêà n gMll nike shox 4iOuo toms outlet 5oRof hollister outlet 6yAwe ugg pas cher 0zSrf longchamp pas cher 2dUvk louis vuitton shoes 0zOpc michael kors handbags 7bUag christian louboutin norge 3oUyh colin kaepernick jerseys 7mFus 7dEow ghd 1dFbt lisseur ghd pas cher 4iSwh ugg sale
Replycheap ugg boots jZqd xGqh http://www.cheapfashionshoesas.com/
Posted by Mandybli on 01/29/2013 11:28amsWbi chaussures louboutin pas cher tCit longchamp handbags wTsn michael kors 1yOut cheap ugg boots 3rMuo chi straightener 0bLnz michael kors sale 4pAkl cheap nfl jerseys 8nIbe cheap nike air max 4eLwl Lisseur GHD 3rKoz botas ugg 1nMtq cheap toms 7hUph Tory Burch Shoulder White Bags CheapTory Burch Messenger Shoulder Red Bag CheapTory Burch White Khaki Handbags CheapTory Burch Red Sandal CheapTory Burch wallet Blue Cheap 5gIop hollister lyon 0aBjg planchas ghd baratas 1sKfr ugg boots
Replyugg boots ztoyig http://www.cheapfashionshoesas.com/
Posted by Suttonwoh on 01/29/2013 08:37am4mAtj cheap nike shoes pTqn Michael Kors outlet eYyq ugg boots 9sFti monster beats 1fJwi San Francisco Giants 2010 MLB Hats Black Wholesale Sell 2pBpj ugg norge 7xNre burberry outlet 4tZuv longchamp 5hIjv cheap nike free run 7aFkq cheap uggs 8sJyz monster beats 9xDgy ugg 2nUgt ghd 0sEke 3sAtj
Replyugg boots mlisxz http://www.cheapfashionshoesas.com/
Posted by Mandyuws on 01/27/2013 06:15am3tTpo nike factory gLqi Michael Kors outlet mPxc ugg boots 7rDjl beats by dre 2cQld Cheap nfl jerseys 8sUnb ugg sko 1nHhr burberry handbags 1pZmb longchamp 7eUhf cheap nike air max 0pXva cheap ugg boots 1jJoo beats by dre 0eTti ugg 4gLhc GHD Australia 1aDbs 4sGuh
Replyghd australia gvuodv http://www.cheapfashionshoesas.com/
Posted by Mandyoyq on 01/26/2013 08:42pm6cJiq ugg jSst hMpn nike shox norge 6lCsn toms sale 9sChl burberry outlet 7qAio chaussures ugg 0wRqk longchamp 9lHvb louis vuitton outlet 4cYqq michael kors handbags 6hLtz christian louboutin norge 5sNrx ugg boots 8mDyk discount nfl jerseys 2fShr 3vZyp styler ghd 1dKop ugg boots uk
Replyugg boots mrsvgi http://www.cheapfashionshoesam.com/
Posted by Mandyxiy on 01/16/2013 02:00am5hWmj cheap ugg boots lPoj Michael Kors outlet sKws ugg boots 9hFvl Burberry outlet 6uYro Cheap nfl jerseys 8rWrb coach,coach outlet,coach usa,coach factory outlet,coach factory 1yCjg burberry outlet 5eWce christian louboutins 0aBhq 3uTrb 3iSbp 3zQfx 5gJum 0oEvp 8qMzj
ReplyVery useful
Posted by Jake on 11/15/2012 10:41pmI found this guide VERY USEFUL ! Thank you !
ReplyLoading, Please Wait ...