WPF窗口在启动时抛出TypeInitializationException异常

5
我有一个带有多个选项卡的Excel插件,其中一个选项卡是设置窗口。当单击它时,会显示一个自定义窗口。但是,在单击时没有任何内容显示。我在日志文件中看到了以下异常。这是什么原因,如何解决?非常感谢。
编辑,这里是XAML代码:
<Window x:Class="MyShared.View.ConnectionSetup"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit"
    xmlns:Converter="clr-namespace:MIMICShared.Converter" 
    WindowStartupLocation="CenterOwner"
    Title="Settings" Width="446" Height="650"  Closing="WindowClosing"
DataContextChanged="UserControlDataContextChanged" Foreground="Black" FontWeight="Normal" HorizontalAlignment="Stretch">
<Window.Resources>
    <ResourceDictionary>
        <Converter:Status2ImageConverter x:Key="string2Image"/>
    </ResourceDictionary>
</Window.Resources>

<Grid FocusManager.FocusedElement="{Binding ElementName=uname}" >
    <Grid.RowDefinitions>
        <RowDefinition Height="0"></RowDefinition>
        <RowDefinition Height="*"></RowDefinition>
        <RowDefinition Height="0"></RowDefinition>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition Height="0"></RowDefinition>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition Height="5"></RowDefinition>
    </Grid.RowDefinitions>
    <GroupBox Grid.Row="1" Foreground="Blue">
        <Grid >
            <Grid.ColumnDefinitions>                    
                <ColumnDefinition Width="5"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="5"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="2"></RowDefinition>
                <RowDefinition Height="30"></RowDefinition>
                <RowDefinition Height="2"></RowDefinition>
                <RowDefinition Height="*"></RowDefinition>
                <RowDefinition Height="2"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height="2"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height="2"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height="2"></RowDefinition>
            </Grid.RowDefinitions>
            <Label Grid.Row="1" Grid.ColumnSpan="4" FontSize="14" HorizontalAlignment="Center" FontWeight="Bold" VerticalAlignment="Bottom">Historical</Label>
            <GroupBox Grid.Column="1" Grid.Row="3" Header="Connections" Foreground="Blue" Grid.ColumnSpan="2">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*" ></RowDefinition>
                        <RowDefinition Height="Auto"></RowDefinition>
                    </Grid.RowDefinitions>

                    <toolkit:DataGrid x:Name="connectionlist" Margin="5" Style="{DynamicResource DataGridStyleDefault}" Background="White"
              AutoGenerateColumns="False" HeadersVisibility="All"
              SelectionMode="Single"
              GridLinesVisibility="None"                  
              ItemsSource="{Binding SettingList}" CanUserAddRows="False" CanUserDeleteRows="True"
              SelectionUnit="FullRow" SelectedItem="{Binding SelectedItem}" 
              SelectionChanged="ConnectionlistSelectionChanged"                               
              VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled">
                        <!-- Column definition -->
                        <toolkit:DataGrid.Columns>
                            <!-- Pool -->
                            <!--<toolkit:DataGridCheckBoxColumn Header="Pool" Width="40" Binding="{Binding pool}" />-->
                            <!-- Connections info -->
                            <toolkit:DataGridTextColumn Header="Connection Info" Width="*" Binding="{Binding host}" />
                            <toolkit:DataGridTextColumn Header="Description" Width="*"  Binding="{Binding desc}" >
                                <toolkit:DataGridTextColumn.ElementStyle>
                                    <Style TargetType="{x:Type TextBlock}">
                                        <Setter Property="TextWrapping" Value="Wrap" />
                                    </Style>
                                </toolkit:DataGridTextColumn.ElementStyle>
                            </toolkit:DataGridTextColumn>
                        </toolkit:DataGrid.Columns>
                    </toolkit:DataGrid>

                    <Grid Grid.Row="2" HorizontalAlignment="Center">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"></ColumnDefinition>
                                <ColumnDefinition Width="Auto"></ColumnDefinition>
                            </Grid.ColumnDefinitions>
                            <Button Margin="2" Padding="10 0" x:Name="AddButton" Click="AddClicked">Add</Button>
                            <Button Grid.Column="1" Margin="2" Padding="10 0" Command="{Binding Remove}">Remove</Button>
                        </Grid>
                    </Grid>
                </Grid>
            </GroupBox>

            <GroupBox Header="Authentication" Grid.Row="5" Grid.Column="1" Foreground="Blue" Grid.ColumnSpan="2">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition  Height="Auto"/>
                        <RowDefinition  Height="2"/>
                        <RowDefinition  Height="Auto"/>
                        <RowDefinition  Height="2"/>
                        <RowDefinition  Height="Auto"/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition  Width="Auto"/>
                        <ColumnDefinition  Width="5"/>
                        <ColumnDefinition  Width="Auto"/>
                        <ColumnDefinition  Width="Auto"/>
                    </Grid.ColumnDefinitions>

                    <Label Grid.Row="0" Grid.Column="0">Username:</Label>
                    <TextBox x:Name="uname" Width="100" Grid.Row="0" Grid.Column="2" Text="{Binding UserName, UpdateSourceTrigger=PropertyChanged}" ></TextBox>

                    <Label Grid.Row="2" Grid.Column="0">Password:</Label>
                    <PasswordBox x:Name="pwd" ToolTip="Password" PasswordChar="*"                             
                         Width="100" Grid.Row="2" Grid.Column="2" KeyDown="OnKeyDownHandler"></PasswordBox>
                    <Label Grid.Row="2" Grid.Column="3">
                        <Hyperlink Click="HyperlinkClick">Forgot Password?</Hyperlink>
                    </Label>
                    <CheckBox Margin="10 0 0 0" Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2" x:Name="saveauth" IsChecked="{Binding SaveAuth}">Save username and password</CheckBox>
                    <Button Margin="10 0 0 0" Grid.Row="4" Grid.Column="3" Click="VerifyButtonClick">OK</Button>
                </Grid>
            </GroupBox>
            <GroupBox Header="Status" Grid.Row="7" Grid.Column="1" Foreground="Blue" Grid.ColumnSpan="2">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition  Height="0"/>
                        <RowDefinition  Height="Auto"/>
                        <RowDefinition  Height="0"/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition  Width="80"/>
                        <ColumnDefinition  Width="20"/>
                        <ColumnDefinition  Width="Auto"/>
                    </Grid.ColumnDefinitions>
                    <Label Grid.Row="1" Grid.Column="0">Historical</Label>
                    <Image Grid.Row="1" Grid.Column="1" 
                           Source="{Binding ElementName=historicalStatusLabel, Path=Content, Converter={StaticResource string2Image}}"
                           ToolTip="Connection Status"></Image>
                    <Label  Name="historicalStatusLabel" Grid.Row="1" Grid.Column="2" Content="{Binding ConnectionStatus, UpdateSourceTrigger=PropertyChanged}"/>
                </Grid>
            </GroupBox>
        </Grid>
    </GroupBox>
    <GroupBox Grid.Row="3" Grid.Column="1" Foreground="Blue">
        <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="0"></RowDefinition>
            <RowDefinition Height="30"></RowDefinition>
            <RowDefinition Height="0"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="0"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="0"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="0"></RowDefinition>
        </Grid.RowDefinitions>
            <Label Grid.Row="1" FontSize="14" FontWeight="Bold" HorizontalAlignment="Center" VerticalAlignment="Bottom">Real Time</Label>
                <GroupBox Header="Authorization" Grid.Row="3" Grid.Column="1" Foreground="Blue">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition  Height="Auto"/>
                <RowDefinition  Height="2"/>
                <RowDefinition  Height="Auto"/>
                <RowDefinition  Height="2"/>
                <RowDefinition  Height="Auto"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition  Width="Auto"/>
                <ColumnDefinition  Width="2"/>
                <ColumnDefinition  Width="Auto"/>
                <ColumnDefinition  Width="Auto"/>
            </Grid.ColumnDefinitions>

            <Label Grid.Row="0" Grid.Column="0">Username:</Label>
            <TextBox x:Name="RTDemail" Width="100" Grid.Row="0" Grid.Column="2" Text="{Binding RTDUserName, UpdateSourceTrigger=PropertyChanged}"></TextBox>

            <Label Grid.Row="2" Grid.Column="0">Password:</Label>
            <PasswordBox x:Name="RTDpwd" ToolTip="Password" PasswordChar="*"                             
                         Width="100" Grid.Row="2" Grid.Column="2" KeyDown="OnKeyDownHandler"></PasswordBox>
            <Label Grid.Row="2" Grid.Column="3">
                <Hyperlink Click="HyperlinkClickRTD">Forgot Password?</Hyperlink>
            </Label>
            <CheckBox Margin="10 0 0 0" Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2" x:Name="RTDsaveauth" IsChecked="{Binding RTDSaveAuth}">Save username and password</CheckBox>
            <Button Margin="10 0 0 0" Grid.Row="4" Grid.Column="3" Click="RTDVerifyButtonClick">OK</Button>
        </Grid>
    </GroupBox>
    <GroupBox Grid.Row="5" Grid.ColumnSpan="3">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="10"></ColumnDefinition>
                <ColumnDefinition Width="Auto"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Label Grid.Column="1" Margin="0 0 0 0" Foreground="Blue">Server</Label>
            <Label Name="RTDServerURLLabel" Grid.Column="2" Content="{Binding RTDServerURL}"></Label>
        </Grid>
    </GroupBox>
    <GroupBox Header="Status" Grid.Row="7" Grid.Column="1" Foreground="Blue">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition  Height="2"/>
                <RowDefinition  Height="Auto"/>
                <RowDefinition  Height="2"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition  Width="80"/>
                <ColumnDefinition  Width="20"/>
                <ColumnDefinition  Width="Auto"/>
            </Grid.ColumnDefinitions>
            <Label Grid.Row ="1" Grid.Column="0">Real Time</Label>
            <Image Grid.Row ="1" Grid.Column="1" 
                    Source="{Binding ElementName=RTDStatusLabel, Path=Content, Converter={StaticResource string2Image}}"
                   ToolTip="Connection Status" HorizontalAlignment="Left" Width="20"></Image>
            <Label Name="RTDStatusLabel" Grid.Row="1" Grid.Column="2" Content="{Binding RTDConnectionStatus, UpdateSourceTrigger=PropertyChanged}"></Label>
        </Grid>
    </GroupBox>
    </Grid>
    </GroupBox>        
    <Grid Grid.Row="5" Grid.Column="0" Grid.ColumnSpan="3" >
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"></ColumnDefinition>
                <ColumnDefinition Width="60"></ColumnDefinition>
                <ColumnDefinition Width="20"></ColumnDefinition>
        </Grid.ColumnDefinitions>
            <Button x:Name="CloseButton" Click="CloseButtonClick" Grid.Column ="1" Margin="0,0,0,0">Close</Button>
        </Grid>                  
