2015-05-11 20 views
1

我正在開發使用第三方DrawerLayout的Windows Phone App 8.1。 我的問題是當我使用draweralayout設計頁面時。我需要將相同的佈局傳遞給我的應用程序中的其他頁面。但是當我使用加粗 Frame.navigate(typeof(Page1));該應用程序崩潰。 我無法調試該問題。 請幫忙!!!在windows phone 8.1中使用抽屜佈局時的幀導航問題

我的XAML代碼是

<Grid x:Name="Rootlayout"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"/> 
     <RowDefinition Height="*"/> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="Auto"/> 
     <ColumnDefinition Width="*"/> 
    </Grid.ColumnDefinitions> 
    <!-- title bar--> 
    <Grid x:Name="TitleBar" Height="50" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" Background="SkyBlue"> 
     <Image Source="/Assets/fs-logo.png" Height="50" HorizontalAlignment="Left" Margin="10,0,0,0"/> 
    </Grid> 
    <!--Drawer Layout--> 
    <drawer:DrawerLayout x:Name="DrawerLayout" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2"> 
     <!--Main Layout--> 
     <Grid x:Name="mainLayout" Background="White" > 
      <ScrollViewer> 
       <Grid Margin="5"> 
        <Grid.RowDefinitions> 
         <RowDefinition Height="*"/> 
         <RowDefinition Height="*"/> 
        </Grid.RowDefinitions> 
        <Image x:Name="mainImage" Grid.Row="0" Source="{Binding }"></Image> 
        <GridView x:Name="grdView" Grid.Row="1" ItemsSource="{Binding }" ItemContainerStyle="{StaticResource GridViewItemContainer}" Tapped="grdView_Tapped" > 
         <GridView.ItemTemplate> 
          <DataTemplate> 
           <Border BorderThickness="1" BorderBrush="Blue"> 
           <Grid Height="{Binding Size}" 
             Width="{Binding Size}"> 
            <Image Height="120" Width="150" Source="{Binding sImageURL}" Stretch="Fill"> 

            </Image> 
            <TextBlock Text="{Binding Name}" HorizontalAlignment="Center" VerticalAlignment="Bottom" Foreground="Black" FontSize="18"/> 
           </Grid> 
           </Border> 
          </DataTemplate> 
         </GridView.ItemTemplate> 
         <GridView.ItemsPanel> 
          <ItemsPanelTemplate> 
           <ItemsWrapGrid Orientation="Horizontal"></ItemsWrapGrid> 
          </ItemsPanelTemplate> 
         </GridView.ItemsPanel> 
        </GridView> 
       </Grid> 
      </ScrollViewer> 
     </Grid> 
     <!--Drawer Layout--> 
     <Grid x:Name="listFragment" FlowDirection="LeftToRight" > 
      <ListView x:Name="listItem" SelectionChanged="listItem_SelectionChanged"> 
       <ListView.ItemTemplate> 
        <DataTemplate> 
         <TextBlock Text="{Binding}" FontSize="20" Foreground="Black" Margin="10" VerticalAlignment="Center" HorizontalAlignment="Left"/>  
        </DataTemplate> 
       </ListView.ItemTemplate> 
      </ListView> 
     </Grid> 
    </drawer:DrawerLayout> 
    <Image Grid.Row="1" Grid.Column="0" Height="50" Width="50" Source="/Assets/appbar.right.png" Tapped="chevlon_Tapped"> 

    </Image> 
</Grid> 

我MainPage.cs

public sealed partial class MainPage : Page 
{ 
    private NavigationHelper navigationHelper; 
    private ObservableDictionary defaultViewModel = new ObservableDictionary(); 

    List<Services.Company> companyData; 
    List<Services.Category> categoryData; 

    public MainPage() 
    { 
     this.InitializeComponent(); 

     Loaded += MainPage_Loaded; 
     this.navigationHelper = new NavigationHelper(this); 
     this.navigationHelper.LoadState += this.NavigationHelper_LoadState; 
     this.navigationHelper.SaveState += this.NavigationHelper_SaveState; 
     this.NavigationCacheMode = NavigationCacheMode.Required; 
    } 

