AISYS VISION
About us News Products download Technology Contact us
Sitemap Home Chinese English
Technology Questions

[WPF]如何解決在WPF應用程式運行時出現System.StackOverflowException 問題

 

windows 10 18xx版本中若把元件的始化時放置於Window.Loaded事件中並發生StackOverflowException錯誤。這可能為WPF在視窗生成時但渲染尚未完畢並與OCX發生問題,

因此將OCX元件初始化置於Window.ContentRendered事件內,此事件會在渲染完成時發出,如此能避免掉問題。

 

以下為程式片段

MainWindow.xaml

<Window x:Class="Aisys.WpfFormApp.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:Aisys.WpfFormApp"

        mc:Ignorable="d"

        ContentRendered="Window_ContentRendered" <!-- 加入ContentRendered事件 -->

        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">

    <Grid>

        <Button Click="Button_Click" />

    </Grid>

</Window>

 

MainWindow.xaml.cs

private void Window_ContentRendered(object sender, EventArgs e)

{

    axAltairU = new AxAltairU();

    axAltairU.OnChannelCreated += AxAltairU_OnChannelCreated;

    axAltairU.OnChannelDestroyed += AxAltairU_OnChannelDestroyed;

    axAltairU.QuickCreateChannel();

    …

}

Copyright ©2008 Internet Service by
AISYS Vison company.