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.