    async void MainPage_Loaded(object sender, RoutedEventArgs e) 
    { 
     await Windows.UI.ViewManagement.StatusBar.GetForCurrentView().HideAsync(); 


     // SetGridViewItemSize();   
      //throw new NotImplementedException(); 
    } 
    /// <summary> 
    /// Gets the <see cref="NavigationHelper"/> associated with this <see cref="Page"/>. 
    /// </summary> 
    public NavigationHelper NavigationHelper 
    { 
     get { return this.navigationHelper; } 
    } 

    /// <summary> 
    /// Gets the view model for this <see cref="Page"/>. 
    /// This can be changed to a strongly typed view model. 
    /// </summary> 
    public ObservableDictionary DefaultViewModel 
    { 
     get { return this.defaultViewModel; } 
    } 

    private async void NavigationHelper_LoadState(object sender, LoadStateEventArgs e) 
    { 
     if (e.PageState != null) 
     { 

      // this.DefaultViewModel["MainPage"] = (MainPage)e.PageState["MainPage"]; 
      // Restore scroll offset 
      var index = (int)e.PageState["FirstVisibleItemIndex"]; 
      var container = grdView.ContainerFromIndex(index); 
      grdView.ScrollIntoView(container); 



     } 
     else 
     { 
      //Load Data for the First time 

      DrawerLayout.InitializeDrawerLayout(); 
      var company = await Services.CompCatInfo.GetCompaniesAsync(App.compId); 
      companyData = company.ToList(); 
      if (companyData.Count > 0) 
      { 
       mainImage.Source = new BitmapImage(new Uri(companyData.ElementAt(0).LogoImgUrl, UriKind.Absolute)); 
      } 
      var category = await Services.CompCatInfo.GetCategoriesAsync(App.compId); 
      categoryData = category.ToList(); 

      SetGridViewItemSize(); 
      if (categoryData.Count > 0) 
      { 
       grdView.ItemsSource = categoryData; 
      } 


     } 

    } 
    private void NavigationHelper_SaveState(object sender, SaveStateEventArgs e) 
    { 
     var isp = (ItemsWrapGrid)grdView.ItemsPanelRoot; 
     int firstVisibleItem = isp.FirstVisibleIndex; 
     e.PageState["FirstVisibleItemIndex"] = firstVisibleItem; 
     // e.PageState["HomePage"] = this.Frame.CurrentSourcePageType; 
     // This must be serializable according to the SuspensionManager 
     //e.PageState["MainPage"] = this.DefaultViewModel["MainPage"]; 
    } 
    private void SetGridViewItemSize() 
    { 
     //var viewModel = (DataContext as ViewModel1); 

     var width = Math.Truncate((grdView.ActualWidth - 10)); 
     var height = Math.Truncate((grdView.ActualHeight - 10)); 

     // total left + right margin for each tile (ItemContainerStyle) 
     var margin = 10; 
     var twoColumnGridPortrait = (width/2) - margin; 
    // var threeColumnGridPortrait = (width/3) - margin; 
     var twoColumnGridLandscape = (height/2) - margin; 
     var threeColumnGridLandscape = (height/3) - margin; 
     double portrait; 
     double landscape; 
     portrait = twoColumnGridPortrait; 
     landscape = threeColumnGridLandscape; 

     Application.Current.Resources["GridViewItemPortrait"] = Math.Round(portrait, 2); 
     Application.Current.Resources["GridViewItemLandscape"] = Math.Round(landscape, 2); 

     if (DisplayInformation.GetForCurrentView().CurrentOrientation == DisplayOrientations.Portrait) 
     { 
      Measure((double)Application.Current.Resources["GridViewItemPortrait"]); 
     } 
     else 
     { 
      Measure((double)Application.Current.Resources["GridViewItemLandscape"]); 
     } 
     // throw new NotImplementedException(); 
    } 

