2011-12-10 64 views
1
using System.Windows; 
using System.Windows.Input; 
using System.Windows.Navigation; 
using Microsoft.Phone.Controls; 
using WindowsPhoneApp; // For the Setting class 
namespace Tally 
{ 
public partial class MainPage : PhoneApplicationPage 
{ 
int count = 0; 
// Remember what the user typed, for future app activations or launches 
Setting<int> savedCount = new Setting<int>(「SavedCount」, 0); 
public MainPage() 
{ 
InitializeComponent(); 
} 
// Handle a tap anywhere on the page (other than the Button) 
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) 
{ 
base.OnMouseLeftButtonDown(e); 
this.count++; 
this.CountTextBlock.Text = this.count.ToString(「N0」); 
} 
// Handle a tap on the button 
void ResetButton_Click(object sender, RoutedEventArgs e) 
{ 
this.count = 0; 
this.CountTextBlock.Text = this.count.ToString(「N0」); 
} 
protected override void OnNavigatedFrom(NavigationEventArgs e) 
{ 
base.OnNavigatedFrom(e); 
// Persist state when leaving for any reason (Deactivated or Closing) 
this.savedCount.Value = this.count; 
} 
protected override void OnNavigatedTo(NavigationEventArgs e) 
{ 
base.OnNavigatedTo(e); 
// Restore persisted state 
this.count = this.savedCount.Value; 
this.CountTextBlock.Text = this.count.ToString(「N0」); 
} 
} 
} 

我不是C#編碼器..我使用VB.net ...無論如何,我嘗試使用在線轉換工具將其轉換...但vb代碼充滿了錯誤。任何人都可以幫助我呢?我剛開始學習Windows Phone 7.VB.Net命名空間等效於C#

什麼名字空間應該在VB中導入using WindowsPhoneApp; ??

+0

WindowsPhoneApp命名空間在C#中不存在。你在哪裏找到這個代碼?你可能錯過了一些東西。 –

回答

1

http://forums.create.msdn.com/forums/p/82711/514488.aspx

第1章應用程序(如幾乎每一個在這本書的應用程序)使用Settings類與獨立存儲進行交互。這樣,它可以在下次運行應用程序時記住值。在本書的代碼下載中,該項目包含必要的Settings.cs類,這個錯誤消失了。當討論隔離存儲的話題時,這個類的代碼也包含在第20章的書中。從第1章代碼下載

1.複印Settings.cs,包括在您的項目:

所以,你有兩個選擇。

2.在您的項目中創建一個新的Settings.cs文件並鍵入第20章的Settings.cs代碼。 第1章中有一個要點試圖解釋這種情況,但我意識到它是太混亂了。

1

試試這個在線converter

我試圖轉換器,這是轉換結果:

Imports System.Windows 
Imports System.Windows.Input 
Imports System.Windows.Navigation 
Imports Microsoft.Phone.Controls 
Imports WindowsPhoneApp 
' For the Setting class 
Namespace Tally 
    Public Partial Class MainPage 
     Inherits PhoneApplicationPage 
     Private count As Integer = 0 
     ' Remember what the user typed, for future app activations or launches 
     Private savedCount As New Setting(Of Integer)(SavedCount, 0) 
     Public Sub New() 
      InitializeComponent() 
     End Sub 
     ' Handle a tap anywhere on the page (other than the Button) 
     Protected Overrides Sub OnMouseLeftButtonDown(e As MouseButtonEventArgs) 
      MyBase.OnMouseLeftButtonDown(e) 
      Me.count += 1 
      Me.CountTextBlock.Text = Me.count.ToString(N0) 
     End Sub 
     ' Handle a tap on the button 
     Private Sub ResetButton_Click(sender As Object, e As RoutedEventArgs) 
      Me.count = 0 
      Me.CountTextBlock.Text = Me.count.ToString(N0) 
     End Sub 
     Protected Overrides Sub OnNavigatedFrom(e As NavigationEventArgs) 
      MyBase.OnNavigatedFrom(e) 
      ' Persist state when leaving for any reason (Deactivated or Closing) 
      Me.savedCount.Value = Me.count 
     End Sub 
     Protected Overrides Sub OnNavigatedTo(e As NavigationEventArgs) 
      MyBase.OnNavigatedTo(e) 
      ' Restore persisted state 
      Me.count = Me.savedCount.Value 
      Me.CountTextBlock.Text = Me.count.ToString(N0) 
     End Sub 
    End Class 
End Namespace