首页 > 解决方案 > '输入字符串的格式不正确。' 在使用程序集版本“1.0.0.*”时从 C# 中的 .NET 核心应用程序打开新窗口时

问题描述

我最近在 C# 中使用 Visual Studio 2019 创建了一个 .NET 核心 WPF 应用程序,并将程序集版本更改为最后自动生成(“1.0.0.*”)。

我使用 WPF 应用程序多次执行此操作都没有问题,但使用 .NET 核心时,每当我尝试从主窗口创建新窗口时,都会收到以下错误消息:

System.FormatException: 'Input string was not in a correct format.'

我用来创建新窗口的代码如下(按下按钮):

About windowHelp = new About();
windowHelp.Owner = this;
windowHelp.WindowStartupLocation = WindowStartupLocation.CenterOwner;
windowHelp.Show();

我确实将以下内容添加到 .CSPROJ 文件中(使用文本编辑器):

    <Deterministic>false</Deterministic>

每当我将程序集版本恢复为“1.0.0.0”时,一切正常。

有人可以帮我解决这个问题吗?目标是删除“输入字符串的格式不正确”。消息,同时保持“1.0.0.*”程序集版本。

感谢您的时间和帮助,非常感谢。

编辑:根据要求的其他详细信息:

在此处输入图像描述

注意:这是添加“*”的地方。

我会得到一个堆栈跟踪。

编辑:这是堆栈跟踪:

at System.Number.ThrowOverflowOrFormatException(ParsingStatus status, TypeCode type)
at System.Version.TryParseComponent(ReadOnlySpan`1 component, String componentName, Boolean throwOnFailure, Int32& parsedComponent)
at System.Version.ParseVersion(ReadOnlySpan`1 input, Boolean throwOnFailure)
at System.Version.Parse(String input)
at System.Version..ctor(String version)
at System.Windows.Navigation.BaseUriHelper.GetLoadedAssembly(String assemblyName, String assemblyVersion, String assemblyKey)
at MS.Internal.AppModel.ResourceContainer.GetResourceManagerWrapper(Uri uri, String& partName, Boolean& isContentFile)
at MS.Internal.AppModel.ResourceContainer.GetPartCore(Uri uri)
at System.IO.Packaging.Package.GetPartHelper(Uri partUri)
at System.IO.Packaging.Package.GetPart(Uri partUri)
at System.Windows.Application.GetResourceOrContentPart(Uri uri)
at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
at REDACTED.Help.InitializeComponent() in REDACTED\Help.xaml:line 1
at REDACTED.Help..ctor() in REDACTED\Help.xaml.cs:line 22
at REDACTED.MainWindow.buttonHelp_Click(Object sender, RoutedEventArgs e) in REDACTED\MainWindow.xaml.cs:line 153
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
at System.Windows.Controls.Primitives.ButtonBase.OnClick()
at System.Windows.Controls.Button.OnClick()
at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
at System.Windows.UIElement.OnMouseLeftButtonUpThunk(Object sender, MouseButtonEventArgs e)
at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
at System.Windows.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e)
at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
at System.Windows.Input.InputManager.ProcessStagingArea()
at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at System.Windows.Interop.HwndSource.InputFilterMessage(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.Threading.Dispatcher.Run()
at System.Windows.Application.RunDispatcher(Object ignore)
at System.Windows.Application.RunInternal(Window window)
at System.Windows.Application.Run()
at REDACTED.App.Main()

编辑:当前“关于”窗口:XAML 代码:

<Window x:Class="REDACTED.About"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:REDACTED"
        mc:Ignorable="d"
        Title="About" Height="446" Width="800" WindowStartupLocation="CenterOwner">
    <Grid>

    </Grid>
</Window>

而且界面完全是空的:

在此处输入图像描述

编辑:当前“帮助”窗口:XAML 代码:

<Window x:Class="REDACTED.Help"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:REDACTED"
        mc:Ignorable="d"
        Title="Help" Height="450" Width="800" WindowStartupLocation="CenterOwner">
    <Grid>

    </Grid>
</Window>

与“关于”窗口相同,“帮助”窗口完全为空:

在此处输入图像描述

标签: c#.netwpf.net-core

解决方案


GitHub dotnet/wpf #2517 复制:对于非确定性构建 WPF 应用程序在资源字典加载时崩溃InitializeComponent(),会生成以下内容:

public void InitializeComponent()
{
    if (!_contentLoaded)
    {
        _contentLoaded = true;
        base.set_StartupUri(new Uri("MainWindow.xaml", UriKind.Relative));
        Uri resourceLocater = new Uri("/NetCoreWpfNonDeterministic;V1.0.0.*;component/app.xaml", UriKind.Relative);
        Application.LoadComponent((object)this, resourceLocater);
    }
}

那是一个错误。有人会怀疑这个错误就在附近MarkupCompiler.cs#L2606,或者更确切地说是在它上面的某个地方。

这看起来像是一个先有鸡还是先有蛋的问题,因为标记编译器(将 XAML 转换为 IL)还不知道程序集的版本号,但我不确定。

无论如何,除了将资源放在具有固定版本号的程序集中之外,这不是您可以轻松解决的问题。


推荐阅读