簡體   English   中英

在頁面之間傳遞單選按鈕內容

[英]Pass radio button content between pages

我想在頁面之間傳遞單選按鈕內容。 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.HI要使用單選按鈕中的數據。 因此,如果用戶選擇

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

在第二頁上將是:

ErrorCorrection = ErrorCorrectionLevel.Q

你知道我該怎么做嗎?

因此,傳遞任何類型的對象(包括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"]

如果只想傳遞變量,則可以使用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);

如果您想發送更復雜的對象,可以像這里一樣進行操作-您可以編寫擴展名:

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;

您也可以在這里閱讀更多內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM