I would like to know why creating another instance of Grid
is making my app crash.
我想知道为什么创建另一个Grid实例会让我的应用程序崩溃。
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.
它是一个基本的RSS Feed阅读器应用程序。该应用程序打开正常,我可以看到MainPage,DrawerLayout对象工作,但当点击一个项目(文章)(然后导致AppDetailPage)应用程序崩溃。
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.
2 个解决方案
#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.
实际上,事实证明,错误的是Windows Phone 8,1 SDK。我通过在导航到页面时调用Dispatcher来修复它。
To anyone who has a similar problem, edit your NavigationServices.cs file, and under NavigateToPage method invoke the Dispatcher:
对于有类似问题的任何人,编辑您的NavigationServices.cs文件,并在NavigateToPage方法下调用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); });
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
.
不要忘记将函数声明更改为static public async void NavigateToPage。
Cheers.
#2
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.
据推测,DrawerLayout只允许一个子元素(例如,如果它派生自ContentControl)。如果这是正确的,那么您需要创建一个占位符父容器作为DrawerLayout的唯一子容器,然后您可以将MainFragment和ListFragment添加到它。
#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.
实际上,事实证明,错误的是Windows Phone 8,1 SDK。我通过在导航到页面时调用Dispatcher来修复它。
To anyone who has a similar problem, edit your NavigationServices.cs file, and under NavigateToPage method invoke the Dispatcher:
对于有类似问题的任何人,编辑您的NavigationServices.cs文件,并在NavigateToPage方法下调用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); });
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
.
不要忘记将函数声明更改为static public async void NavigateToPage。
Cheers.
#2
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.
据推测,DrawerLayout只允许一个子元素(例如,如果它派生自ContentControl)。如果这是正确的,那么您需要创建一个占位符父容器作为DrawerLayout的唯一子容器,然后您可以将MainFragment和ListFragment添加到它。