Visualizzazione post con etichetta windows RT. Mostra tutti i post
Visualizzazione post con etichetta windows RT. Mostra tutti i post

sabato 4 gennaio 2014

Windows Phone 8 - Folders and files


Windows Phone 8 introduced the ApplicationData class to easily work with IsolatedStorage and async/await keywords.

In Windows Phone currently it is possible to use only LocalFolder because RoamingStorage and TemporaryStorage throw the NotSupportedException.

However, all the operations can be done with a few lines.

How to use folders

Create folder

StorageFolder folder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("myFolder", CreationCollisionOption.ReplaceExisting);


Read folder

var folder = await ApplicationData.Current.LocalFolder.GetFolderAsync("myFolder");


Check if folder exists

StorageFolder folder;
try
{
    folder = await ApplicationData.Current.LocalFolder.GetFolderAsync("myFolder");
    MessageBox.Show(folder.Name);
}
catch (FileNotFoundException ex)
{
    MessageBox.Show("Folder not found");
}


Read all folders

var folders = await ApplicationData.Current.LocalFolder.GetFoldersAsync();
foreach (var folder in folders)
    MessageBox.Show(folder.Name);


Rename folder

var folder = await ApplicationData.Current.LocalFolder.GetFolderAsync("myFolder");
await folder.RenameAsync("myRenamedFolder");


Delete folder

var folder = await ApplicationData.Current.LocalFolder.GetFolderAsync("myFolder");
await folder.DeleteAsync(StorageDeleteOption.PermanentDelete);


How to use files

Create file

var file = await ApplicationData.Current.LocalFolder.CreateFileAsync("myFile.txt", CreationCollisionOption.ReplaceExisting);


Read all files

var files = await ApplicationData.Current.LocalFolder.GetFilesAsync();
foreach (var file in files)
    MessageBox.Show(file.Name);


Check if file exists

StorageFile file;
try
{
    file = await ApplicationData.Current.LocalFolder.GetFileAsync("myFile.txt");
    MessageBox.Show(file.Name);
}
catch (FileNotFoundException ex)
{
    MessageBox.Show("file not found");
}

Write file

var file = await ApplicationData.Current.LocalFolder.CreateFileAsync("myFile.txt", CreationCollisionOption.ReplaceExisting);
var stream = await file.OpenAsync(FileAccessMode.ReadWrite);
using (var writer = new DataWriter(stream.GetOutputStreamAt(0)))
{
    writer.WriteString("Hello");
    await writer.StoreAsync();
    await writer.FlushAsync();
}


Read file

var file = await ApplicationData.Current.LocalFolder.GetFileAsync("myFile.txt");
var stream = await file.OpenAsync(FileAccessMode.Read);
 
using (var reader = new DataReader(stream.GetInputStreamAt(0)))
{
    var bytes = await reader.LoadAsync((uint)stream.Size);
    var s = reader.ReadString(bytes);
    MessageBox.Show(s);
}


Rename file

var file = await ApplicationData.Current.LocalFolder.GetFileAsync("myFile.txt");
await file.RenameAsync("myFileRenamed.txt");


Delete file

var file = await ApplicationData.Current.LocalFolder.GetFileAsync("myFile.txt");
await file.DeleteAsync(StorageDeleteOption.PermanentDelete);


Copy file

var file = await ApplicationData.Current.LocalFolder.GetFileAsync("myFile.txt");
var folder = await ApplicationData.Current.LocalFolder.GetFolderAsync("myFolder");
await file.CopyAsync(folder);


Move file

var file = await ApplicationData.Current.LocalFolder.GetFileAsync("myFile.txt");
var folder = await ApplicationData.Current.LocalFolder.GetFolderAsync("myFolder");
await file.MoveAsync(folder);

The sample project

You can download the sample project here.


The original post is in the Microsoft TechNet Wiki

venerdì 13 settembre 2013

Force binding update for a TextBox


The TextBox control updates the Text binding source only when it looses focus.

When you are writing in a TextBox and you press a button in the ApplicationBar, the focus not changes and your MVVM property doesn't update.

The workaround is to force the binding update like this:
 
var focusedElement = FocusManager.GetFocusedElement();
var focusedTextBox = focusedElement as TextBox;

if (focusedTextBox != null)
{
    var binding = focusedTextBox.GetBindingExpression(TextBox.TextProperty);
    if (binding != null)
        binding.UpdateSource();
}


giovedì 22 agosto 2013

Windows Store Apps Succinctly - Free eBook

Some days ago I started to develop an application for Windows Store.
I love to develop in C#, and after some pages in MSDN documentation, I searched for an offline free e-book to read with my tablet.
I found a great resource from Syncfusion:

