Category Archives: windows

Launch another app from metro style app

In Windows(phone) 8 you cannot run other external apps just like that. As seen in Windows 8, Microsoft is moving away from allowing an application to start any other application.  The recommended method of starting another process and that’s to use the File Asssociations. There is some old similar example and a new one.

On the desktop side this

Process p = new Process();
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.FileName = "C:\SomeApp.exe";
    p.Start();

or this

   ProcessStartInfo startInfo = new ProcessStartInfo("C:\SomeApp.exe");
    Process.Start(startInfo);
    startInfo.Arguments = "123";
    Process.Start(startInfo);

would work fine but for Windows 8 we have to use Windows.System.Launcher class.

Basically you will need to “launch” a file or an uri then you will have the option to choose how it will be run if not configured yet.

  var someFile = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(@"Assets\somedata.xml";); 
  bool success = await Windows.System.Launcher.LaunchFileAsync(someFile);

 

Work with Storage, read/write files in Windows Phone 8

Windows Phone 7/8 storage options are quite limited.

here are some notes and snippets about that.

How to save data from your app.

On Windows Phone, writable user storage is handled by Silverlight, not by XNA Game Studio. For more information about saving game data on Windows Phone, see Writing Data (Windows Phone).
example:

try
{
using (IsolatedStorageFile someFile = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream someStream = new IsolatedStorageFileStream("some.txt", FileMode.Append, someFile))
{
using (StreamWriter sw = new StreamWriter(someStream))
{
sw.Write("someText");
sw.Close();
}
}
}
}
catch { }

The problem is that these cannot be really seen by the user without the help Visual Studio tools.

another one:

try
{
byte[] fileBytes = System.Text.Encoding.UTF8.GetBytes("some text").ToCharArray());
StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;

////does not work for Windows.Storage.KnownFolders.CameraRoll; and the others we know.
var dataFolder = await local.CreateFolderAsync("SomeFile",CreationCollisionOption.OpenIfExists);
var file = await dataFolder.CreateFileAsync("some.txt", CreationCollisionOption.OpenIfExists);

using (var s = await file.OpenStreamForWriteAsync())
{
s.Write(fileBytes, 0, fileBytes.Length);
}
}
catch { }

What can be seen from the user must be located in Storage Drive… and the winners are:

1.Documents
2.Music
3.Pictures
4.Ringtones
5.Videos.

  • Documents: Any file, which you have, saved to phone will show up here. However PDF files still don’t show up here as there is no save to phone option with default PDF reader from Microsoft. However, you can read and view the docs from there.
  • Music: This is a bit different from rest as you have folders like Album, Artist, Playlist and Podcast Series. So if you want to do it right, drop the music files into the Album Folder and the rest will take care by the Windows Phone Music App.
  • Ringtones: This special folder makes it easy for you to copy any MP3 file and set it as ringtone on your Windows Phone. Yes, you can have custom ringtone on your Windows Phone. The songs, which you copy, here appear under Custom Label in Ringtones and will work only if the audio file is DRM free and smaller than 30MB, it can be used as custom ringtone.
  • The Video and image folders are pretty simple. All you need is add files to it and it will show up in its respective section i.e. Picture Library & Music + Video App.

 What is accessible:

  • Contacts: Read access available since WP7.5, write-access available via WP8 ContactStore.
  • Appointments: Read-access available since WP7.5. In WP8 you can add individual appointments after user confirmation via the SaveAppointmentTask.
  • Photos: Read access to all folders available since WP7. Write-access to Camera Roll and Saved Pictures available since WP7.5.
  • Audio: Developers can iterate over the music library and even play it via the native player since WP7. Starting WP8 developers can add songs to the music library using MediaLibraryExtensions.SaveSong().
  • Video: Read-write isn’t available. There are security and storage issue with transporting files that big.
  • uSD Card: Starting WP8 apps can register for specific file extensions and read those from the micro-SD card.
  • Documents: No read-write access. But starting WP8 developers can open up docx/xlsx/etc files using Launcher.LaunchFileAsync and it will open up a read-only copy in Office. Users can then choose to save that copy in the Office Hub.

However…

You can do some tricks to save a text file for example as a song. The same can be done with video and other available libraries… but it is nasty .. :S

{
Microsoft.Xna.Framework.Media.PhoneExtensions.SongMetadata metaData = new Microsoft.Xna.Framework.Media.PhoneExtensions.SongMetadata();
metaData.AlbumName = "fakeAlbumName";
metaData.ArtistName = "fakeArtistName";
metaData.GenreName = "fakeGenreName";
metaData.Name = "some_file";
metaData.Duration = TimeSpan.FromSeconds(185);
Microsoft.Xna.Framework.Media.MediaLibrary ml = new Microsoft.Xna.Framework.Media.MediaLibrary();
Uri songUri = new Uri("some_file.txt", UriKind.RelativeOrAbsolute);
//delete the file if already created otherwise we will have an error.
//not that delete needs user input.
if (song != null) 
    Microsoft.Xna.Framework.Media.PhoneExtensions.MediaLibraryExtensions.Delete(ml, song);
try {
song = Microsoft.Xna.Framework.Media.PhoneExtensions.MediaLibraryExtensions.SaveSong(ml, songUri, metaData, Microsoft.Xna.Framework.Media.PhoneExtensions.SaveSongOperation.CopyToLibrary);
} catch (InvalidOperationException ex) {
songCnt++;
}

}

 

More info on that:

storage general – http://msdn.microsoft.com/en-us/library/bb200105.aspx
Writing Data (Windows Phone) – http://msdn.microsoft.com/en-us/library/ff604992.aspx
other examples – http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj681698(v=vs.105).aspx

Windows Phone Developer Registration – phone name

Do you have an issue to register your windows phone 8 for development because it claims another device with the same name is already registered and you have to rename the device with a unique name. The message: “The device name is already in use by this account”.

Well the solution is simple – change the name, but how?

Trying to that from the device will be unsuccessful, you will just be able to see the name in About menu but not change it.

The actual solution is not that intuitive, – you need to change the name from the File Explorer.

Funny Windows it is!

enjoy 🙂