WPF窗口抛出在启动TypeInitializationException [英] WPF window throws TypeInitializationException at start up

查看:731
本文介绍了WPF窗口抛出在启动TypeInitializationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 2012-04-09 09:59:50161 [1] ERROR助手[(NULL)]  - 名称:TypeInitializationException 
留言:对于System.Windows.Window'的类型初始值引发了异常。
目标:虚空.ctor()
堆栈:在MyShared.View.ConnectionSetup..ctor在MyAddIn()
在System.Windows.Window..ctor()
。 Connect.GetSettings()
在MyAddIn.Connect.BtnClick(IRibbonControl控制)

名称:TypeInitializationException
消息:System.Windows.FrameworkElement'的类型初始值引发了异常。
目标:虚空.cctor()
堆栈:在System.Windows.Window..cctor()

名称:TypeInitializationException
消息:的类型初始值System.Windows.Documents.TextElement引发了异常。
目标:虚空.cctor()
堆栈:在System.Windows.FrameworkElement..cctor()

名称:TypeInitializationException
消息:的类型初始值MS.Internal.FontCache.Util引发了异常。
目标:的Int32 get_Dpi()
堆栈:在MS.Internal.FontCache.Util.get_Dpi()
在System.Windows.SystemFonts.ConvertFontHeight(的Int32高度)
。在系统.Windows.Documents.TextElement..cctor()

名称:UriFormatException
消息:无效的URI:URI的格式无法确定。
目标:虚空CreateThis(System.String,布尔,System.UriKind)
堆栈:在System.Uri.CreateThis(字符串的uri,布尔dontEscape,UriKind uriKind)
在的System.Uri。 .ctor(字符串uriString中,uriKind uriKind)
在MS.Internal.FontCache.Util..cctor()

编辑,这里是XAML代码

 <窗口x:类=MyShared.View.ConnectionSetup
的xmlns =HTTP ://schemas.microsoft.com/winfx/2006/xaml/presentation
的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml
的xmlns:工具= http://schemas.microsoft.com/wpf/2008/toolkit
的xmlns:转换器=CLR的命名空间:MIMICShared.Converter
WindowStartupLocation =CenterOwner
标题=设置WIDTH =446HEIGHT =650闭幕=的windowClosing
DataContextChanged仅=UserControlDataContextChanged前景=黑粗细=正常的Horizo​​ntalAlignment =拉伸>
< Window.Resources>
<&ResourceDictionary中GT;
<转换器:Status2ImageConverter X:键=string2Image/>
< / ResourceDictionary的>
< /Window.Resources>

<电网FocusManager.FocusedElement ={绑定的ElementName =的uname}>
< Grid.RowDefinitions>
将; RowDefinition高度=0>&下; / RowDefinition>
将; RowDefinition高度=*>&下; / RowDefinition>
将; RowDefinition高度=0>&下; / RowDefinition>
< RowDefinition高度=自动>< / RowDefinition>
将; RowDefinition高度=0>&下; / RowDefinition>
< RowDefinition高度=自动>< / RowDefinition>
< RowDefinition HEIGHT =5>< / RowDefinition>
< /Grid.RowDefinitions>
<分组框中Grid.Row =1前景=蓝>
<网格和GT;
< Grid.ColumnDefinitions>
< ColumnDefinition WIDTH =5>< / ColumnDefinition>
将; ColumnDefinition宽度=*>&下; / ColumnDefinition>
< ColumnDefinition WIDTH =*/>
< ColumnDefinition WIDTH =5>< / ColumnDefinition>
< /Grid.ColumnDefinitions>
< Grid.RowDefinitions>
将; RowDefinition高度=2>&下; / RowDefinition>
将; RowDefinition高度=30>&下; / RowDefinition>
将; RowDefinition高度=2>&下; / RowDefinition>
将; RowDefinition高度=*>&下; / RowDefinition>
将; RowDefinition高度=2>&下; / RowDefinition>
< RowDefinition高度=自动>< / RowDefinition>
将; RowDefinition高度=2>&下; / RowDefinition>
< RowDefinition高度=自动>< / RowDefinition>
将; RowDefinition高度=2>&下; / RowDefinition>
< RowDefinition高度=自动>< / RowDefinition>
将; RowDefinition高度=2>&下; / RowDefinition>
< /Grid.RowDefinitions>
<标签Grid.Row =1Grid.ColumnSpan =4字号=14的Horizo​​ntalAlignment =中心粗细=大胆VerticalAlignment =底>历史< /标签>
<分组框中Grid.Column =1Grid.Row =3标题=连接前景=蓝Grid.ColumnSpan =2>
<网格和GT;
< Grid.RowDefinitions>
将; RowDefinition高度=*>&下; / RowDefinition>
< RowDefinition高度=自动>< / RowDefinition>
< /Grid.RowDefinitions>