Windows Store Apps Succinctly

This book is written by John Garland and you can download 185 pages in PDF or Kindle format.

Before starting, please remember: if you are a beginner in development, this ebook is not for you.

Why is it a good choice? Because it's simple and fast to read. It starts from core concepts until you get to the deployment. Then at the end of this book, you are ready to fully develop your first application.

Main Chapters
  1. Core Concepts: the introduction to Windows Store apps, WinRT and the Windows Runtime with a simple "Hello World" sample.
  2. XAML, Controls, and Pages: all you need to know about XAML, from Namespace declarations, to Animations and Data Binding. The chapter shows all essential controls for the user interface and explains how to work with Pages and Frames.
  3. Application Life Cycle and Storage: one of the most important chapters. The Windows Store apps life cycle is not the same as the desktop apps, then you need to understand all steps for the the best user experience. This chapter continues with Data Storage. It explains how to work with Application Data (local, roaming, temporary), User Data (file and folder picker) and Data Storage (files/folder and some useful links to LiveConnect and SQLite). 
  4. Contracts and Extensions: the Windows 8 Charms: search, share, print, app settings, etc.. The chapter ends with handling file types and protocols.
  5. Tiles, Toast, and Notifications: yes..the big feature of Windows Phone, now in Windows 8. If you want to release a great app, you really need a good Live Tile. This chapter covers all types of Tiles and how to schedule the updates. Then it talks about the Toast Notifications and ends with a sample of Push Notification.
  6. Hardware and Sensors: an overview of all sensors (like compass, gyroscope, accelerometer and gps) and how to interact with Camera. The user love those features, then use them in your app.
  7. Deployment: your application is done, let's publish it on Windows Store! Understand the Store prices and accounts; learn how to use the Windows Application Certification Kit; add the trial mode and the In-app purchase; configure the PubCenter and the Ads.

Pro
  • Free! :)
  • Easy and fast to read
  • XAML + C#
  • All development cycle
  • A lot of samples and tips
  • Kindle format
  • Not for development beginners

Cons
  • No NFC samples
  • No gestures samples

Now enjoy your reading... thanks Syncfusion.

FYI another good free resource is Metro Studio: a customizable collection of icon templates.

giovedì 15 agosto 2013

String format in Xaml

When you need to concatenate some strings in a TextBlock you can use the tag like Run.

Silverlight 4 has introduced an useful feature to make this more easy: StringFormat.
Now you can put more informations in a single element.

The code is very simple:
<TextBlock Text="{Binding Username, StringFormat='Hello \{0\}'}" />
<TextBlock Text="{Binding Temperature, StringFormat=\{0\}°}" />

and it works also with numbers and dates:
<TextBlock Text="{Binding Value, StringFormat=\{0:n2\}}" />
<TextBlock Text="{Binding Value, StringFormat=\{0:c2\}}" />
<TextBlock Text="{Binding Date, StringFormat=f}" />

You can check more details in the Kunal's blog

domenica 4 novembre 2012

Windows 8 - Images and DPI

Windows 8 is DPI aware then the applications auto-scale images according to DPI and screen resolution:
  • 100% when the scale is not applied.
  • 140% when the screen resolution is 1920x1080 and for all devices with minimum 174 DPI.
  • 180% when the screen resolution is 2560x1440 and for all devices with minimum 240 DPI.

The developer can use vectorial elements like SVG or XAML without problems.

Otherwise when the developer uses the images raw, bitmap, png or jpg, he needs to create a file for every case.

He can explicit the scale factor in the file extension:
\myLogo.scale-100.jpg
\myLogo.scale-140.jpg
\myLogo.scale-180.jpg

Or use the folder convention:
\scale-100\myLogo.jpg
\scale-140\myLogo.jpg
\scale-180\myLogo.jpg

When the application don't has static images, it's possible to create a logic to have the same result from code behind.
switch (DisplayProperties.ResolutionScale)
{
 case ResolutionScale.Scale100Percent:
  img.Source = new BitmapImage(new Uri("url?s=100"));
 break;
 case ResolutionScale.Scale140Percent:
  img.Source = new BitmapImage(new Uri("url?s=140"));
 break;
 case ResolutionScale.Scale180Percent:
  img.Source = new BitmapImage(new Uri("url?s=180"));
 break;

 default:
 // some exception
}

The class DisplayProperties has some event to help the developer:
  • ColorProfileChanged: when the color profile changes.
  • LogicalDpiChanged: when the the pixel per inches (PPI) changes.
  • OrientationChanged: when device orientation changes.
  • StereoEnabledChanged: when the property StereoEnabled changes (3D stereoscopic).