1

I want to pass radio button content between pages. XAML Code:

<RadioButton Name="errorCorrectionHLevelRadioButton"
                             Content="H (~30% correction)"
                             GroupName="errorCorrectionLevel" 
                             IsChecked="True" BorderBrush="Black" Foreground="Black" Background="Black"
                             />

                    <RadioButton Name="errorCorrectionLLevelRadioButton"
                             Content="Q (~25% correction)"
                             GroupName="errorCorrectionLevel" BorderBrush="Black" Foreground="Black" Background="Black"
                             />

                    <RadioButton Name="errorCorrectionMLevelRadioButton"
                             Content="M (~15% correction)"
                             GroupName="errorCorrectionLevel" BorderBrush="Black" Foreground="Black" Background="Black"
                             />

                    <RadioButton Name="errorCorrectionQLevelRadioButton"
                             Content="L (~7% correction)"
                             GroupName="errorCorrectionLevel" BorderBrush="Black" Foreground="Black" Background="Black"
                             />

First page code:

string myECL;
            if (errorCorrectionHLevelRadioButton.IsChecked == true)
                myECL = ErrorCorrectionLevel.H.ToString();
            else if (errorCorrectionQLevelRadioButton.IsChecked == true)
                myECL = ErrorCorrectionLevel.Q.ToString();
            else if (errorCorrectionMLevelRadioButton.IsChecked == true)
                myECL = ErrorCorrectionLevel.M.ToString();
            else
                myECL = ErrorCorrectionLevel.L.ToString();

            NavigationService.Navigate(new Uri("/QRGeneratePage.xaml?text=" + textToEncodeTextBox.Text +"&errorCorrection="+myECL+"&logo="+logoQrCodeImage.Source, UriKind.Relative)); 

And on the second page I want to use date form radio buton. For example: I have a constructor where:

        string errorCorrectionLevelChoose = String.Empty;
        if (NavigationContext.QueryString.TryGetValue("errorCorrection", out errorCorrectionLevelChoose))
        {
            ErrorCorrectionLevel ecl = (ZXing.QrCode.Internal.ErrorCorrectionLevel)errorCorrectionLevelChoose;
        }

        var writer = new BarcodeWriter
        {
            Format = BarcodeFormat.QR_CODE,
            Renderer = new ZXing.Rendering.WriteableBitmapRenderer()
            {
            Foreground = colorQRCode
            },
            Options = new ZXing.QrCode.QrCodeEncodingOptions
            {
                Height = 300,
                Width = 300,
                Margin = 1,
                ErrorCorrection = ErrorCorrectionLevel.H
            }
        };

In this line ErrorCorrection = ErrorCorrectionLevel.H I want to use my data from radio button. So if user choose

<RadioButton Name="errorCorrectionLLevelRadioButton"
                             Content="Q (~25% correction)"
                             GroupName="errorCorrectionLevel" BorderBrush="Black" Foreground="Black" Background="Black"
                             />

On the second page it will be:

ErrorCorrection = ErrorCorrectionLevel.Q

Do you know how I can do this ?

2 Answers2

1

So a quick and dirty way of passing any type of object, including UIElements is to stick them in the PhoneApplicationService.Current.State dictionary

It is of type Dictionary<String,Object>

For example, if you had a RadioButton you wanted to put in there you could

var myButton =    PhoneApplicationService.Current.State.add("MyRadioButton",TheInstanceOfMyRadioButton);

Then, once you navigate to your next page you pull it back out

PhoneApplicationService.Current.State["MyRadioButton"]

All that said, you would be much better off just passing the value of the radio button

For example,

bool isChecked = (bool)MyRadioButton.IsChecked;

PhoneApplicationService.Current.State.add("MyRadioButtonIsChecked",isChecked);

Then to retrieve it

bool isChecked = (bool)PhoneApplicationService.Current.State["MyRadioButtonIsChecked"]
DotNetRussell
  • 9,716
  • 10
  • 56
  • 111
  • Yeah - you are right - it will be far more easier if he do it this way, and move those if statements to his second page. – Romasz Nov 20 '13 at 21:06
  • It will be also easier if he saves in State his ErrorCorrection rather than his RadioButton's Instance. – Romasz Nov 20 '13 at 21:40
0

If you want only to pass a variable you can use NavigationService to pass it - for example do it like this:
On the first page, when Navigating (I assume your Q is the variable you want to pass):

string myQ = Q.ToString();      
NavigationService.Navigate(new Uri("/secondPage.xaml?Q=" + myQ, UriKind.Relative));

On the second page, in OnNavigatingTo() read that variable:

string myQ;
NavigationContext.QueryString.TryGetValue("myQ", out myQ);
// it's string so you probably need to for example Q = int.Parse(myQ);

If you want to send more complex objects you can do it like here - you can write an extension:

public static class Extensions
{
  private static object Data;

  public static void Navigate(this NavigationService navigationService,
                              Uri source, object data)
  {
     Data = data;
     navigationService.Navigate(source);
  }

  public static object GetNavigationData(this NavigationService service)
  {
     return Data;
  }
}

Usage:

NavigationService.Navigate(new Uri("/Page2.xaml", UriKind.RelativeOrAbsolute), ErrorCorrectionLevel.H);

Then after Navigating you use:

object myQ = NavigationService.GetNavigationData();
ErrorCorrection fromPreviousPage = (ZXing.QrCode.Internal.ErrorCorrectionLevel)myQ;

You can also read some more here.

Community
  • 1
  • 1
Romasz
  • 29,662
  • 13
  • 79
  • 154
  • ErrorCorrectionLevel.H is a class ZXing.QrCode.Internal.ErrorCorrectionLevel so it's not my variable. After ErrorCorrectionLevel. I have a choice of four option (Q, M, H, L). – user2962457 Nov 20 '13 at 19:57
  • @user2962457 You can play with strings which you pass to the next page, and then depending on which string you can choose your CorrectionLevel. Or you can pas this ErrorCorrection as complex object - I've edited post above. I think you should figure out how to do it. – Romasz Nov 20 '13 at 20:05
  • @user2962457 Please tell where and what error you get. It's probably because you try to cast a string to your error class. If you have chosen to do it such a way, the simplest will be that you make some if statements like this: if (errorCorrectionLevelChoose == ErrorCorrectionLevel.H.ToString()) ErrorCorrectionLevel ecl = ErrorCorrectionLevel.H; - its hard coded but should work. – Romasz Nov 20 '13 at 20:54
  • I had an error in line: ErrorCorrectionLevel ecl = (ZXing.QrCode.Internal.ErrorCorrectionLevel)errorCorrectionLevelChoose; – user2962457 Nov 20 '13 at 21:06
  • I have edited just like you have written and in line rrorCorrectionLevel ecl = ErrorCorrectionLevel.H; I have error: Error 1 Embedded statement cannot be a declaration or labeled statement – user2962457 Nov 20 '13 at 21:06
  • @user2962457 ErrorCorrection ecl = ErrorCorrectionLevel.H; - it probably should be like this. On the other hand I think AMR posted easier method. – Romasz Nov 20 '13 at 21:14