<工具箱:DataGrid的X:名称=connectionlist保证金=10风格={DynamicResource DataGridStyleDefault}背景=白
的AutoGenerateColumns =FALSEHeadersVisibility =所有的
的SelectionMode =单
GridLinesVisibility =无
的ItemsSource ={结合SettingList}CanUserAddRows =FALSECanUserDeleteRows =真
SelectionUnit =FullRow 的SelectedItem ={结合的SelectedItem}
的SelectionChanged =ConnectionlistSelectionChanged
VerticalScrollBarVisibility =自动Horizo​​ntalScrollBarVisibility =已禁用>
<! - 列定义 - >
<工具箱:DataGrid.Columns>
<! - 泳池 - >
< - <工具箱:DataGridCheckBoxColumn标题=池WIDTH =40绑定={结合池}/> - >
<! - 连接信息 - >
<工具箱:DataGridTextColumn标题=连接信息WIDTH =*绑定={结合主机}/>
<工具箱:DataGridTextColumn标题=说明WIDTH =*绑定={结合递减}>
<工具箱:DataGridTextColumn.ElementStyle>
<风格的TargetType ={X:类型的TextBlock}>
< setter属性=TextWrappingVALUE =自动换行/>
< /样式和GT;
< /工具包:DataGridTextColumn.ElementStyle>
< /工具包:DataGridTextColumn>
< /工具包:DataGrid.Columns>
< /工具包:DataGrid的>

<电网Grid.Row =2的Horizo​​ntalAlignment =中心>
<网格和GT;
< Grid.ColumnDefinitions>
< ColumnDefinition WIDTH =自动>< / ColumnDefinition>
< ColumnDefinition WIDTH =自动>< / ColumnDefinition>
< /Grid.ColumnDefinitions>
<按钮保证金=2填充=10 0X:NAME =Add按钮点击=AddClicked>添加< /按钮>
<按钮Grid.Column =1保证金=2填充=10 0命令={结合删除}>删除< /按钮>
< /网格和GT;
< /网格和GT;
< /网格和GT;
< /分组框中>

<分组框标题=验证Grid.Row =5Grid.Column =1前景=蓝Grid.ColumnSpan =2>
<网格和GT;
< Grid.RowDefinitions>
< RowDefinition高度=自动/>
< RowDefinition高度=2/>
< RowDefinition高度=自动/>
< RowDefinition高度=2/>
< RowDefinition高度=自动/>
< /Grid.RowDefinitions>
< Grid.ColumnDefinitions>
< ColumnDefinition WIDTH =自动/>
< ColumnDefinition WIDTH =5/>
< ColumnDefinition WIDTH =自动/>
< ColumnDefinition WIDTH =自动/>
< /Grid.ColumnDefinitions>

<标签Grid.Row =0Grid.Column =0>用户名:其中p /标签>
<文本框X:名称=的unameWIDTH =100Grid.Row =0Grid.Column =2文本={结合用户名,UpdateSourceTrigger =的PropertyChanged}>< /文本框>

