-->

WPF窗口抛出在启动时TypeInitializationException(WPF window

2019-06-23 12:16发布

我有一个Excel加载项与几个丝带,一个色带设置窗口。 当它被点击,自定义窗口会显示出来。 然而,没有什么点击时显示出来。 我看到在日志文件中的下列情况除外。 是什么原因导致这个问题以及如何解决它? 非常感谢

2012-04-09 09:59:50,161 [1] ERROR Helper [(null)] - Name:TypeInitializationException
Message:The type initializer for 'System.Windows.Window' threw an exception.
Target:Void .ctor()
Stack:   at System.Windows.Window..ctor()
   at MyShared.View.ConnectionSetup..ctor()
   at MyAddIn.Connect.GetSettings()
   at MyAddIn.Connect.BtnClick(IRibbonControl control)

Name:TypeInitializationException
Message:The type initializer for 'System.Windows.FrameworkElement' threw an exception.
Target:Void .cctor()
Stack:   at System.Windows.Window..cctor()

Name:TypeInitializationException
Message:The type initializer for 'System.Windows.Documents.TextElement' threw an exception.
Target:Void .cctor()
Stack:   at System.Windows.FrameworkElement..cctor()

Name:TypeInitializationException
Message:The type initializer for 'MS.Internal.FontCache.Util' threw an exception.
Target:Int32 get_Dpi()
Stack:   at MS.Internal.FontCache.Util.get_Dpi()
   at System.Windows.SystemFonts.ConvertFontHeight(Int32 height)
   at System.Windows.Documents.TextElement..cctor()

Name:UriFormatException
Message:Invalid URI: The format of the URI could not be determined.
Target:Void CreateThis(System.String, Boolean, System.UriKind)
Stack:   at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind)
   at System.Uri..ctor(String uriString, UriKind uriKind)
   at MS.Internal.FontCache.Util..cctor()

编辑,这里是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);
                                            }
                                        };
    }

Answer 1:

从您发布的错误堆栈来看,似乎你的设置窗口中可能有它的链接。 不幸的是,链接中有一个无效的URL,这将导致窗口创建失败。

编辑:你的代码和错误消息说完看着更加紧密,很明显,我一直在寻找在错误的区域。 某些搜索打开了一个错误的WPF 。

短版本:WINDIR环境变量可以在目标计算机,这将导致字体subsytem打破上设置不正确。 解决方法是通过添加类似以下的启动修复环境变量,无论是在目标机器的注册表,或代码:

Environment.SetEnvironmentVariable("windir", Environment.GetEnvironmentVariable("SystemRoot"));


Answer 2:

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



Answer 3:

我很担心,上述建议的声明可能是在与过多或过少的权限运行的代码有问题。 当然,我的应用我的工作站上运行我的管理员有这个电话转给Environment.SetEnvironmentVariable(“WINDIR”,Environment.GetEnvironmentVariable(“SYSTEMROOT”))没有问题;

但我不知道SetEnvironmentVariable方法实际上将在锁定的OS工作。 有没有人一)尝试这是真的和B)试图在低特权环境>

另外,我看到在应用程序启动时有些古怪我安装使用该调用应用程序后,一旦运行它。 所有其他应用程序不再能看到WINDIR,未扩展到C:\ Windows操作系统。 我要遍历整个安装和不到位运行,查看是否不TypeInvocation例外复出如果还是不行,在OS WINDIR变量是否保持不变。

更新:

我已经证实,不良行为我注意到的是,由于没有指定SetEnvironment调用的目标。 使用下面的我的问题似乎有所缓解:

System.Environment.SetEnvironmentVariable("windir", System.Environment.GetEnvironmentVariable("SystemRoot"), EnvironmentVariableTarget.User);

当我没有设定目标,很奇怪的事情发生在我的Windows会话要求我要么注销和登录,甚至重新启动。 因此,除非这种变化是真的要在机器或工艺水平,不承担与默认的幸福结果



文章来源: WPF window throws TypeInitializationException at start up