2010-12-14 52 views
1

我一直在閱讀其他問題和頁面,並看到一些想法,但無法理解它們或使它們像初學者一樣正常工作,所以請和我一起裸照。WP7 - 將頁面複選框值傳遞給其他類

我的例子:

我有這個checkBox1我MainPage.xaml中

<CheckBox Content="Central WC/EC" Height="68" HorizontalAlignment="Left" Margin="106,206,0,0" Name="checkBox1" VerticalAlignment="Top" BorderThickness="0" /> 

我有anotherpage.xaml.cs其C#中的anotherpage.xaml:

public void Feed(object Sender, DownloadStringCompletedEventArgs e) 
    { 
     if (checkBox1.Checked("SE" == (_item.Sector))) ; 
     { 

     } 
    } 

如何將mainpage.xaml上的checkBox1的值傳遞給anotherpage.xaml.cs

非常感謝。如果您需要更多信息,請與我們聯繫。

+0

http://stackoverflow.com/questions/4143383/wp7-pass-parameter-to-new-page檢查出來;) – Machinarius 2010-12-14 20:50:05

+0

'Checked'是CheckBox上的事件。你可能想要'IsChecked'屬性來判斷用戶是否選中了該框。 – 2010-12-15 11:55:49

回答

1

你可以通過打開下一個頁面時,該複選框是否被選中:

NavigationService.Navigate(new Uri("/AnotherPage.xaml?chkd=" + checkBox1.IsChecked, UriKind.Relative)); 

然後,您可以查詢這個在OnNavigatedTo事件中的「其他」頁面上:

protected override void OnNavigatedTo(NavigationEventArgs e) 
{ 
    string isChecked; 
    if (NavigationContext.QueryString.TryGetValue("chkd", out isChecked)) 
    { 
     if (bool.Parse(isChecked)) 
     { 
      // 
     } 
    } 
} 

編輯:
要傳遞多個值,只需將它們添加到查詢字符串:

NavigationService.Navigate(new Uri("/AnotherPage.xaml?chk1=" + checkBox1.IsChecked + "&chk2=" + checkBox2.IsChecked, UriKind.Relative)); 

(你可能會想,雖然一個好一點格式化代碼)

然後,您可以從

protected override void OnNavigatedTo(NavigationEventArgs e) 
{ 
    string is1Checked; 
    if (NavigationContext.QueryString.TryGetValue("chk1", out is1Checked)) 
    { 
     if (bool.Parse(is1Checked)) 
     { 
      // 
     } 
    } 

    string is2Checked; 
    if (NavigationContext.QueryString.TryGetValue("chk2", out is2Checked)) 
    { 
     if (bool.Parse(is2Checked)) 
     { 
      // 
     } 
    } 
} 

得到各參數反過來當你要傳遞更多價值這會因大量重複的代碼而變得混亂。而不是傳遞多個值individualy你可以將它們連接起來一起:

​​

然後,您可以分割字符串並單獨解析部分。

+0

嗨馬特,這似乎是一個有趣的解決方案。我可以用它來傳遞多個複選框嗎?我總共有8個。謝謝。 – 2010-12-15 11:45:31

+0

@丹更新了答案,以包含處理多個複選框的兩種方法。 – 2010-12-15 11:54:32

+0

謝謝你完美! – 2010-12-15 14:58:45

0

您可以在App類中聲明一個公共屬性。

public partial class App : Application 
{ 
    public int Shared { set; get; } 
    //... 
} 

然後你可以從頁面通過訪問:

(Application.Current as App).Shared 

你可以存儲到窗體的引用或把一個事件或者其他任何你想做的事情。

在旁註中,我強烈建議Petzold's WP7 book free for download

+0

謝謝,我會研究一下。 – 2010-12-15 09:26:17

相關問題