<标签Grid.Row =2Grid.Column =0>密码:LT; /标签>
< PasswordBox X:NAME =PWD工具提示=密码PasswordChar =*
WIDTH =100Grid.Row =2Grid.Column =2的KeyDown = OnKeyDownHandler>< / PasswordBox>
<标签Grid.Row =2Grid.Column =3>
<超链接点击=HyperlinkClick>忘记密码<?/超链接>
< /标签>
将;复选框保证金=10 0 0 0Grid.Row =4Grid.Column =0Grid.ColumnSpan =2×:名称=saveauth器isChecked ={结合SaveAuth} >保存用户名和密码< /复选框>
<按钮保证金=10 0 0 0Grid.Row =4Grid.Column =3点击=VerifyButtonClick>确定< /按钮>
< /网格和GT;
< /分组框中>
<分组框标题=状态Grid.Row =7Grid.Column =1前景=蓝Grid.ColumnSpan =2>
<网格和GT;
< Grid.RowDefinitions>
< RowDefinition高度=0/>
< RowDefinition高度=自动/>
< RowDefinition高度=0/>
< /Grid.RowDefinitions>
< Grid.ColumnDefinitions>
< ColumnDefinition WIDTH =80/>
< ColumnDefinition宽度=20/>
< ColumnDefinition WIDTH =自动/>
< /Grid.ColumnDefinitions>
<标签Grid.Row =1Grid.Column =0>历史及LT; /标签>
<图像Grid.Row =1Grid.Column =1
来源={绑定的ElementName = historicalStatusLabel,路径=内容,转换器= {StaticResource的string2Image}}
工具提示=连接状态>< /图像>
<标签名称=historicalStatusLabelGrid.Row =1Grid.Column =2CONTENT ={结合ConnectionStatus,UpdateSourceTrigger =的PropertyChanged}/>
< /网格和GT;
< /分组框中>
< /网格和GT;
< /分组框中>
<分组框中Grid.Row =3Grid.Column =1前景=蓝>
<网格和GT;
< Grid.RowDefinitions>
将; RowDefinition高度=0>&下; / RowDefinition>
将; RowDefinition高度=30>&下; / RowDefinition>
将; RowDefinition高度=0>&下; / RowDefinition>
< RowDefinition高度=自动>< / RowDefinition>
将; RowDefinition高度=0>&下; / RowDefinition>
< RowDefinition高度=自动>< / RowDefinition>
将; RowDefinition高度=0>&下; / RowDefinition>
< RowDefinition高度=自动>< / RowDefinition>
将; RowDefinition高度=0>&下; / RowDefinition>
< /Grid.RowDefinitions>
<标签Grid.Row =1字号=14粗细=大胆的Horizo​​ntalAlignment =中心VerticalAlignment =底>实时< /标签>
<分组框标题=授权Grid.Row =3Grid.Column =1前景=蓝>
<网格和GT;
< Grid.RowDefinitions>
< RowDefinition高度=自动/>
< RowDefinition高度=2/>
< RowDefinition高度=自动/>
< RowDefinition高度=2/>
< RowDefinition高度=自动/>
< /Grid.RowDefinitions>
< Grid.ColumnDefinitions>
< ColumnDefinition WIDTH =自动/>
< ColumnDefinition WIDTH =2/>
< ColumnDefinition WIDTH =自动/>
< ColumnDefinition WIDTH =自动/>
< /Grid.ColumnDefinitions>

<标签Grid.Row =0Grid.Column =0>用户名:其中p /标签>
<文本框X:NAME =RTDemailWIDTH =100Grid.Row =0Grid.Column =2文本={结合RTDUserName,UpdateSourceTrigger =的PropertyChanged}>< /文本框>

<标签Grid.Row =2Grid.Column =0>密码:LT; /标签>
< PasswordBox X:NAME =RTDpwd工具提示=密码PasswordChar =*
WIDTH =100Grid.Row =2Grid.Column =2的KeyDown = OnKeyDownHandler>< / PasswordBox>
<标签Grid.Row =2Grid.Column =3>
<超链接点击=HyperlinkClickRTD>忘记密码<?/超链接>
< /标签>
将;复选框保证金=10 0 0 0Grid.Row =4Grid.Column =0Grid.ColumnSpan =2×:名称=RTDsaveauth器isChecked ={结合RTDSaveAuth} >保存用户名和密码< /复选框>
<按钮保证金=10 0 0 0Grid.Row =4Grid.Column =3点击=RTDVerifyButtonClick>确定< /按钮>
< /网格和GT;
< /分组框中>
<分组框中Grid.Row =5Grid.ColumnSpan =3>
<网格和GT;
< Grid.ColumnDefinitions>
将; ColumnDefinition宽度=10>&下; / ColumnDefinition>
< ColumnDefinition WIDTH =自动>< / ColumnDefinition>
将; ColumnDefinition宽度=*>&下; / ColumnDefinition>
< /Grid.ColumnDefinitions>
<标签Grid.Column =1保证金=0 0 0 0前景=蓝>服务器16; /标签>
<标签名称=RTDServerURLLabelGrid.Column =2CONTENT ={结合RTDServerURL}>< /标签>
< /网格和GT;
< /分组框中>
<分组框标题=状态Grid.Row =7Grid.Column =1前景=蓝>
<网格和GT;
< Grid.RowDefinitions>
< RowDefinition高度=2/>
< RowDefinition高度=自动/>
< RowDefinition高度=2/>
< /Grid.RowDefinitions>
< Grid.ColumnDefinitions>
< ColumnDefinition WIDTH =80/>
< ColumnDefinition宽度=20/>
< ColumnDefinition WIDTH =自动/>
< /Grid.ColumnDefinitions>
<标签Grid.Row =1Grid.Column =0>实时< /标签>
<图像Grid.Row =1Grid.Column =1
来源={绑定的ElementName = RTDStatusLabel,路径=内容,转换器= {StaticResource的string2Image}}
工具提示=连接状态的Horizo​​ntalAlignment =左WIDTH =20>< /图像>
<标签名称=RTDStatusLabelGrid.Row =1Grid.Column =2CONTENT ={结合RTDConnectionStatus,UpdateSourceTrigger =的PropertyChanged}>< /标签>
< /网格和GT;
< /分组框中>
< /网格和GT;
< /分组框中>
将;网格Grid.Row =5Grid.Column =0Grid.ColumnSpan =3>
< Grid.ColumnDefinitions>
将; ColumnDefinition宽度=*>&下; / ColumnDefinition>
< ColumnDefinition WIDTH =60>< / ColumnDefinition>
< ColumnDefinition WIDTH =20>< / ColumnDefinition>
< /Grid.ColumnDefinitions>
<按钮X:NAME =CloseButton点击=CloseButtonClickGrid.Column =1保证金=0,0,0,0>关闭< /按钮>
< /网格和GT;
< /网格和GT;





