-1

I would like to know why creating another instance of Grid is making my app crash.

It is a basic RSS Feed reader app. The app opens fine, I can see the MainPage, and the DrawerLayout object works, but when clicking an item (article) (which then leads to AppDetailPage) the app crashes.

Here is the code :

------------------------------MAIN PAGE---------------------------------------

<Page
    xmlns:drawerLayout="using:DrawerLayout"  
    x:Class="AppStudio.Views.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:AppStudio.Views"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:vm="using:AppStudio.ViewModels"
    mc:Ignorable="d">

    <Grid x:Name="RootLayout" Margin="0,-26.667,0,-0.333">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <!-- Title Bar -->
        <Grid x:Name="TitleBar" Background="#ffffff" Grid.Row ="0" Height="60" >
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Image Margin="0,8,0,0"  x:Name="DrawerIcon"  Grid.Column="0" Source="/Assets/kaltert.png" HorizontalAlignment="Left" Tapped="DrawerIcon_Tapped" />
            <Image Margin="0,2,55,2"  x:Name="Logo"  Grid.Column="1" Source="/Assets/tmheaderkaltert.png" HorizontalAlignment="Center"  />
        </Grid>
        <drawerLayout:DrawerLayout Grid.Row="1" x:Name="DrawerLayout">
            <Grid x:Name="MainFragment" Background="#ffffff">
                <Hub x:Name="Container"  Margin="0,28,0,0" Background="{StaticResource AppBackground}" DataContext="{Binding}" SectionsInViewChanged="OnSectionsInViewChanged">
                    <HubSection x:Name="LajmetSection" Padding="0,-30,20,0" Width="400" DataContext="{Binding MainViewModel.LajmetModel}"
                        d:DataContext="{d:DesignData Source=/Assets/Data/LajmetDataSource.json, Type=vm:LajmetViewModel, IsDesignTimeCreatable=true}"
                        ContentTemplate="{StaticResource LajmetList}" IsHeaderInteractive="{Binding HasMoreItems}" />
                </Hub>
            </Grid>
-----------ANY GRID I ADD AFTER THIS LINE CRASHES THE APP LIKE THE "LISTFRAGMENT FOR EXAMPLE"--------------------------------------
            <Grid x:Name="ListFragment" Background="#141e28">
                <ListView x:Name="ListMenuItems">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding}" Margin="10" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="23" Foreground="White" />
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </Grid>
-----------------------------------------------------------------------------------------------------------------------------------
        </drawerLayout:DrawerLayout>
    </Grid>
</Page>






_____________________________________________________       ________________________________________________________________
_____________________________________________________C# Code here:__________________________________________________________

using System;
using System.Linq;
using System.Net.NetworkInformation;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Popups;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
using Windows.ApplicationModel.DataTransfer;
using Windows.ApplicationModel.Background;
using AppStudio.Services;
using AppStudio.ViewModels;


namespace AppStudio.Views
{

    public sealed partial class MainPage : Page
    {

        private MainViewModel _mainViewModel = null;

        private NavigationHelper _navigationHelper;

        private DataTransferManager _dataTransferManager;

        public MainPage()
        {
            this.NavigationCacheMode = NavigationCacheMode.Required;

            this.InitializeComponent();

//drawerlayout

            DrawerLayout.InitializeDrawerLayout();

            string[] menuItems = new string[5] { "Item1", "Item2", "Item3", "Item4", "Item5" };
      //      ListMenuItems.ItemsSource = menuItems.ToList();


            this.NavigationCacheMode = NavigationCacheMode.Required;
            _navigationHelper = new NavigationHelper(this);

            _mainViewModel = _mainViewModel ?? new MainViewModel();

            DataContext = this;

            ApplicationView.GetForCurrentView().
                SetDesiredBoundsMode(ApplicationViewBoundsMode.UseVisible);
        }


        public MainViewModel MainViewModel
        {
            get { return _mainViewModel; }
        }

        private void DrawerIcon_Tapped(object sender, TappedRoutedEventArgs e)
        {
            if (DrawerLayout.IsDrawerOpen)
                DrawerLayout.CloseDrawer();
            else
                DrawerLayout.OpenDrawer();
        }


        /// <summary>
        /// NavigationHelper is used on each page to aid in navigation and
        /// process lifetime management
        /// </summary>
        public NavigationHelper NavigationHelper
        {
            get { return _navigationHelper; }
        }

        private void OnSectionsInViewChanged(object sender, SectionsInViewChangedEventArgs e)
        {
            var selectedSection = Container.SectionsInView.FirstOrDefault();
            if (selectedSection != null)
            {
                MainViewModel.SelectedItem = selectedSection.DataContext as ViewModelBase;
            }
        }
        #region NavigationHelper registration
        /// The methods provided in this section are simply used to allow
        /// NavigationHelper to respond to the page's navigation methods.
        ///
        /// Page specific logic should be placed in event handlers for the  
        /// <see cref="GridCS.Common.NavigationHelper.LoadState"/>
        /// and <see cref="GridCS.Common.NavigationHelper.SaveState"/>.
        /// The navigation parameter is available in the LoadState method
        /// in addition to page state preserved during an earlier session.
        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            _dataTransferManager = DataTransferManager.GetForCurrentView();
            _dataTransferManager.DataRequested += OnDataRequested;
            _navigationHelper.OnNavigatedTo(e);
            await MainViewModel.LoadDataAsync();
        }


        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {  
            _navigationHelper.OnNavigatedFrom(e);
            _dataTransferManager.DataRequested -= OnDataRequested;
        }
        #endregion

