lundi 11 mai 2015

Frame navigation issue while using Drawer Layout in windows phone 8.1

I am developing a Windows Phone App 8.1 using a third party DrawerLayout. My problem is when I have design the page using draweralayout. I need to pass the same layout to each other pages in my application. but when i use bold Frame.navigate(typeof(Page1)); the application is crashed. I am unable to debug the problem. please help!!!

My Xaml Code is

<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>

My 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);

        }

    }
}

Aucun commentaire:

Enregistrer un commentaire