【问题标题】:Creating new grid crashes Windows phone app创建新网格会导致 Windows 手机应用程序崩溃
【发布时间】:2015-05-02 21:32:55
【问题描述】:

我想知道为什么创建 Grid 的另一个实例会导致我的应用崩溃。

这是一个基本的 RSS Feed 阅读器应用程序。应用可以正常打开,我可以看到 MainPage,并且 DrawerLayout 对象可以正常工作,但是当单击项目(文章)(然后会导致 AppDetailPage)时,应用会崩溃。

代码如下:

------------------主页面--------------- ------------------------------------

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

亲切的问候。

【问题讨论】:

  • 把你的代码放在你原来的问题中..不要发布链接
  • 你有没有通过代码..?错误发生在哪里..?哪一行..使用调试器..
  • 这就是问题所在。崩溃时显示的所有调试器都是这样的: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'.
  • 你在用这个控件吗? nuget.org/packages/DrawerLayout
  • @Abhishek 是的。我解决了,如果你有类似的问题,请查看答案

标签: c# xaml windows-phone windows-phone-8.1 drawerlayout


【解决方案1】:

实际上,故障是 Windows Phone 8,1 SDK。我通过在导航到页面时调用 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); });

不要忘记将函数的声明更改为static public async void NavigateToPage

干杯。

【讨论】:

    【解决方案2】:

    大概DrawerLayout 只允许单个子元素(例如,如果它派生自ContentControl)。如果正确,那么您需要创建一个占位符父容器作为DrawerLayout 的唯一子容器,然后您可以将MainFragmentListFragment 添加到其中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-05
      • 1970-01-01
      • 2015-12-18
      相关资源
      最近更新 更多