InitializeComponent()无法根据系统语言

时间:2017-12-14 09:03:29

标签: c# asp.net wpf mvvm initializecomponent

该应用程序是在运行Windows 10的计算机上开发的,系统语言设置为我们的本地语言,丹麦语。现在我们处于需要在其他国家/地区的办公室中使用该软件的阶段。 但是,视图在这些机器上启动时崩溃,我发现如果我在自己的机器上更改系统语言,它也不适用于我。 为什么会这样,我们无法弄清楚。

InitializeComponent的错误消息:

  

System.IO.IOException:'无法找到资源'views / main / mainview.xaml'。'

堆栈跟踪:

  

在MS.Internal.AppModel.ResourcePart.GetStreamCore(FileMode模式,   FileAccess访问)在   System.IO.Packaging.PackagePart.GetStream(FileMode模式,FileAccess   访问)在System.IO.Packaging.PackagePart.GetStream()处   System.Windows.Application.LoadComponent(Object component,Uri   resourceLocator)   InfoCenter.Views.Main.MainView.InitializeComponent()in   D:\ Workspace \ Visual Studio \ MT信息中心WPF \ Info   Center \ Views \ Main \ MainView.xaml:第1行   D:\ Workspace \ Visual中的InfoCenter.Views.Main.MainView..ctor()   Studio \ MT信息中心WPF \信息中心\视图\ Main \ MainView.xaml.cs:行   21在InfoCenter.App.OnStartup(StartupEventArgs e)中   D:\ Workspace \ Visual Studio \ MT信息中心WPF \ Info   Center \ App.xaml.cs:第57行   System.Windows.Application。< .ctor> b__1_0(Object unused)at   System.Windows.Threading.ExceptionWrapper.InternalRealCall(代表   回调,对象args,Int32 numArgs)at   System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source,   委托回调,Object args,Int32 numArgs,Delegate catchHandler)   在System.Windows.Threading.DispatcherOperation.InvokeImpl()at   System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(对象   国家)   System.Threading.ExecutionContext.RunInternal(执行上下文   executionContext,ContextCallback回调,对象状态,布尔值   preserveSyncCtx)at   System.Threading.ExecutionContext.Run(执行上下文   executionContext,ContextCallback回调,对象状态,布尔值   preserveSyncCtx)at   System.Threading.ExecutionContext.Run(执行上下文   executionContext,ContextCallback回调,对象状态)at   MS.Internal.CulturePreservingExecutionContext.Run(CulturePreservingExecutionContext   executionContext,ContextCallback回调,对象状态)at   System.Windows.Threading.DispatcherOperation.Invoke()at   System.Windows.Threading.Dispatcher.ProcessQueue()at   System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd,Int32   msg,IntPtr wParam,IntPtr lParam,Boolean&处理)   MS.Win32.HwndWrapper.WndProc(IntPtr hwnd,Int32 msg,IntPtr wParam,   IntPtr lParam,Boolean&处理)   MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)at   System.Windows.Threading.ExceptionWrapper.InternalRealCall(代表   回调,对象args,Int32 numArgs)at   System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source,   委托回调,Object args,Int32 numArgs,Delegate catchHandler)   在   System.Windows.Threading.Dispatcher.LegacyInvokeImpl(的DispatcherPriority   优先级,TimeSpan超时,委托方法,对象args,Int32   numArgs)在MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd,   Int32 msg,IntPtr wParam,IntPtr lParam)at   MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)at at   System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame   框架)   System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame框架)
  在System.Windows.Application.RunDispatcher(Object ignore)at   System.Windows.Application.RunInternal(窗口窗口)at   System.Windows.Application.Run(窗口窗口)at   InfoCenter.App.Main()中的System.Windows.Application.Run()   D:\ Workspace \ Visual Studio \ MT信息中心WPF \ Info   Center \ obj \ Debug \ App.g.cs:第51行

以下是一些代码:

的App.xaml

<!--ANCHOR: Updater-->
<updater:SingleInstanceApplication x:Class="InfoCenter.App"
                                   x:Name="App"
                                   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                                   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                                   xmlns:updater="clr-namespace:InfoCenter.Updater">
    <Application.Resources>
    </Application.Resources>
</updater:SingleInstanceApplication>

App.xaml.cs:

protected override void OnStartup(StartupEventArgs e)
{
    if (PreProcessor.Prod)
        Dispatcher.UnhandledException += OnDispatcherUnhandledException;

    base.OnStartup(e);

    AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);

    var app = new MainView { ShowInTaskbar = false, Topmost = true }
    var context = new MainViewModel();
    app.DataContext = context;
    app.Show();
}

其他信息: - 我们使用ClickOnce进行部署。 - 该应用程序有一些将继续运行的后台任务。看起来它只是在没有视图的情况下运行。 - 我们使用MVVM模式。

如果我遗漏了一些信息,请不要犹豫!

1 个答案:

答案 0 :(得分:3)

这是关于Culure Info的一个问题,因为您已在资源文件中添加了这一行

<UICulture>en-US</UICulture> 

关注this post您应该能够解决您的问题,取消注释此行

[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)] 
你的assembly.info.cs中的