c#WPF无法获取父窗口

Tan*_*Tan 9 c# wpf

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

 NewPage page = new NewPage ();
 Window w = Window.GetWindow(this.Parent);
 w.Content = page;
Run Code Online (Sandbox Code Playgroud)

编辑:

继承人完整的代码:

    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();
    }
Run Code Online (Sandbox Code Playgroud)

错误 :

{"值不能为空.\ r \nParameter name: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"}
Run Code Online (Sandbox Code Playgroud)

Cod*_*ray 11

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

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

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

  • 在我的情况下,我必须用“Loaded”而不是“Initialized”来调用它。WPF 可能会多次运行 Loaded。 (2认同)

klm*_*lm_ 5

尝试所有者属性您必须分配它.

样品:

   public Activity ShowLookUp(Window owner)
    {

        ActivityLookUp lookup = new ActivityLookUp();
        lookup.Owner = owner;
        lookup.ShowDialog();
    }
Run Code Online (Sandbox Code Playgroud)

  • 我不确定我是否理解这一点。为什么这是一个比“Window.GetWindow”更好的解决方案?你能提供更多的解释吗? (2认同)