InitializeComponent() 无法根据系统语言定位资源
InitializeComponent() Cannot locate resource based on system language
该应用程序是在 运行 windows10 机器上开发的,系统语言设置为我们当地的语言,丹麦语。现在我们正处于需要在其他国家的办公室使用该软件的阶段。
但是,视图在这些机器上启动时崩溃,而且我发现如果我在自己的机器上更改系统语言,它也不会为我工作。
这是为什么,我们搞不清楚。
来自 InitializeComponent 的错误消息:
System.IO.IOException: 'Cannot locate resource 'views/main/mainview.xaml'.'
堆栈跟踪:
at MS.Internal.AppModel.ResourcePart.GetStreamCore(FileMode mode,
FileAccess access) at
System.IO.Packaging.PackagePart.GetStream(FileMode mode, FileAccess
access) at System.IO.Packaging.PackagePart.GetStream() at
System.Windows.Application.LoadComponent(Object component, Uri
resourceLocator) at
InfoCenter.Views.Main.MainView.InitializeComponent() in
D:\Workspace\Visual Studio\MT Info Center WPF\Info
Center\Views\Main\MainView.xaml:line 1 at
InfoCenter.Views.Main.MainView..ctor() in D:\Workspace\Visual
Studio\MT Info Center WPF\Info Center\Views\Main\MainView.xaml.cs:line
21 at InfoCenter.App.OnStartup(StartupEventArgs e) in
D:\Workspace\Visual Studio\MT Info Center WPF\Info
Center\App.xaml.cs:line 57 at
System.Windows.Application.<.ctor>b__1_0(Object unused) at
System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate
callback, Object args, Int32 numArgs) at
System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source,
Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.DispatcherOperation.InvokeImpl() at
System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object
state) at
System.Threading.ExecutionContext.RunInternal(ExecutionContext
executionContext, ContextCallback callback, Object state, Boolean
preserveSyncCtx) at
System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state, Boolean
preserveSyncCtx) at
System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state) at
MS.Internal.CulturePreservingExecutionContext.Run(CulturePreservingExecutionContext
executionContext, ContextCallback callback, Object state) 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& handled) at
MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam,
IntPtr lParam, Boolean& handled) at
MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) at
System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate
callback, Object args, Int32 numArgs) at
System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source,
Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
at
System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority
priority, TimeSpan timeout, Delegate method, Object args, Int32
numArgs) at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd,
Int32 msg, IntPtr wParam, IntPtr lParam) at
MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) at
System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame
frame) at
System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
at System.Windows.Application.RunDispatcher(Object ignore) at
System.Windows.Application.RunInternal(Window window) at
System.Windows.Application.Run(Window window) at
System.Windows.Application.Run() at InfoCenter.App.Main() in
D:\Workspace\Visual Studio\MT Info Center WPF\Info
Center\obj\Debug\App.g.cs:line 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 模式。
如果我遗漏了一些信息,请随时询问!
这是关于文化信息的问题,因为您已经在资源文件中添加了这一行
<UICulture>en-US</UICulture>
按照 this post 你应该能够解决你的问题取消注释这一行
[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]
在你的assembly.info.cs
该应用程序是在 运行 windows10 机器上开发的,系统语言设置为我们当地的语言,丹麦语。现在我们正处于需要在其他国家的办公室使用该软件的阶段。 但是,视图在这些机器上启动时崩溃,而且我发现如果我在自己的机器上更改系统语言,它也不会为我工作。 这是为什么,我们搞不清楚。
来自 InitializeComponent 的错误消息:
System.IO.IOException: 'Cannot locate resource 'views/main/mainview.xaml'.'
堆栈跟踪:
at MS.Internal.AppModel.ResourcePart.GetStreamCore(FileMode mode, FileAccess access) at System.IO.Packaging.PackagePart.GetStream(FileMode mode, FileAccess access) at System.IO.Packaging.PackagePart.GetStream() at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator) at InfoCenter.Views.Main.MainView.InitializeComponent() in D:\Workspace\Visual Studio\MT Info Center WPF\Info Center\Views\Main\MainView.xaml:line 1 at InfoCenter.Views.Main.MainView..ctor() in D:\Workspace\Visual Studio\MT Info Center WPF\Info Center\Views\Main\MainView.xaml.cs:line 21 at InfoCenter.App.OnStartup(StartupEventArgs e) in D:\Workspace\Visual Studio\MT Info Center WPF\Info Center\App.xaml.cs:line 57 at System.Windows.Application.<.ctor>b__1_0(Object unused) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler) at System.Windows.Threading.DispatcherOperation.InvokeImpl() at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at MS.Internal.CulturePreservingExecutionContext.Run(CulturePreservingExecutionContext executionContext, ContextCallback callback, Object state) 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& handled) at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler) at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs) at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam) at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame) at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
at System.Windows.Application.RunDispatcher(Object ignore) at System.Windows.Application.RunInternal(Window window) at System.Windows.Application.Run(Window window) at System.Windows.Application.Run() at InfoCenter.App.Main() in D:\Workspace\Visual Studio\MT Info Center WPF\Info Center\obj\Debug\App.g.cs:line 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 模式。
如果我遗漏了一些信息,请随时询问!
这是关于文化信息的问题,因为您已经在资源文件中添加了这一行
<UICulture>en-US</UICulture>
按照 this post 你应该能够解决你的问题取消注释这一行
[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]
在你的assembly.info.cs