本文介绍了如何根据参数选择遍历WPF中的多个表单。用户代码未处理Typeinitializationexception的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 在我的Btn_Cart上点击,根据页面重定向的实体类型将在相应的表单上。当我预订一个产品时,它第一次没有任何错误。在第一个产品添加之后,如果我回来并在此页面上再次返回并选择不同类型的产品,则在初始化窗口的第一行上会出现错误。如果我第一次选择事件类型产品,那么当我尝试在初始化窗口时预订另一种产品类型时会出现以下错误。 错误:类型初始值设定项'WPFKioskDAUZ.ProductAddtoCartWindow'引发了异常。 在另一种情况下,如果我第二次执行与第一次相同的EntityType预订,那么就没有错误。 返回此屏幕时 BookingYourTicketsWindow 和 ProductAddtoCartWindow 窗口我在后面的按钮上有下面的代码。 private void Btn_Back(object sender,RoutedEventArgs e) { GC.Collect (); GC.WaitForPendingFinalizers(); GC.Collect(); this.Close(); } 我的尝试: private void Btn_Cart( object sender,RoutedEventArgs e) { if (EntityType == 12) { SyncProcces(); } else if (EntityType == 5) { SyncProccesEvent(); } } /// /// 如果发生事件 /// private void SyncProccesEvent() { // throw new NotImplementedException() ; BookingYourTicketsWindow eventBooking = new BookingYourTicketsWindow(); eventBooking.Owner = Application.Current。主窗口; eventBooking.ShowInTaskbar = false ; eventBooking.Tag = ChildWindow; eventBooking.Owner = this ; eventBooking.Top = this .Top; eventBooking.Left = this .Left; eventBooking.Show(); // eventBooking.ShowDialog(); } /// // / 如果是产品 /// private void SyncProcces() { // throw new NotImplementedException(); 尝试 { ProductAddtoCartWindow ProductBooking = new ProductAddtoCartWindow(); ProductBooking.Owner = Application.Current.MainWindow; ProductBooking.ShowInTaskbar = false ; ProductBooking.Tag = ChildWindow; ProductBooking.Owner = this ; ProductBooking.Top = this .Top; ProductBooking.Left = this .Left; ProductBooking.Show(); // ProductBooking.ShowDialog(); } catch (例外情况) { / / HideLoader(); // PopUpAlert NewAlert = new PopUpAlert(No Tickets Available); // MainWindowPannel.Children.Add(NewAlert); } } 解决方案 On my Btn_Cart click as per the Entity type of the redirection of the page will be on respective form. When I book one product, first time its went through without any error. After first product add if i do back and come back again on this page and choose different type of product then error is coming on the first line of initialize the window. If first time I have choose event type product then below error is coming when i try to book another product type at the time of initialize the window.Error : The type initializer for 'WPFKioskDAUZ.ProductAddtoCartWindow' threw an exception.In another scenario, if second time I do the same EntityType booking as first then there is no error. While coming back to this screen BookingYourTicketsWindow and ProductAddtoCartWindow window i have below code on back button.private void Btn_Back(object sender, RoutedEventArgs e) { GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); this.Close(); }What I have tried:private void Btn_Cart(object sender, RoutedEventArgs e){if (EntityType == "12"){SyncProcces();}else if (EntityType == "5"){SyncProccesEvent();}}/// /// in case of event /// private void SyncProccesEvent(){//throw new NotImplementedException();BookingYourTicketsWindow eventBooking = new BookingYourTicketsWindow();eventBooking.Owner = Application.Current.MainWindow;eventBooking.ShowInTaskbar = false;eventBooking.Tag = "ChildWindow";eventBooking.Owner = this;eventBooking.Top = this.Top;eventBooking.Left = this.Left;eventBooking.Show();//eventBooking.ShowDialog();}/// /// in case of product/// private void SyncProcces(){//throw new NotImplementedException();try{ ProductAddtoCartWindow ProductBooking = new ProductAddtoCartWindow();ProductBooking.Owner = Application.Current.MainWindow;ProductBooking.ShowInTaskbar = false;ProductBooking.Tag = "ChildWindow";ProductBooking.Owner = this;ProductBooking.Top = this.Top;ProductBooking.Left = this.Left;ProductBooking.Show();//ProductBooking.ShowDialog();}catch (Exception ex){//HideLoader();//PopUpAlert NewAlert = new PopUpAlert("No Tickets Available");//MainWindowPannel.Children.Add(NewAlert);}} 解决方案 这篇关于如何根据参数选择遍历WPF中的多个表单。用户代码未处理Typeinitializationexception的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-30 03:24