显示Window时抛出System.NullReferenceException

时间:2013-06-04 07:58:51

标签: c# wpf xaml devexpress

我正在研究WPF并使用Devexpress工具进行GUI,

每当我调试我的程序时,它总是生成Null对象引用的异常并指向W1.SHOW();我创建了一个window2。

有人可以解释原因吗?

程序工作正常,但是当我点击按钮然后出现window2时,在那个窗口中我显示了两个标签。

Window2也会显示这两个标签,但是当我点击其中一个标签时,它会生成一个例外。我不知道为什么这样做:(

// using statements omitted for brevity...

namespace WpfApplication1
{

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
                   CreateNavBarControl();
        }

     // When user will click NewDocumentButton, following function will get execute.   
        private void NewDocumentButton_Click(object sender, RoutedEventArgs e)
        {
            Window2 W1 = null;
            //obj.ShowDialog();
            //obj.Close();

            if (W1 == null)
            {
                ***W1 = new Window2();
                W1.Show();***

            }


        }

        }
    }

Window2 - 代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for Window2.xaml
    /// </summary>
    public partial class Window2 : Window
    {
        public Window2()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }


    }
}

Window1 - XAML

    <Window x:Class="WpfApplication1.MainWindow"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="News Room Computer System" Height="196" Width="331"  xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" 
            xmlns:dxn="http://schemas.devexpress.com/winfx/2008/xaml/navbar"
           xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars"
            dx:ThemeManager.ThemeName="MetropolisDark" 
            xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" 
            Loaded="Window_Loaded" DataContext="{Binding}" xmlns:dxlc="http://schemas.devexpress.com/winfx/2008/xaml/layoutcontrol" Background="Black">
        <Grid>

            <DockPanel x:Name="dockPanel" Margin="5,25,0,0"   Width="86" LastChildFill="False" HorizontalAlignment="Left">

            </DockPanel>
            <dxlc:LayoutGroup Margin="5,0,12,138" Name="layoutGroup1">
                <Button Name="NewDocumentButton" Height="19" Width="45"   
                        VerticalAlignment="Top" Content="New" Click="NewDocumentButton_Click"  />
            </dxlc:LayoutGroup>
        </Grid>


    </Window>

窗口2 - XAML

<Window x:Class="WpfApplication1.Window2"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window2" Height="300" Width="300" xmlns:dxlc="http://schemas.devexpress.com/winfx/2008/xaml/layoutcontrol" Loaded="Window_Loaded" xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors">
    <Grid>
        <dxlc:LayoutGroup Header="LayoutGroup" Name="layoutGroup2" View="Tabs" Width="202" Height="175" Margin="38,20,38,66" ContextMenuClosing="layoutGroup2_ContextMenuClosing" ContextMenuOpening="layoutGroup2_ContextMenuClosing" DragEnter="layoutGroup2_DragEnter" MeasureSelectedTabChildOnly="True" SelectedTabIndex="2" DragScrolling="True">
            <dxlc:LayoutGroup Header="ass" Name="layoutGroup3"  Height="116" Width="153" />
            <dxlc:LayoutControl Height="117"  HorizontalAlignment="Left" dxlc:LayoutControl.TabHeader="va" Name="layoutControl1"  VerticalAlignment="Top" Width="164">
                <dxlc:LayoutItem Label="layoutItem1" Name="layoutItem1">
                    <dxe:TextEdit Name="textEdit1" />
                </dxlc:LayoutItem>
            </dxlc:LayoutControl>
        </dxlc:LayoutGroup>
    </Grid>
</Window>

// ---- STACKTRACE ------------------

System.NullReferenceException was unhandled
  Message=Object reference not set to an instance of an object.
  Source=DevExpress.Xpf.LayoutControl.v12.2
  StackTrace:
       at DevExpress.Xpf.LayoutControl.LayoutGroup.OnTabControlSelectionChanged()
       at DevExpress.Xpf.LayoutControl.LayoutGroup.OnTabControlSelectionChanged(Object sender, TabControlSelectionChangedEventArgs e)
       at DevExpress.Xpf.Core.DXTabControl.RaiseSelectionChanged(Int32 oldSelectedIndex)
       at DevExpress.Xpf.Core.DXTabControl.OnSelectedIndexPropertyChanged(Int32 oldValue)
       at DevExpress.Xpf.Core.DXTabControl.<.cctor>b__0(DependencyObject d, DependencyPropertyChangedEventArgs e)
       at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
       at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
       at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
       at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
       at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
       at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
       at DevExpress.Xpf.Core.DXTabControl.OnSelectedItemPropertyChanged(Object oldValue)
       at DevExpress.Xpf.Core.DXTabControl.<.cctor>b__1(DependencyObject d, DependencyPropertyChangedEventArgs e)
       at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
       at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
       at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
       at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
       at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
       at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
       at DevExpress.Xpf.Core.DXTabControl.OnSelectedTabItemPropertyChanged(DXTabItem oldValue)
       at DevExpress.Xpf.Core.DXTabControl.<.cctor>b__2(DependencyObject d, DependencyPropertyChangedEventArgs e)
       at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
       at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
       at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
       at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
       at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
       at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
       at DevExpress.Xpf.Core.DXTabControl.DevExpress.Xpf.Core.ITabControl.SelectItem(DXTabItem item)
       at DevExpress.Xpf.Core.DXTabItem.OnIsSelectedPropertyChanged()
       at DevExpress.Xpf.Core.DXTabItem.<.cctor>b__2(DependencyObject d, DependencyPropertyChangedEventArgs e)
       at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
       at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
       at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
       at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
       at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
       at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
       at DevExpress.Xpf.Core.DXTabItem.OnMouseLeftButtonClick(MouseButtonEventArgs e)
       at DevExpress.Xpf.Core.DXTabItem.OnMouseLeftButtonDown(MouseButtonEventArgs e)
       at System.Windows.UIElement.OnMouseLeftButtonDownThunk(Object sender, MouseButtonEventArgs e)
       at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
       at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
       at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
       at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
       at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
       at System.Windows.UIElement.OnMouseDownThunk(Object sender, MouseButtonEventArgs e)
       at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
       at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
       at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
       at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
       at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
       at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
       at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
       at System.Windows.Input.InputManager.ProcessStagingArea()
       at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
       at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
       at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
       at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
       at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
       at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
       at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
       at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
       at System.Windows.Threading.Dispatcher.Run()
       at System.Windows.Application.RunDispatcher(Object ignore)
       at System.Windows.Application.RunInternal(Window window)
       at System.Windows.Application.Run(Window window)
       at System.Windows.Application.Run()
       at WpfApplication1.App.Main() in C:\Documents and Settings\Administrator\My Documents\Visual Studio 2010\Projects\WpfApplication1\WpfApplication1\obj\x86\Debug\App.g.cs:line 0
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
  Detail:  at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart()  InnerException: 

2 个答案:

答案 0 :(得分:1)

SelectedTabIndex="2"中的LayoutGroup移除Window2声明。 LayoutGroup中没有足够的选项卡可供选择,这会导致NullReferenceException。

也许这是来自DevExpress的LayoutGroup的错误。

答案 1 :(得分:0)

  

无法将LayoutGroup放入任意容器中。

尝试在View="Tabs"控件中使用<dxlc:LayoutControl>包装LayoutGroup。

请参阅B201388