当点击丝带,代码被执行,ConnectionSetup高于

 私人布尔的getSettings()
{
变种settingUI XAML文件=新ConnectionSetup();
settingUI.DataContext =设置;

CenterMainWindow(settingUI,wndHandle.Handle);

布尔?结果= settingUI.ShowDialog();
返回结果?假;
}

私人无效CenterMainWindow(System.Windows.Window窗口,IntPtr的ownerHandle)
{
变种帮手=新System.Windows.Interop.WindowInteropHelper(窗口) {业主= ownerHandle};

//中心窗口
//注 - 需要使用HwndSource得到处理对WPF资窗口,
//只有当SourceInitialized已经$ B $存在手柄b //上调

window.SourceInitialized + =代表
{
//获取非WPF所有者窗口
VAR WPF的大小和位置nonWPFOwnerLeft = XLApp.Left ;
VAR nonWPFOwnerWidth = XLApp.Width;
VAR nonWPFOwnerTop = XLApp.Top;
VAR nonWPFOwnerHeight = XLApp.Height;

//获取变换矩阵非WPF所有者窗口
//的大小和位置的单位转换成设备无关WPF
//的大小和位置的单位
变种来源= System.Windows.Interop.HwndSource.FromHwnd(helper.Handle);
如果(来源= NULL&放大器;!&安培;!source.CompositionTarget = NULL)
{
VAR矩阵= source.CompositionTarget.TransformFromDevice;
变种ownerWPFSize =
matrix.Transform(新System.Windows.Point(nonWPFOwnerWidth,
nonWPFOwnerHeight));
变种ownerWPFPosition =
matrix.Transform(新System.Windows.Point(nonWPFOwnerLeft,
nonWPFOwnerTop));

//中心WPF窗口
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);
}
};
}


解决方案

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



编辑:
已经更加紧密地看着你的代码和错误消息,很明显,我是在找错了地方。一些搜索变成了一个错误 WPF 。



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

  Environment.SetEnvironmentVariable(WINDIR,Environment.GetEnvironmentVariable(SYSTEMROOT)); 


I have an Excel AddIn with several ribbons, one ribbon is setting window. When it is clicked, a custom window will show up. However, there is nothing to show up when clicked. I saw the following exceptions in log file. What causes this and how to fix it? thanks a lot

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()

Edit, here is xaml code

<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>

When ribbon is clicked, the code is executed, ConnectionSetup is the xaml file above

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

解决方案

Judging from the error stack you posted, it appears that your settings window probably has a link on it. Unfortunately, the link has an invalid url in it, which causes the creation of the window to fail.

EDIT: Having looked more closely at your code and error messages, it's clear that I was looking in the wrong area. Some searching turned up a bug in wpf.

The short version: the windir environment variable may be set incorrectly on the target machine, which causes the font subsytem to break. The workaround is to fix the environment variable, either on the target machine's registry, or in the code by adding something like the following to the startup:

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

这篇关于WPF窗口抛出在启动TypeInitializationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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