TypeInitializationException으로 인해 WPF 응용 프로그램 시작 시 실패

StackOverflow https://stackoverflow.com/questions/58425

  •  09-06-2019
  •  | 
  •  

문제

시작하려고 하는 간단한 WPF 응용 프로그램이 있습니다.저는 Microsoft Patterns and Practices "WPF용 복합 응용 프로그램 지침"을 따르고 있습니다.해당 지침을 따랐지만 내 WPF 응용 프로그램은 "TypeInitializationException"으로 인해 즉시 실패합니다.

InnerException 속성은 "'System.Windows.Navigation.BaseUriHelper'에 대한 형식 이니셜라이저에서 예외가 발생했습니다."를 나타냅니다.

내 app.xaml은 다음과 같습니다.

<Application x:Class="MyNamespace.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Application.Resources>         
    </Application.Resources>
</Application>

그리고 여기 내 app.xaml.cs가 있습니다("public App()"에서 예외가 발생함).

public partial class App : Application
{
    public App()
    {
        Bootstrapper bootStrapper = new Bootstrapper();
        bootStrapper.Run();
    }
}

프로젝트의 시작 개체로 "App" 클래스를 설정했습니다.

무엇이 잘못되고 있습니까?

도움이 되었습니까?

해결책

@ima에게 감사드립니다. 귀하의 답변이 올바른 방향을 가리켰습니다.나는 app.config 파일을 사용하고 있었고 여기에는 다음이 포함되었습니다.

<configuration>
  <startup>
    <supportedRuntime version="v2.0.50727" sku="Client"/>
  </startup>
  <configSections>
    <section name="modules" type="Microsoft.Practices.Composite.Modularity.ModulesConfigurationSection, Microsoft.Practices.Composite"/>
  </configSections>
  <modules>
    <module assemblyFile="Modules/MyNamespace.Modules.ModuleName.dll" moduleType="MyNamespace.Modules.ModuleName.ModuleClass" moduleName="Name"/>
  </modules>
</configuration>

문제는 <startup> 요소인 것 같습니다. 제거했을 때 애플리케이션이 제대로 실행되었기 때문입니다.3.5 SP1에서 사용할 수 있는 "클라이언트 프로필"을 활용하기 위해 확인란을 선택했을 때 Visual Studio 2008에서 이를 추가했기 때문에 혼란스러웠습니다.

상자를 선택하고 선택 취소하는 것에 대해 약간의 고민 끝에 다음과 같은 구성 파일이 생겼습니다.

<configuration>
  <configSections>
    <section name="modules" type="Microsoft.Practices.Composite.Modularity.ModulesConfigurationSection, Microsoft.Practices.Composite"/>
  </configSections>
  <modules>
    <module assemblyFile="Modules/MyNamespace.Modules.ModuleName.dll" moduleType="MyNamespace.Modules.ModuleName.ModuleClass" moduleName="Name"/>
  </modules>
  <startup>
    <supportedRuntime version="v2.0.50727" sku="Client"/>
  </startup>
</configuration>

작동합니다!

app.config의 요소 순서가 왜 중요한지는 잘 모르겠지만 그런 것 같습니다.

다른 팁

에 문제가 있는 경우 App.config 파일에 오타 등의 오류가 발생할 수 있습니다. * 줄 끝에서, 예를 들어 ...</startup> 줄 끝에 추가 "*"가 있습니다 ...</startup>*.

.config 파일을 사용하시나요?그렇다면 오류가 있는지 확인하세요.이러한 종류의 초기화 오류는 잘못된 XML로 인해 발생하는 경우가 많습니다.XAML에 오류가 없으면 XML 구성을 가장 먼저 살펴보아야 합니다.

InnerExceptions를 자세히 추적하면 다음 오류를 발견할 수 있습니다.

"Only one <configSections> element allowed per config file and if present must be the first child of the root <configuration> element"

이 순서 변경은 Visual Studio EntityFramework 마법사가 맨 위에 ConnectionStrings 요소를 추가한 후에 발생했습니다.

이유 없이 또는 무엇이 잘못되었는지에 대한 세부 정보 없이 TypeInitializationException만 표시되는 경우 Visual Studio 옵션에서 내 코드만을 사용하지 않도록 설정하세요.

저는 다른 애플리케이션의 앱 설정을 내 app.config의 "userSettings"라는 새 섹션에 복사했습니다.그러나 "userSettings"를 정의하는 app.config에 "configSections"도 추가되어야 합니다.userSettings 섹션을 삭제한 다음 앱 설정을 편집하고 저장했습니다.VS는 올바른 "userSettings" 및 "configSections"가 없는 경우 자동으로 생성합니다.

"모듈"이라는 두 개의 섹션이 있습니다.두 모듈 정의를 "modules"라는 하나의 섹션에 배치합니다.

나는 비슷한 상황에 직면했다.일주일 동안 검색한 후 해결책을 찾았고 정말 효과가 있었습니다.동일한 문제로 인해 발생하는 2-3개의 문제를 해결했습니다.

다음과 같이하세요:레지스트리에서 WPF 키(부재)를 확인합니다.hkey_local_machine 소프트웨어 microsoft net 프레임 워크 설정 ndp v3.0 setup windows 프레젠테이션 기초 내 문제는 위에서 언급 한 키가 없기 때문입니다.

레지스트리에서 다음 세부 정보를 수정하고 사용할 수 있습니다.(실제로는 파일로 저장하고 레지스트리로 불러올 수 있습니다)

hkey_local_machine 소프트웨어 microsoft net 프레임 워크 설정 ndp v3.0 setup wind 프로그램 파일 참조 어셈블리 microsoft framework v3.0 ""wpfcommonassembliespathx86 "="c : wind ""installSuccess "= dword : 00000001"productversion "="3.0.6920.1453 ""wpfnonreferenceAssembliespathx86 "="c : windows microsoft.net framework v3.0 wpf "

나는 그것이 효과가 있다고 확신합니다.

모두 제일 좋다.

문안 인사,

우메쉬

내 경우에는 다음을 추가해야 합니다.

<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />

App.config의 섹션(VS 2015 .NET 4.5.2)

이전에 빌드된 WPF 프로젝트를 열고 빌드를 확인하고 괜찮으면 두 프로젝트에서 App.config를 확인하고 비교합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top