</Grid>

当单击功能区时,代码将被执行。ConnectionSetup是上面的XAML文件。
private bool GetSettings()
{
    var settingUI = new ConnectionSetup();
    settingUI.DataContext = Settings;

    CenterMainWindow(settingUI, wndHandle.Handle);

    bool? result = settingUI.ShowDialog();
    return result ?? false;
}

    private void CenterMainWindow(System.Windows.Window window, IntPtr ownerHandle)
    {
        var helper = new System.Windows.Interop.WindowInteropHelper(window) {Owner = ownerHandle};

        // Center window
        // Note - Need to use HwndSource to get handle to WPF owned window,
        //        and the handle only exists when SourceInitialized has been
        //        raised

        window.SourceInitialized += delegate
                                        {
                                            // Get WPF size and location for non-WPF owner window
                                            var nonWPFOwnerLeft = XLApp.Left;
                                            var nonWPFOwnerWidth = XLApp.Width;
                                            var nonWPFOwnerTop = XLApp.Top;
                                            var nonWPFOwnerHeight = XLApp.Height;

                                            // Get transform matrix to transform non-WPF owner window
                                            // size and location units into device-independent WPF
                                            // size and location units
                                            var source = System.Windows.Interop.HwndSource.FromHwnd(helper.Handle);
                                            if (source != null && source.CompositionTarget != null)
                                            {
                                                var matrix = source.CompositionTarget.TransformFromDevice;
                                                var ownerWPFSize =
                                                    matrix.Transform(new System.Windows.Point(nonWPFOwnerWidth,
                                                                                              nonWPFOwnerHeight));
                                                var ownerWPFPosition =
                                                    matrix.Transform(new System.Windows.Point(nonWPFOwnerLeft,
                                                                                              nonWPFOwnerTop));

                                                // Center WPF window
                                                window.WindowStartupLocation = WindowStartupLocation.Manual;
                                                window.Left = Math.Max(0,
                                                                       ownerWPFPosition.X +
                                                                       (ownerWPFSize.X - window.Width)/2);
                                                window.Top = Math.Max(0,
                                                                      ownerWPFPosition.Y +
                                                                      (ownerWPFSize.Y - window.Height)/2);
                                            }
                                        };
    }

