2012-08-13 48 views
1

我從windows phone工具包中具有以下HubTile控件的實現,並且除了從選定的圖塊實現點擊事件外,一切都正常工作。我不確定如何將特定圖塊的tap連接到代碼後面的事件處理程序(它應該簡單地導航到我的項目中的新頁面)。我至今如下:如何將點擊事件添加到HubTile

MainPage.xaml中

<ListBox Grid.Row="0" x:Name="tileList" toolkit:TiltEffect.IsTiltEnabled="True"> 
       <ListBox.ItemsPanel> 
        <ItemsPanelTemplate> 
         <toolkit:WrapPanel Orientation="Horizontal" /> 
        </ItemsPanelTemplate> 
       </ListBox.ItemsPanel> 
       <ListBox.ItemTemplate> 
        <DataTemplate> 
         <toolkit:HubTile Title="{Binding Title}" Margin="3" 
             Notification="{Binding Notification}" 
             DisplayNotification="{Binding DisplayNotification}" 
             Message="{Binding Message}" 
             GroupTag="{Binding GroupTag}" 
             Source="{Binding ImageUri}" 
             Tap="hubTile_Tap"> 
         </toolkit:HubTile> 

        </DataTemplate> 
       </ListBox.ItemTemplate> 
      </ListBox> 

TileItem.cs

public class TileItem 
{ 
    public string ImageUri 
    { 
     get; 
     set; 
    } 

    public string Title 
    { 
     get; 
     set; 
    } 

    public string Notification 
    { 
     get; 
     set; 
    } 

    public bool DisplayNotification 
    { 
     get 
     { 
      return !string.IsNullOrEmpty(this.Notification); 
     } 
    } 

    public string Message 
    { 
     get; 
     set; 
    } 

    public string GroupTag 
    { 
     get; 
     set; 
    } 

    //not sure how to implement this? 
    public string Tap 
    { 
     get; 
     set; 
    } 
} 

MainPage.xaml.cs中

#region Ctr 

    public MainPage() 
    { 
     InitializeComponent(); 

     CreateHubTiles(); 
    } 

    #endregion 

    private void CreateHubTiles() 
    { 
     List<TileItem> tileItems = new List<TileItem>() 
     { 
      //there will be at least 2 distinct tiles linking to seperate pages 
      new TileItem() { ImageUri = "/Images/shareStatusImage.jpg", Title = "status", /*Notification = "last shared link uri",*/ Message = "last shared status message", GroupTag = "TileGroup", Tap = "shareStatus_Tap" }, 
      new TileItem() { ImageUri = "/Images/shareLinkImage.jpg", Title = "link", /*Notification = "last shared status message",*/ Message = "last shared link uri", GroupTag = "TileGroup", Tap = "shareLink_Tap" } 
     }; 

     this.tileList.ItemsSource = tileItems; 
    } 

    #region Navigation 

    protected override void OnNavigatedTo(NavigationEventArgs e) 
    { 
     base.OnNavigatedTo(e); 

     HubTileService.UnfreezeGroup("TileGroup"); 
    } 

    protected override void OnNavigatedFrom(NavigationEventArgs e) 
    { 
     base.OnNavigatedFrom(e); 

     HubTileService.FreezeGroup("TileGroup"); 
    } 

    #endregion 

    #region Event Handlers 

    private void hubTile_Tap(object sender, System.Windows.Input.GestureEventArgs e) 
    { 
     //vibrate 
     if (Settings.EnableVibration.Value) 
     { 
      VibrateController.Default.Start(TimeSpan.FromMilliseconds(40)); 
     } 

     TileItem tap = sender as TileItem; 
     string _tap = tap.Tap.ToString(); //NullReferenceException occurs here 

     switch(_tap) 
     { 
      case "shareStatus_Tap": 
       this.NavigationService.Navigate(new Uri("/Views/ShareStatusPage.xaml", UriKind.Relative)); 
       break; 
      case "shareLink_Tap": 
       this.NavigationService.Navigate(new Uri("/Views/ShareLinkPage.xaml", UriKind.Relative)); 
       break; 
     } 
    } 
    #endregion 

所以我試圖爲每個圖塊創建一個tap屬性,然後當點擊一個圖塊時,事件處理程序決定哪個圖塊是被點擊的圖塊並導航到相應的新頁面。由於某些原因,Tap屬性爲空,但?該項目在模擬器中加載,但無法啓動磁貼,並且在我的實際設備上,應用程序根本無法加載?

回答

1

您不需要在xaml中綁定tap屬性,而是創建一個tap處理程序並將該處理程序的名稱提供給tap屬性。

+0

我有我的解決方案更新到我目前有,但我得到的在我的事件處理程序中的NullReferenceException? – Matthew 2012-08-14 17:42:43

1

改變抽頭事件處理程序,爲正確執行以下操作:因爲,也許,你的TileItem實例點擊屬性未設置

private void hubTile_Tap(object sender, System.Windows.Input.GestureEventArgs e) 
    { 
     //vibrate 
     if (Settings.EnableVibration.Value) 
     { 
      VibrateController.Default.Start(TimeSpan.FromMilliseconds(40)); 
     } 

     //TileItem tap = sender as TileItem; 
     HubTile tap = sender as HubTile; 
     string _tap = tap.Title.ToString(); //NullReferenceException occurs here 

     switch(_tap) 
     { 
      //case "shareStatus_Tap": 
      case "status": 
       this.NavigationService.Navigate(new Uri("/Views/ShareStatusPage.xaml", UriKind.Relative)); 
       break; 
      //case "shareLink_Tap": 
      case "link": 
       this.NavigationService.Navigate(new Uri("/Views/ShareLinkPage.xaml", UriKind.Relative)); 
       break; 
     } 
    } 
1

你得到的NullReferenceException 。您正在設置的是點按事件處理程序HubTile控件。

現在,爲了得到你想要的行爲,我建議如下:

  1. 而不是在TileItem一個點擊財產,你應該添加一個NavigationUri財產。
  2. 在爲自來水事件的的HubTile事件處理程序,而不是檢查哪個瓦片被竊聽,你可以直接導航到使用NavigationUri

下面是它如何工作的相應的視圖:

public class TileItem 
{ 
    // previous properties (except Tap, as you don't need it) 
    ... 

    public string NavigationUri {get; set;} 
} 

每個TileItem,給它的相應NavigationUri

var shareStatusPage= new TileItem 
        { 
         Title="Share Status", 
         ImageUri="ShareStatus.png", 
         NavigationUri="/Views/ShareStatusPage.xaml", 
         //rest of properties 
        }; 
var shareLinkPage= new TileItem 
        { 
         Title="Share Link", 
         ImageUri="ShareLink.png", 
         NavigationUri="/Views/ShareLinkPage.xaml", 
         //rest of properties 
        }; 

與簡易點擊事件處理程序變爲:

private void hubTile_Tap(object sender, System.Windows.Input.GestureEventArgs e) 
{ 
    //vibrate 
    if (Settings.EnableVibration.Value) 
    { 
     VibrateController.Default.Start(TimeSpan.FromMilliseconds(40)); 
    } 

    TileItem item = sender as TileItem; 
    this.NavigationService.Navigate(new Uri(item.NavigationUri, UriKind.Relative)); 
} 

希望這有助於:)

相關問題