2010-11-17 42 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); 

簡單地省略。父