错误可能来自于 .cs 代码后台文件。您能否仅发布被单击的按钮的 xaml 和 codebehind(以及它调用的任何函数/方法)? - Alain
只是指出一下,.cctor 方法实际上是静态构造函数。 - Kendall Frey
你能试着将xaml的所有内容注释掉,然后查看是否仍有异常吗?我建议从Converter开始。 - Vincent Hubert
谢谢,这是客户端的问题。我的那边没问题。在客户端放置调试代码需要得到客户的许可和老板的批准,这不是一个快速简单的过程。 - toosensitive
3个回答

4
根据您发布的错误堆栈来看,似乎您的设置窗口上可能有一个链接。不幸的是,该链接中有一个无效的url,导致创建窗口失败。
编辑: 仔细查看您的代码和错误消息后,很明显我在错误的地方寻找。一些搜索发现bug存在于wpf中。
简而言之:目标机器上windir环境变量可能设置不正确,导致字体子系统出现故障。解决方法是修复环境变量,可以在目标机器的注册表中进行修复,或者在启动时添加类似以下内容的代码。
Environment.SetEnvironmentVariable("windir", Environment.GetEnvironmentVariable("SystemRoot"));

谢谢。在窗口上,没有直接的链接。有一个绑定到某个属性的链接。 - toosensitive
HyperlinkClickRTD或HyperlinkClick是属性,而不是方法?那甚至不应该编译。 - Jon
@toosensitive 在错误的区域查找了。根据新信息更新了答案。 - Jon
很好:http://connect.microsoft.com/VisualStudio/feedback/details/618027/uriformatexception-thrown-by-ms-internal-fontcache-util - BendEg

