如何根据参数选择遍历WPF中的多个表单。用户代码未处理Typeinitializationexception [英] How do I traverse to multiple form in WPF as per parameter selection. Typeinitializationexception was unhandled by usercode

查看:61
本文介绍了如何根据参数选择遍历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);
}
}

解决方案

好的,我已经使用了您的概念并为您准备了线框样本。



主窗口Xaml:

 <   Window     x:Class   =  WpfWindows.MainWindow  

xmlns = http://schemas.microsoft.com/winfx/2006/xaml/presentation

xmlns:x = http://schemas.microsoft.com/winfx/2006/xaml

xmlns:d = http:// schemas.microsoft.com/expression/blend/2008\"

xmlns:mc = http://schemas.openxmlformats.org/markup-compatibility/2006

xmlns:local = clr-namespac e:WpfWindows

mc:可忽略 = d WindowStartupLocation = CenterScreen

标题 = MainWindow 高度 = 350 宽度 = 525 < span class =code-keyword>>
< 网格 >
< StackPanel 方向 = 水平

< span class =code-attribute> Horizo​​ntalAlignment = 中心

< span class =code-attribute> VerticalAlignment = 中心 >
< 按钮 x:名称 = Action1 内容 = Action1

< span class =code-attribute> 填充 = 10 5 保证金 = 5

< span class =code-attribute> 点击 = Action_Click / >
< 按钮 x:姓名 = Action2 内容 = Action2

填充 = 10 5 保证金 = 5

点击 < span class =code-keyword> =
Action_Click / >
< / StackPanel >
< / Grid >
< / Window >

主窗口代码 - 背后:

 使用 System.Windows; 

命名空间 WpfWindows
{
public partial class MainWindow:Window
{
public MainWindow()
{
InitializeComponent();
}

private void Action_Click( object sender,RoutedEventArgs e)
{
Window window = null ;
if (sender == Action1)
window = new Window1();
else
window = new Window2();
window.Owner = ;
window.Show();
}
}
}

Window1 Xaml:

 <  < span class =code-leadattribute> Window     x:Class   =  WpfWindows.Window1  

< span class =code-attribute> xmlns = http://schemas.microsoft.com/winfx/2006/xaml/presentation

xmlns:x = http://schemas.microsoft.com/winfx / 2006 / xaml

xmlns:d = http://schemas.microsoft.com/expression/blend/2008\"

xmlns:mc = http://schemas.openxmlformats.org/markup-compatibility/2006

xmlns :local = clr-命名空间:WpfWindows

mc:可忽略 = d WindowStartupLocation = CenterOwner

标题 < span class =code-keyword> = Window1 高度 = 300 宽度 = 300 >
< 网格 >
< Grid.RowDefinitions >
< RowDefinition / >
< RowDefinition 高度 = 自动 / >
< /Grid.RowDefinitions >
< Viewbox >
< TextBlock 文字 = Window1 / >
< / Viewbox >
< 按钮 x:名称 = 返回 Grid.Row = 1

填充 = 10 5 保证金 = < span class =code-keyword> 10

< span class =code-attribute> Horizo​​ntalAlignment = 中心

内容 = BACK

< span class =code-attribute> 点击 = Back_Click / >
< / Grid >
< / Window >

Window1代码隐藏:

 使用系统。视窗; 

命名空间 WpfWindows
{
public partial class Window1:Window
{
public Window1()
{
InitializeComponent();
}

private void Back_Click( object sender,RoutedEventArgs e)
{
Close();
}
}
}

Window2 Xaml:

 <  < span class =code-leadattribute> Window     x:Class   =  WpfWindows.Window2  

< span class =code-attribute> xmlns = http://schemas.microsoft.com/winfx/2006/xaml/presentation

xmlns:x = http://schemas.microsoft.com/winfx /2006/xaml\"

xmlns:d=\" http://schemas.microsoft.com/expression/blend/2008\"

xmlns:mc=\"http://schemas.openxmlformats.org/markup-compatibility/2006\"

xmlns:local=\"clr- namespace:WpfWindows\"

mc:Ignorable=\"d\" WindowStartupLocation=\"CenterOwner\"

