2014-01-14 42 views
0

我想從特定列表中導航到另一個頁面,但此時無論點擊哪個列表,它仍會進入同一頁面。我該怎麼辦。我在這裏還是新手,並且瞭解更多。謝謝。 以下是代碼。導航到不同的頁面,具體取決於windows phone 8中的項目列表

MainPage.xaml中

<!--Pivot Control--> 
    <phone:Pivot Title="DAILY ROUTINE"> 
     <!--Pivot item one--> 
     <phone:PivotItem Header="activity"> 
      <!--Double line list with text wrapping--> 
      <phone:LongListSelector x:Name="MainLongListSelector" Margin="0,0,-12,0" ItemsSource="{Binding Items}" SelectionChanged="LongListSelector_SelectionChanged"> 
       <phone:LongListSelector.ItemTemplate> 
        <DataTemplate> 
         <StackPanel Margin="0,0,0,17"> 
          <TextBlock Text="{Binding LineOne}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/> 
         </StackPanel> 
        </DataTemplate> 
       </phone:LongListSelector.ItemTemplate> 
      </phone:LongListSelector> 
     </phone:PivotItem> 

     <!--Pivot item two--> 
     <phone:PivotItem Header="today"> 

     </phone:PivotItem> 
    </phone:Pivot> 

MainPage.xaml.cs中

namespace PivotApp3 
{ 
    public partial class MainPage : PhoneApplicationPage 
    { 
    // Constructor 
    public MainPage() 
    { 
     InitializeComponent(); 

     // Set the data context of the listbox control to the sample data 
     DataContext = App.ViewModel; 

     // Sample code to localize the ApplicationBar 
     //BuildLocalizedApplicationBar(); 
    } 

    // Load data for the ViewModel Items 
    protected override void OnNavigatedTo(NavigationEventArgs e) 
    { 
     if (!App.ViewModel.IsDataLoaded) 
     { 
      App.ViewModel.LoadData(); 
     } 
    } 

    private void LongListSelector_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     NavigationService.Navigate(new Uri("/todolistPage.xaml", UriKind.Relative)); 
    } 

} 

}

todolistPage.xaml

<Grid x:Name="LayoutRoot" Background="Transparent"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"/> 
     <RowDefinition Height="*"/> 
    </Grid.RowDefinitions> 

    <!--TitlePanel contains the name of the application and page title--> 
    <StackPanel Grid.Row="0" Margin="12,17,0,28"> 
     <TextBlock Text="TO DO LIST" Style="{StaticResource PhoneTextNormalStyle}"/> 
     <TextBlock Text="add" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> 
    </StackPanel> 

    <!--ContentPanel - place additional content here--> 
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 

    </Grid> 
</Grid> 

MainViewModel.cs

namespace PivotApp3.ViewModels 
{ 
public class MainViewModel : INotifyPropertyChanged 
{ 
    public MainViewModel() 
    { 
     this.Items = new ObservableCollection<ItemViewModel>(); 
    } 

    /// <summary> 
    /// A collection for ItemViewModel objects. 
    /// </summary> 
    public ObservableCollection<ItemViewModel> Items { get; private set; } 

    private string _sampleProperty = "Sample Runtime Property Value"; 
    /// <summary> 
    /// Sample ViewModel property; this property is used in the view to display its value using a Binding 
    /// </summary> 
    /// <returns></returns> 
    public string SampleProperty 
    { 
     get 
     { 
      return _sampleProperty; 
     } 
     set 
     { 
      if (value != _sampleProperty) 
      { 
       _sampleProperty = value; 
       NotifyPropertyChanged("SampleProperty"); 
      } 
     } 
    } 

    /// <summary> 
    /// Sample property that returns a localized string 
    /// </summary> 
    public string LocalizedSampleProperty 
    { 
     get 
     { 
      return AppResources.SampleProperty; 
     } 
    } 

    public bool IsDataLoaded 
    { 
     get; 
     private set; 
    } 