3

我们有几个人遇到了这个问题。在这里描述的解决方案(由于PATH大于2048个字符而引起)解决了这个问题。


1
我担心上述建议可能在以太过多或过少的权限运行的代码中存在问题。当然,在我的工作站上以管理员身份运行的应用程序对于这个调用Environment.SetEnvironmentVariable("windir", Environment.GetEnvironmentVariable("SystemRoot"))没有任何问题;但是我不确定SetEnvironmentVariable是否会在锁定的操作系统上真正起作用。有人尝试过a)真实操作b)在低特权环境下尝试吗?
此外,我在安装具有此调用的应用程序并运行一次后看到了一些奇怪的启动项。所有其他应用程序都无法再看到windir,它不再扩展为C:\Windows。我将在没有它的情况下进行整个安装和运行,看看TypeInvocation异常是否重新出现,如果没有,操作系统windir变量是否保持不变。
更新:
我已经确认我注意到的不良行为是由于没有指定SetEnvironment调用的目标所致。使用以下内容似乎可以缓解我的问题:
System.Environment.SetEnvironmentVariable("windir", System.Environment.GetEnvironmentVariable("SystemRoot"), EnvironmentVariableTarget.User);

当我没有设置目标时,我的Windows会话发生了非常奇怪的事情,要求我注销并登录,甚至重新启动。因此,除非这个更改确实是在机器或进程级别上进行的,否则不要假定默认设置会产生良好的结果。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接