2010-11-17 20 views
9

Mam stronę wpf przechowywaną w oknie. Ale otrzymuję wyjątek Null, gdy próbuję użyć tego. Działa wtedy używam tego kodu w innej metodzie, ale nie w metodach alla, dlaczego tak jest? proszę o poradę.C# WPF Nie można uzyskać okna nadrzędnego

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

Edit:

herezje pełny kod:

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(); 
    } 

Błąd:

{ "Wartość nie może być null \ r \ nParameter nazwy. DependencyObject"}

Otrzymuję to przy użyciu natychmiastowego okna

?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"} 

Odpowiedz

5

spróbuj Właściciel własność Musisz to przypisać.

Próbka:

public Activity ShowLookUp(Window owner) 
    { 

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

Nie jestem pewien, czy to rozumiem. Dlaczego jest to lepsze rozwiązanie niż 'Window.GetWindow'? Czy możesz podać trochę więcej wyjaśnień? –

+0

Nie mówię, że to jest lepsze rozwiązanie. Po prostu rozwiązuję ten problem. –

+0

Zgadzam się - strona nie ma właściciela za pomocą magii, musisz wyraźnie ustawić właściciela. –

0

Z jakiego kontekstu wchodzisz, dzwoniąc pod numer this.Parent? Czy spodziewasz się, że this będzie odniesieniem do obiektu page? Z przykładowego kodu, który dodałeś, tak nie będzie. Proponuję umieścić punkt przerwania na linii Window.GetWindow i wykonać ?this.GetType() w bezpośrednim oknie, aby zobaczyć, co się dzieje.

11

Czy kod został opublikowany w metodzie konstruktora?

Jednostka nadrzędna modelu UserControl ma zawsze wartość null w swoim konstruktorze, więc this.Parent zwraca referencję zerową. Dlatego wywołanie Window.GetWindow(this.Parent) podnosi ArgumentNullException, ponieważ podany obiekt zależności nie został jeszcze utworzony.

Aby to naprawić, należy umieścić kod w module obsługi zdarzenia Initialized. Gdy to wydarzenie zostanie podniesione, możesz być pewien, że został utworzony UserControl.

+0

Dodałem dane wyjściowe z natychmiastowego okna w moim poście. – Tan

+0

To działa dla mnie. I prawdopodobnie powinna być zaakceptowaną odpowiedzią. – gunwin

+0

Dla mnie działa Loaded event handler – netmajor

0

Tag może być przydatne czasami.

Dlaczego nie spróbować tego.

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

i na swojej stronie, spróbuj tego

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

:)

0

Choć istnieje dopuszczalne odpowiedzi wymienione, wszystkie one wydają się być sposobem na komplikuje sprawę.

Strona nie ma rodzica, ale ponieważ strona to tylko strona, a nie okno wywołujące okno get na samym sobie zwróci odniesienie do okna, a nie stronę, a więc wszystko, czego potrzebujesz, to;

Window w = Window.GetWindow(this); 

Po prostu pomiń.Rodzic

Powiązane problemy