[英]c# WPF Cant get Parent Window
我在窗口中托管了一個 wpf 頁面。 但是當我嘗試使用它時,我得到了 Null 異常。 它工作然后我在另一種方法中使用此代碼但不是在所有方法中為什么會這樣? 請指教。
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"}
是您在構造函數方法中發布的代碼嗎?
UserControl
的父級在其構造函數中始終為空,因此this.Parent
返回空引用。 因此,調用Window.GetWindow(this.Parent)
會引發ArgumentNullException
因為尚未創建您指定的依賴對象。
要解決此問題,您需要將代碼放在Initialized
事件處理程序中。 引發此事件時,您可以確定UserControl
已創建。
試試Owner屬性,你必須分配它。
樣本:
public Activity ShowLookUp(Window owner)
{
ActivityLookUp lookup = new ActivityLookUp();
lookup.Owner = owner;
lookup.ShowDialog();
}
標簽有時很有用。
為什么不試試這個。
// "this" is your Window
YourFrame.Content = new YourPage() { Tag = this };
在您的主頁中,試試這個
Window w = (Window)this.Tag;
// and do all the Window wonders
:)
盡管列出了可接受的答案,但它們似乎都使問題復雜化了。
Page 沒有父級,但由於 Page 只是一個頁面而不是一個窗口,調用 get window 本身將返回窗口引用而不是頁面,因此您只需要;
Window w = Window.GetWindow(this);
只需省略 .Parent
當您調用this.Parent
時,您處於什么環境中? 您是否希望this
是對page
對象的引用? 從您添加的代碼示例來看,情況並非如此。 我建議您在Window.GetWindow
行放置一個斷點,然后在直接窗口中執行?this.GetType()
以查看發生了什么。
您可以使用不同的選項:
從子用戶控件背后的代碼
但是如果在構造函數中調用它們,它們都將返回 null,因為子項的初始化發生在將控件附加到可視化樹之前。
因此,您必須在子用戶控件完成加載后調用它們。
public MyChildUserControl()
{
InitializeComponent();
//Postponing the retrieval of the parent using the OnLoaded event
Loaded += (s, e) =>
{
var p = VisualTreeHelper.GetParent(this);
while (!(ucParent is UserControl))
{
ucParent = LogicalTreeHelper.GetParent((DependencyObject) this);
}
_parentWindow = Window.GetWindow(this);
/// whatever you are going to do with parent window
};
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.