    private void Measure(double size) 
    { 
     for (int i = 0; i < categoryData.Count; i++) 
     { 
      var item = categoryData[i]; 
      item.Size = size; 
     } 
     //throw new NotImplementedException(); 
    } 
    #region NavigationHelper registration 
    /// <summary> 
    /// Invoked when this page is about to be displayed in a Frame. 
    /// </summary> 
    /// <param name="e">Event data that describes how this page was reached. 
    /// This parameter is typically used to configure the page.</param> 
    protected override void OnNavigatedTo(NavigationEventArgs e) 
    { 
     this.navigationHelper.OnNavigatedTo(e); 




     //if (e.NavigationMode == NavigationMode.New) 
     //{ 
     // DrawerLayout.InitializeDrawerLayout(); 
     // var company = await Services.CompCatInfo.GetCompaniesAsync(App.compId); 
     // companyData = company.ToList(); 
     // if (companyData.Count > 0) 
     // { 
     //  mainImage.Source = new BitmapImage(new Uri(companyData.ElementAt(0).LogoImgUrl, UriKind.Absolute)); 
     // } 
     // var category = await Services.CompCatInfo.GetCategoriesAsync(App.compId); 
     // categoryData = category.ToList(); 
     // SetGridViewItemSize(); 
     // grdView.ItemsSource = categoryData; 
     //} 
     //else 
     //{ 
     // // this.Frame.Content = e.Content; 

     //} 
     // TODO: Prepare page for display here. 

     // TODO: If your application contains multiple pages, ensure that you are 
     // handling the hardware Back button by registering for the 
     // Windows.Phone.UI.Input.HardwareButtons.BackPressed event. 
     // If you are using the NavigationHelper provided by some templates, 
     // this event is handled for you. 
    } 

    protected override void OnNavigatedFrom(NavigationEventArgs e) 
    { 
     this.navigationHelper.OnNavigatedFrom(e); 
    } 

    #endregion 

    private void chevlon_Tapped(object sender, TappedRoutedEventArgs e) 
    { 
     string[] list = new string[] { "Services","Solutions","Technologies","About Us"}; 
     listItem.ItemsSource = list.ToList(); 
     if(DrawerLayout.IsDrawerOpen) 
     { 
      DrawerLayout.CloseDrawer(); 
     } 
     else 
     { 
      DrawerLayout.OpenDrawer(); 
     } 
    } 

    private void listItem_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     if (listItem.SelectedItem != null) 
     { 
      var selecteditem = listItem.SelectedValue as string; 
      // DetailsTxtBlck.Text = "SelectedItem is: " + selecteditem; 
      DrawerLayout.CloseDrawer(); 
      listItem.SelectedItem = null; 
     } 

    } 

    private void grdView_Tapped(object sender, TappedRoutedEventArgs e) 
    { 
     DrawerLayout.CloseDrawer(); 
     if(e.OriginalSource.GetType().ToString()=="Windows.UI.Xaml.Controls.Image"||e.OriginalSource.GetType().ToString()=="Windows.UI.Xaml.Controls.TextBlock") 
     { 
      var catObj = (sender as GridView).SelectedItem as Services.Category; 
      ////Frame rootFrame = Window.Current.Content as Frame; 
      ////rootFrame.Navigate(typeof(Page2),catObj); 
      Frame.Navigate(typeof(Page2),catObj); 

     } 

    } 
} 
+0

我也有同樣的問題,導航,但沒有得到解決任何地方。 我知道在DrawerLayout演示中,onNavigatedTo沒有被調用。因此Frame是空的。 – karan

回答

0


您可以通過這種方式

CoreDispatcher dispatcher = CoreWindow.GetForCurrentThread().Dispatcher; 
        await dispatcher.RunAsync(CoreDispatcherPriority.Normal,() => 
        { 
         (Window.Current.Content as Frame).Navigate(typeof(Page name)); 

        });