J'ai une page wpf hébergée dans une fenêtre. Mais je reçois une exception Null quand j'essaye de l'utiliser. Cela fonctionne puis j'utilise ce code dans une autre méthode mais pas dans toutes les méthodes pourquoi est-ce comme ça? s'il vous plaît conseil.C# WPF Impossible d'obtenir la fenêtre parent
NewPage page = new NewPage();
Window w = Window.GetWindow(this.Parent);
w.Content = page;
Edit:
Heres le code complet:
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();
}
L'erreur:
{ "La valeur ne peut être nulle \ r \ nParameter nom:. DependencyObject"}
Je reçois ceci lors de l'utilisation de la fenêtre immédiate
?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"}
Je ne suis pas sûr de comprendre cela. Pourquoi est-ce une meilleure solution que 'Window.GetWindow'? Pouvez-vous fournir plus d'explications? –
Je ne dis pas que c'est une meilleure solution. C'est juste la façon dont je résous le problème. –
Je suis d'accord - la page n'a pas de propriétaire par magie, vous devrez définir son propriétaire explicitement. –