    /// <summary> 
    /// Creates and adds a few ItemViewModel objects into the Items collection. 
    /// </summary> 
    public void LoadData() 
    { 
     // Sample data; replace with real data 
     this.Items.Add(new ItemViewModel() { LineOne = "+ To Do List" }); 
     this.Items.Add(new ItemViewModel() { LineOne = "+ Reminder" }); 
     this.Items.Add(new ItemViewModel() { LineOne = "+ Expenses" }); 

     this.IsDataLoaded = true; 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    private void NotifyPropertyChanged(String propertyName) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (null != handler) 
     { 
      handler(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 

}

ItemViewModel.cs

using System; 
using System.ComponentModel; 
using System.Diagnostics; 
using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 

namespace PivotApp3.ViewModels 
{ 
public class ItemViewModel : INotifyPropertyChanged 
{ 
    private string _lineOne; 
    /// <summary> 
    /// Sample ViewModel property; this property is used in the view to display its value using a Binding. 
    /// </summary> 
    /// <returns></returns> 
    public string LineOne 
    { 
     get 
     { 
      return _lineOne; 
     } 
     set 
     { 
      if (value != _lineOne) 
      { 
       _lineOne = value; 
       NotifyPropertyChanged("LineOne"); 
      } 
     } 
    } 

    private string _lineTwo; 
    /// <summary> 
    /// Sample ViewModel property; this property is used in the view to display its value using a Binding. 
    /// </summary> 
    /// <returns></returns> 
    public string LineTwo 
    { 
     get 
     { 
      return _lineTwo; 
     } 
     set 
     { 
      if (value != _lineTwo) 
      { 
       _lineTwo = value; 
       NotifyPropertyChanged("LineTwo"); 
      } 
     } 
    } 

    private string _lineThree; 
    /// <summary> 
    /// Sample ViewModel property; this property is used in the view to display its value using a Binding. 
    /// </summary> 
    /// <returns></returns> 
    public string LineThree 
    { 
     get 
     { 
      return _lineThree; 
     } 
     set 
     { 
      if (value != _lineThree) 
      { 
       _lineThree = value; 
       NotifyPropertyChanged("LineThree"); 
      } 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    private void NotifyPropertyChanged(String propertyName) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (null != handler) 
     { 
      handler(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 

}

+0

你可能想操縱竊聽項目的的datacontext在列表中。 [閱讀](http://stackoverflow.com/a/21089912/1028868) – Pantelis

+0

嗨@Pantelis,老實說,我不確定datacontext在這裏意味着什麼,因爲這對我來說仍然是新的。 –

+0

[DataContext的(http://www.codeproject.com/Articles/321899/DataContext-in-WPF) – Pantelis

回答

3

最平凡的解決方案是:

private void LongListSelector_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    var si = MainLongListSelector.SelectedItem as PivotApp3.ViewModels.ItemViewModel; 

    if(si.LineOne.Equals("+ To Do List")) 
     NavigationService.Navigate(new Uri("/todolistPage.xaml", UriKind.Relative)); 
    else if(si.LineOne.Equals("another")) 
     NavigationService.Navigate(new Uri("/another.xaml", UriKind.Relative)); 
} 

您不必使用ItemViewModel綁定一些數據到列表中。如果您將使用自己的課程 - 比您可以將URI「放在」您的項目後面並使用它。 (這將是更好的解決方案)。

例子:

public void LoadData() 
{ 
    // Sample data; replace with real data 
    this.Items.Add(new ItemViewModel() { LineOne = "+ To Do List", GotoUri = new Uri("/todolistPage.xaml", UriKind.Relative) }); 
    this.Items.Add(new ItemViewModel() { LineOne = "+ Reminder", GotoUri = new Uri("/other.xaml", UriKind.Relative) }); 
    this.Items.Add(new ItemViewModel() { LineOne = "+ Expenses", GotoUri = new Uri("/other2.xaml", UriKind.Relative) }); 

    this.IsDataLoaded = true; 
} 

然後:

public class ItemViewModel : INotifyPropertyChanged 
{ 
    public Uri GotoUri {get; set;} 
    //... 
} 

private void LongListSelector_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    var si = MainLongListSelector.SelectedItem as PivotApp3.ViewModels.ItemViewModel; 
     if(si != null) 
     NavigationService.Navigate(si.GotoUri, UriKind.Relative)); 
} 
+0

嗨,關於SelectedIndex,從哪裏來?謝謝 –

+0

對不起,我的錯誤。 Just LongListSelector沒有SelectedIndex。在這種情況下,您必須使用SelectedItem,但它不那麼優雅... – Rayet

+0

我在此代碼下方顯示紅色下劃線LongListSelector.SelectedItem作爲ItemViewModel;它表示找不到ItemViewModel,並且LongListSelector.SelectedItem需要非靜態字段,方法或屬性的對象引用。 –

相關問題