gpt4 book ai didi

c# WPF 无法获取父窗口

转载 作者:太空狗 更新时间:2023-10-29 17:39:40 26 4
gpt4 key购买 nike

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

 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 的父级在其构造函数中始终为 null,因此 this.Parent 返回 null 引用。因此,调用 Window.GetWindow(this.Parent) 会引发 ArgumentNullException,因为您指定的依赖对象尚未创建。

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

关于c# WPF 无法获取父窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4205085/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com