        private void OnDataRequested(DataTransferManager sender, DataRequestedEventArgs args)
        {
            var viewModel = MainViewModel.SelectedItem;
            if (viewModel != null)
            {
                viewModel.GetShareContent(args.Request);
            }
        }
    }
}
__________________________________________________________________________________
----------------------------------AppDetailPage XAML------------------------------
__________________________________________________________________________________

<Page
    x:Class="AppStudio.Views.LajmetDetail"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:AppStudio.Views"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:vm="using:AppStudio.ViewModels"
    mc:Ignorable="d">

    <Grid Background="{StaticResource AppBackground}"
          DataContext="{Binding LajmetModel}"
          d:DataContext="{d:DesignData Source=/Assets/Data/LajmetDataSource.json, Type=vm:LajmetViewModel, IsDesignTimeCreatable=true}" Margin="0,-26.667,0,-0.333">

        <Grid.ChildrenTransitions>
            <TransitionCollection>
                <EntranceThemeTransition/>
            </TransitionCollection>
        </Grid.ChildrenTransitions>

        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <FlipView x:Name="Flip" Grid.Row="0" Padding="0,10,0,0" AutomationProperties.AutomationId="ItemsFlipView" AutomationProperties.Name="Item Details" TabIndex="1"
                  ItemsSource="{Binding Items}"
                  ItemTemplate="{StaticResource Lajmet1DetailDetail}"
                  SelectedItem="{Binding SelectedItem, Mode=TwoWay}"/>

    </Grid>

    <Page.BottomAppBar>
        <CommandBar ClosedDisplayMode="Minimal" Background="{StaticResource AppBarBackground}" Foreground="{StaticResource AppBarForeground}">
            <AppBarButton x:Uid="RefreshButton" Icon="Refresh" DataContext="{Binding LajmetModel}" Command="{Binding RefreshCommand}" Visibility="{Binding RefreshVisibility}"/>
            <AppBarButton x:Uid="GoToSourceButton" Icon="Globe" DataContext="{Binding LajmetModel}" Command="{Binding GoToSourceCommand}" Visibility="{Binding GoToSourceVisibility}"/>
        </CommandBar>
    </Page.BottomAppBar>
</Page>

_____________________________________________________       ________________________________________________________________
_____________________________________________________C# Code here:__________________________________________________________


using AppStudio.Services;
using AppStudio.ViewModels;

using Windows.ApplicationModel.DataTransfer;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

namespace AppStudio.Views
{
    public sealed partial class LajmetDetail : Page
    {
        private NavigationHelper _navigationHelper;

        private DataTransferManager _dataTransferManager;

        public LajmetDetail()
        {
            this.InitializeComponent();
            _navigationHelper = new NavigationHelper(this);

            LajmetModel = new LajmetViewModel();
        }

        public LajmetViewModel LajmetModel { get; private set; }

        public NavigationHelper NavigationHelper
        {
            get { return _navigationHelper; }
        }

        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            _dataTransferManager = DataTransferManager.GetForCurrentView();
            _dataTransferManager.DataRequested += OnDataRequested;

            _navigationHelper.OnNavigatedTo(e);

            if (LajmetModel != null)
            {
                await LajmetModel.LoadItemsAsync();
                LajmetModel.SelectItem(e.Parameter);

                LajmetModel.ViewType = ViewTypes.Detail;
            }
            DataContext = this;
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            _navigationHelper.OnNavigatedFrom(e);
            _dataTransferManager.DataRequested -= OnDataRequested;
        }

        private void OnDataRequested(DataTransferManager sender, DataRequestedEventArgs args)
        {
            if (LajmetModel != null)
            {
                LajmetModel.GetShareContent(args.Request);
            }
        }
    }
}

Kind Regards.

magicandre1981
  • 27,895
  • 5
  • 86
  • 127
as__
  • 77
  • 1
  • 10
  • put your code in your original question.. don't post links – MethodMan May 02 '15 at 21:41
  • have you stepped through the code..? where is the error happening.. ? what line.. use the debugger.. – MethodMan May 02 '15 at 22:57
  • That's the problem. All the debugger displays upon crashing is this : `The thread 0x2f4 has exited with code 259 (0x103). The thread 0x11e0 has exited with code 259 (0x103). The program '[5568] AppStudio.exe' has exited with code -1073741819 (0xc0000005) 'Access violation'.` – as__ May 02 '15 at 23:59
  • Are you using this control? https://www.nuget.org/packages/DrawerLayout/ – Abhishek May 04 '15 at 13:48
  • @Abhishek yes. I solved it, check the Answer if you have a similar problem – as__ May 04 '15 at 19:32

2 Answers2

1

Actually, it turns out that the faulty one was the Windows Phone 8,1 SDK. I fixed it by invoking the Dispatcher when navigating to a page.

To anyone who has a similar problem, edit your NavigationServices.cs file, and under NavigateToPage method invoke the Dispatcher:

CoreDispatcher dispatcher = CoreWindow.GetForCurrentThread().Dispatcher; await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { string pageTypeName = String.Format("{0}.{1}", typeof(MainPage).Namespace, pageName); Type pageType = Type.GetType(pageTypeName); App.RootFrame.Navigate(pageType, parameter); });

Don't forget to change the declaration of your function to static public async void NavigateToPage.

Cheers.

as__
  • 77
  • 1
  • 10
0

Presumably DrawerLayout only allows a single child element (eg, if it derives from ContentControl). If that is correct, then you need to create a placeholder parent container as the one and only child of the DrawerLayout, and then you can add your MainFragment and ListFragment to it.

Peter Torr - MSFT
  • 11,824
  • 3
  • 18
  • 51