1

我正在研究 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)
        {

        }


    }
}

窗口 1 - 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>

// - - 堆栈跟踪 - - - - - - - - -

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: 
4

2 回答 2

1

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

也许这是 DevExpress 的 LayoutGroup 的错误。

于 2013-06-04T11:59:58.050 回答
0

LayoutGroup 不能放置到任意容器中。

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

请参阅B201388

于 2014-04-09T17:44:37.127 回答