2010-11-17 155 views
9

我有一个wpf页面托管在一个窗口中。但是当我尝试使用这个时,我得到了Null异常。 它的作品,然后我在另一种方法,但不是在alla方法中使用此代码为什么是这样? 请指教。c#WPF不能获得父窗口

NewPage page = new NewPage(); 
Window w = Window.GetWindow(this.Parent); 
w.Content = page; 

编辑:

继承人的完整代码:

public HandOverListPage() { 
     InitializeComponent(); 

     _settings = new Settings(); 
    } 


    public void ShowCurrentInUseAssignment() { 

     _currentDoc = (App.Current as App).SelectedHandOverDoc; 

     var r = from item in (App.Current as App).SelectedHandOverDoc.Items 
       where item.Status != 20 
       select item; 

     if(r.Count() == 0) { 
      //Report assignment to QP with status finished 
      ReportAssignment(); 

      HandOverPage page = new HandOverPage(); 

      Window w = Window.GetWindow(this.Parent); 
      w.Content = page; 

      return; 
     } else { 
      ICollectionView view = CollectionViewSource.GetDefaultView((App.Current as App).SelectedHandOverDoc.Items); 
      view.SortDescriptions.Add(new SortDescription("Status", ListSortDirection.Ascending)); 

      ListBoxAssignmentItems.ItemsSource = view; 
     } 

     TxtBlockCounter.Text = r.Count().ToString(); 
    } 

错误:

{ “值不能为空\ r \ n参数名:DependencyObject的”}

使用立即窗口时我得到这个

?this.GetType() 
{Name = "HandOverListPage" FullName = "QP_Truck.Pages.HandOverListPage"} 
    [System.RuntimeType]: {Name = "HandOverListPage" FullName = "QP_Truck.Pages.HandOverListPage"} 
    base {System.Reflection.MemberInfo}: {Name = "HandOverListPage" FullName = "QP_Truck.Pages.HandOverListPage"} 
    Assembly: {QP Truck, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null} 
    AssemblyQualifiedName: "QP_Truck.Pages.HandOverListPage, QP Truck, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" 
    Attributes: Public | BeforeFieldInit 
    BaseType: {Name = "Page" FullName = "System.Windows.Controls.Page"} 
    ContainsGenericParameters: false 
    DeclaringMethod: 'this.GetType().DeclaringMethod' threw an exception of type 'System.InvalidOperationException' 
    DeclaringType: null 
    FullName: "QP_Truck.Pages.HandOverListPage" 
    GenericParameterAttributes: 'this.GetType().GenericParameterAttributes' threw an exception of type 'System.InvalidOperationException' 
    GenericParameterPosition: 'this.GetType().GenericParameterPosition' threw an exception of type 'System.InvalidOperationException' 
    GUID: {93eb30b9-a64e-3c6b-9182-0f93582d188d} 
    HasElementType: false 
    IsAbstract: false 
    IsAnsiClass: true 
    IsArray: false 
    IsAutoClass: false 
    IsAutoLayout: true 
    IsByRef: false 
    IsClass: true 
    IsCOMObject: false 
    IsContextful: false 
    IsEnum: false 
    IsExplicitLayout: false 
    IsGenericParameter: false 
    IsGenericType: false 
    IsGenericTypeDefinition: false 
    IsImport: false 
    IsInterface: false 
    IsLayoutSequential: false 
    IsMarshalByRef: false 
    IsNested: false 
    IsNestedAssembly: false 
    IsNestedFamANDAssem: false 
    IsNestedFamily: false 
    IsNestedFamORAssem: false 
    IsNestedPrivate: false 
    IsNestedPublic: false 
    IsNotPublic: false 
    IsPointer: false 
    IsPrimitive: false 
    IsPublic: true 
    IsSealed: false 
    IsSerializable: false 
    IsSpecialName: false 
    IsUnicodeClass: false 
    IsValueType: false 
    IsVisible: true 
    MemberType: TypeInfo 
    Module: {QP Truck.exe} 
    Namespace: "QP_Truck.Pages" 
    ReflectedType: null 
    StructLayoutAttribute: {System.Runtime.InteropServices.StructLayoutAttribute} 
    TypeHandle: {System.RuntimeTypeHandle} 
    TypeInitializer: null 
    UnderlyingSystemType: {Name = "HandOverListPage" FullName = "QP_Truck.Pages.HandOverListPage"} 

回答

5

尝试所有者属性 您必须指定它。

样品:

public Activity ShowLookUp(Window owner) 
    { 

     ActivityLookUp lookup = new ActivityLookUp(); 
     lookup.Owner = owner; 
     lookup.ShowDialog(); 
    } 
+1

我不确定我是否理解这一点。为什么这比'Window.GetWindow'更好的解决方案?你能提供更多的解释吗? – 2010-11-17 14:48:04

+0

我并不是说这是更好的解决方案。这只是我解决问题的方式。 – 2010-11-17 14:54:52

+0

我同意 - 页面没有魔法所有者,您必须明确设置其所有者。 – 2010-11-17 15:06:07

0

当你打电话给this.Parent时,你在什么情况下?您是否预计this是对page对象的引用?从您添加的代码示例中,情况并非如此。我建议你在Window.GetWindow行放置一个断点,并在即时窗口中输入?this.GetType()以查看发生了什么。

11

是您在构造函数方法中发布的代码?

UserControl的父项在其构造函数中始终为空,因此this.Parent正在返回空引用。因此,调用Window.GetWindow(this.Parent)会产生一个ArgumentNullException,因为您指定的依赖项对象尚未创建。

要解决这个问题,您需要将代码放在Initialized事件处理程序中。发生此事件时,您可以确定UserControl已创建。

+0

我在我的帖子中添加了立即窗口的输出。 – Tan 2010-11-17 15:05:32

+0

这适用于我。也许应该是被接受的答案。 – gunwin 2015-07-19 00:49:22

+0

对我来说作品加载事件处理程序 – netmajor 2016-07-18 13:36:47

0

标签可以是有用的时候。

为什么不试试这个。

// "this" is your Window 
YourFrame.Content = new YourPage() { Tag = this }; 

,并在您的网页,尝试这个

Window w = (Window)this.Tag; 
// and do all the Window wonders 

:)

0

虽然没有列出了可接受的答案,他们似乎都大大超过此事复杂化。

页面没有父页面,但由于页面只是一个页面,而不是一个窗口调用get窗口本身将返回窗口引用而不是页面,因此您所需要的仅仅是;

Window w = Window.GetWindow(this); 

简单地省略。父