Title=\"Window2\" Height=\"300\" Width=\"300\">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height=\"Auto\"/>
</Grid.RowDefinitions>
<Viewbox>
<TextBlock Text=\"Window2\"/>
</Viewbox>
<Button x:Name=\"Back\" Grid.Row=\"1\"

Padding=\"10 5\" Margin=\"10\"

HorizontalAlignment=\"Center\"

Content=\"BACK\"

Click=\"Back_Click\"/>
</Grid>
</Window>

Window2 Code-Behind:

using System.Windows; 

namespace WpfWindows
{
public partial class Window2 : Window
{
public Window2()
{
InitializeComponent();
}

private void Back_Click(object sender, RoutedEventArgs e)
{
Close();
}
}
}



As you will see, everything works as expected.



So what this is telling me is that there is a problem in your code in one of these windows, BookingYourTicketsWindow in this case.



Time to do some debugging... What I recommend is to comment out parts or all of your code to make sure the Window opens, closes, and reopens without errors. IF the issue is still there, then the problem is in your Xaml. PAir that back gradually until the error goes away, then you have identified the issue. Same goes for the code if the error is not in the Xaml, slowly re-introduce parts until the error re-appears.



Good Luck! :)


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

解决方案

Okay, I have used your concept and put together a wire-frame sample for you.

Main Window Xaml:

<Window x:Class="WpfWindows.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

        xmlns:local="clr-namespace:WpfWindows"

        mc:Ignorable="d" WindowStartupLocation="CenterScreen"

        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <StackPanel Orientation="Horizontal"

                    HorizontalAlignment="Center" 

                    VerticalAlignment="Center">
            <Button x:Name="Action1" Content="Action1" 

                    Padding="10 5" Margin="5"

                    Click="Action_Click"/>
            <Button x:Name="Action2" Content="Action2" 

                    Padding="10 5" Margin="5"

                    Click="Action_Click"/>
        </StackPanel>
    </Grid>
</Window>

Main Window Code-Behind:

using System.Windows;

namespace WpfWindows
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Action_Click(object sender, RoutedEventArgs e)
        {
            Window window = null;            
            if (sender == Action1)
                window = new Window1();
            else
                window = new Window2();
            window.Owner = this;
            window.Show();
        }
    }
}

Window1 Xaml:

<Window x:Class="WpfWindows.Window1"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

        xmlns:local="clr-namespace:WpfWindows"

        mc:Ignorable="d" WindowStartupLocation="CenterOwner"

        Title="Window1" Height="300" Width="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Viewbox>
            <TextBlock Text="Window1"/>
        </Viewbox>
        <Button x:Name="Back" Grid.Row="1"

                Padding="10 5" Margin="10"

                HorizontalAlignment="Center"

                Content="BACK"

                Click="Back_Click"/>
    </Grid>
</Window>

Window1 Code-Behind:

using System.Windows;

namespace WpfWindows
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void Back_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}

Window2 Xaml:

<Window x:Class="WpfWindows.Window2"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

        xmlns:local="clr-namespace:WpfWindows"

        mc:Ignorable="d" WindowStartupLocation="CenterOwner"

        Title="Window2" Height="300" Width="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Viewbox>
            <TextBlock Text="Window2"/>
        </Viewbox>
        <Button x:Name="Back" Grid.Row="1"

                Padding="10 5" Margin="10"

                HorizontalAlignment="Center"

                Content="BACK"

                Click="Back_Click"/>
    </Grid>
</Window>

Window2 Code-Behind:

using System.Windows;

namespace WpfWindows
{
    public partial class Window2 : Window
    {
        public Window2()
        {
            InitializeComponent();
        }

        private void Back_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}


As you will see, everything works as expected.

So what this is telling me is that there is a problem in your code in one of these windows, BookingYourTicketsWindow in this case.

Time to do some debugging... What I recommend is to comment out parts or all of your code to make sure the Window opens, closes, and reopens without errors. IF the issue is still there, then the problem is in your Xaml. PAir that back gradually until the error goes away, then you have identified the issue. Same goes for the code if the error is not in the Xaml, slowly re-introduce parts until the error re-appears.

Good Luck! :)


这篇关于如何根据参数选择遍历WPF中的多个表单。用户代码未处理Typeinitializationexception的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