Tips and Ticks (1)

  • How to force tombstoning for debug in Mango?
    In order to test your tombstoning code you can force your application to be tombstoned instead of kept in memory during debugging. To do this, just open your project properties panel, click the debug tab and check Tombstone upon deactivation while debugging.
  • How to remove a Page from backStack in Mango?
    Sometime (ex. LoginPage, DisclaimerPage) it is useful to change the back navigation normal behaviour. Before Mango the only way to change the back navigation was to use a work around like generate a QuitException (see http://www.imaginativeuniversal.com/blog/post/2010/08/22/How-to-Quit-a-WP7-Silverlight-Application.aspx). Now with Mango you can use the useful NavigationService.RemoveBackEntry() method. Unfortunately, because the backStack is a LIFO, it is possible to remove only the top entry, that is the previous page entry. So the best way to remove a page from the backStack is to use that method just in the overridden OnNavigatedFrom method of that page itself.

    protected override void OnNavigatedFrom(NavigationEventArgs e) {
      this.NavigationService.RemoveBackEntry(); //remove itself from the backStack
      base.OnNavigatedFrom(e);
    }
    

    In particular for a LoginPage (if it is not implemented as a popup window or a section of the MainPage hidden/visible properly), if the login is not always requiìred but depends from some configuration settings (ex. once a day login) it is convinent to call first the MainPage, check if the user is already authenticated in the _load method and if not call the LoginPage. A convenient flag can be used to discriminate in the OnNavigatedFrom method of the MainPage if the LoginPage is called or not and so eventually use the NavigationService.RemoveBackEntry().

    void MainPage_Loaded(object sender, RoutedEventArgs e) {
      if (!DataSourceManager.LoginDataSource.User.Authenticated) {
        //if the user is not authenticated, let's go to the LoginPage
        NavigationService.Navigate(new Uri("/LoginPage.xaml", UriKind.Relative));
        toLoginflag = true;
      } else {
        Main.Visibility = System.Windows.Visibility.Visible;
      }
    }
    
    protected override void OnNavigatedFrom(NavigationEventArgs e) {
      if (toLoginflag) {
        this.NavigationService.RemoveBackEntry(); //remove itself from the backStack
      }
    }
  • Be careful when debugging a CameraCaptureTask with a Windows Phone device!!
    You must disconnect the device from the PC if you want to try the launcher for taking a picture from your program. Otherwise, when you call the Show method of that task, the registered cameraTask_Completed event is suddenly rised with a PhotoResult.TaskResult ==TaskResult.Cancel .
  • Mango provides a build in progressbar shown in the system tray!!
    No need to use the PerformanceProgressBar of the toolkit anymore.
    You just have to add in App.cs:

    public static ProgressIndicator progInd = newProgressIndicator();
    

    and in the constructor add:

    progInd.IsIndeterminate = false; progInd.IsVisible = false;
    

    Then add in the page where you need the progressBar a method like this:

    privatevoid RunProgressBar(bool value) {
      if (value) {
        App.progInd.Text = LocalizedStrings.GetString(“Loading”);
        App.progInd.IsIndeterminate = true;
        App.progInd.IsVisible = true;
        SystemTray.SetProgressIndicator(this, App.progInd);
      }
      else {
        App.progInd.Text = null;
        App.progInd.IsIndeterminate = false;
        App.progInd.IsVisible = false;
        SystemTray.SetProgressIndicator(this, null);
      }
    }
    
  • How you can easely know how a page is navigated (for example if it is called for the first time or from a back)?
    You can override the OnNavigationTo method and test its  NavigationEventArgs parameter comparing it with the appropriate value of the NavigationMode emumeration. For example:

    if(e.NavigationMode == NavigationMode.New) { //it is called for the first time
    

    Remember that the OnNavigationTo method is called just after the page contructor and befor the possible MyPage_Load method, if you define that new RoutedEventHandler in the page contructor

     this.Loaded += newRoutedEventHandler(MyPage_Loaded);) 
  • How you can wrap a text longer than a line in the text of a CheckBox?
    You must override TextBox’s style inside the CheckBox tag:

                            <CheckBox x:Name="checkBoxMergeAudio" IsChecked="{Binding IsMergeAudio, Mode=TwoWay}" Margin="0,0,0,0" Click="checkBoxMergeAudio_Changed" ClickMode="Release">
                                <TextBlock TextWrapping="Wrap" Text="{Binding LocalizedResources.MergeAudio, Source={StaticResource LocalizedStrings}}" />
                            </CheckBox>
    
  • How to play an audio recorded with Phone 7 off somewhere else (ex. on a PC)?
    If you record voice with the Microsoft.Xna.Framework.Audio.Microfone,  it just gets a PCM encoded stream of samples. Only if you use the DynamicSoundEffectInstance on the Phone 7 code the PCM encoded stream can be played also without the wav header. So, if you want to send this audio stream off somewhere else (ex. to PC Server using a web service) to be played later, it needs a proper header otherwise no PC audio player can read it. Here you can find useful code: http://damianblog.com/2011/02/07/storing-wp7-recorded-audio-as-wav-format-streams/
    If you need to merge more audio in a single one, you need to recontruct the wav header followed by the data content of each sigle audio file. Here you can find a useful link if all the wav files have the same coding: http://www.codeproject.com/Articles/15187/Concatenating-Wave-Files-Using-C-2005
  • Remember to escape the string of URI data passed in a query when you want to Navigate to an other page.
    If any of your query strings contain characters that are considered invalid in a Uri what you’re doing will fail, as you’ve discovered. You need to use Uri.EscapeDataString to escape any illegal characters first.
    The escaped strings are automatically unescaped when you read them using NavigationContext.QueryString, so there’s no need to call Uri.UnescapeDataString explicitly.
    For example:

    StringBuilder sbPage = new StringBuilder("/").Append(baseUri);
    sbPage.Append("?ID=").Append("&asset=").Append(<strong>Uri.EscapeDataString</strong>(asset.<em>Description</em>));
    NavigationService.Navigate(new Uri(sbPage.ToString(), UriKind.Relative));
    
Annunci

Informazioni su Enzo Contini

Electronic engineer
Questa voce è stata pubblicata in Windows Phone/Mobile. Contrassegna il permalink.

Lascia un Commento/Leave a comment

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...