在页面之间传递单选按钮内容

时间:2013-11-20 14:09:49

标签: c# .net windows-phone-7 windows-phone-8 windows-phone

我想在页面之间传递单选按钮内容。 XAML代码:

<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"
                             />

第一页代码:

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)); 

在第二页,我想使用日期格式电台。 例如: 我有一个构造函数:

        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
            }
        };

在这行中ErrorCorrection = ErrorCorrectionLevel.H我想使用单选按钮中的数据。 所以如果用户选择

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

在第二页上,它将是:

ErrorCorrection = ErrorCorrectionLevel.Q

你知道我怎么做吗?

2 个答案:

答案 0 :(得分:1)

所以传递任何类型的对象(包括UIElements)的快速而肮脏的方法是将它们粘贴在PhoneApplicationService.Current.State字典中

类型为Dictionary<String,Object>

例如,如果您想要放置RadioButton,则可以

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

然后,一旦您导航到下一页,就将其拉出来

PhoneApplicationService.Current.State["MyRadioButton"]

所有这一切,只要传递单选按钮的值

就会好得多

例如,

bool isChecked = (bool)MyRadioButton.IsChecked;

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

然后检索它

bool isChecked = (bool)PhoneApplicationService.Current.State["MyRadioButtonIsChecked"]

答案 1 :(得分:0)

如果您只想传递一个变量,可以使用NavigationService传递它 - 例如这样做:
在第一页上,当导航时(我假设您的Q是您要传递的变量):

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

在第二页上,在OnNavigatingTo()中读取该变量:

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

如果你想发送更复杂的对象,可以像here那样进行 - 你可以写一个扩展名:

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;
  }
}

用法:

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

然后在导航后使用:

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

您还可以阅读更多here