该应用程序是在运行 Windows 10 的计算机上开发的,系统语言设置为我们的本地语言丹麦语。现在我们正处于需要在其他国家的办公室使用该软件的阶段。 然而,在这些机器上启动时 View 崩溃,而且我发现如果我在自己的机器上更改系统语言,它也不适用于我。 这是为什么,我们无法弄清楚。
来自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
下面是一些代码:
应用程序.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 进行部署。 - 该应用程序有一些后台任务将继续运行。看起来它只是在没有 View 的情况下运行。 - 我们使用MVVM模式。
如果我遗漏了一些信息,请随时询问!
最佳答案
这是关于文化信息的问题,因为您已在资源文件中添加了这一行
<UICulture>en-US</UICulture>
已关注 this post您应该能够解决您的问题取消注释此行
[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]
在你的 assembly.info.cs 中
关于c# - InitializeComponent() 无法根据系统语言定位资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47809479/