在 WPF XAML 窗口上添加图标会导致错误/崩溃 VS2012

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20169366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:06:42  来源:igfitidea点击:

Adding icon on WPF XAML window causes error/crash VS2012

c#wpfxamlvisual-studio-2012

提问by MC9000

I found a bug in WPF XAML when specifying an icon for a XAML Window, trying to run the program generates an error message on the line:

我在为 XAML 窗口指定图标时发现了 WPF XAML 中的一个错误,尝试运行该程序会在行上生成一条错误消息:

 System.Windows.Application.LoadComponent(Me, resourceLocater)

XamlParseException occurred

发生 XamlParseException

Provide value on 'System.Windows.Baml2006.TypeConverterMarkupExtension' threw an exception.' Line number '5' and line position '100'.

在“System.Windows.Baml2006.TypeConverterMarkupExtension”上提供值引发异常。行号“5”和行位置“100”。

I've set the icon to Always Createon the Copy To Output Directorywith no luck.
I changed the icon from Resourceto EmbeddedResource- no luck there either.
I added it to the Resourcesfor the project - still no luck.
I've verified the file and it's location 100%.
The XAML of the window is correct, the name is correct, the path is correct.

我已经将图标设置为Always Createon Copy To Output Directory,但没有运气。
我将图标从 更改ResourceEmbeddedResource- 那里也没有运气。
我将它添加到Resources项目中 - 仍然没有运气。
我已经验证了文件,它的位置 100%。
窗口的XAML正确,名称正确,路径正确。

Icon="Resources/VisualizerIcon.ico" 

Stumped - online forums say to Copy to output Directory is the solution, but, after building the solution, only the Folder is copied over (even though I explicitly set the ICO file to be copied).

难倒 - 在线论坛说复制到输出目录是解决方案,但是,在构建解决方案后,仅复制文件夹(即使我明确设置了要复制的 ICO 文件)。

Anyone?

任何人?

回答by ArchAngel

You just need to set the build action for the actual image/icon to Resource as shown in the properties window..

您只需要将实际图像/图标的构建操作设置为 Resource,如属性窗口中所示。

enter image description here

在此处输入图片说明

I know it's an old post, just wanted to share a dummy error..

我知道这是一个旧帖子,只是想分享一个虚拟错误..

回答by Noctis

First, I'm glad you found a solution. Seems that not many around here actually try to find solution to their problems, and they wait for others to find them.

首先,很高兴您找到了解决方案。似乎这里没有多少人真正尝试找到解决问题的方法,他们等待其他人找到它们。

Having said that, why don't you use the project setting to set it up (right click on your project, then select 'Properties') ?

话虽如此,为什么不使用项目设置来设置它(右键单击您的项目,然后选择“属性”)?

As in this screenshot: enter image description here

如此屏幕截图所示: 在此处输入图片说明

I find it is simpler, no code needed, and it works ...

我发现它更简单,不需要代码,而且它有效......

回答by Screamin

Just set the Resources compile property to Resources. then use

只需将资源编译属性设置为资源。然后使用

    <Image width="80" Height="80" Source="Resources/my_image.png"/>

or icon for window

或窗口图标

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:MyApplication" x:Name="Main_Window" x:Class="MainWindow"
    Title="MainWindow" Height="600" Width="800" Icon="Resources/icon.ico">

回答by hillin

Try check for the inner exception. If the call site looks like:

尝试检查内部异常。如果呼叫站点如下所示:

at System.Windows.Media.Imaging.BitmapSource.CriticalCopyPixels(System.Windows.Int32Rect, IntPtr, Int32, Int32) at System.Windows.Media.Imaging.BitmapSource.CriticalCopyPixels(System.Windows.Int32Rect, System.Array, Int32, Int32) at System.Windows.Media.Imaging.BitmapSource.CopyPixels(System.Windows.Int32Rect, System.Array, Int32, Int32) at System.Windows.Media.Imaging.BitmapSource.CopyPixels(System.Array, Int32, Int32) at MS.Internal.AppModel.IconHelper.CreateIconHandleFromBitmapFrame(System.Windows.Media.Imaging.BitmapFrame) at MS.Internal.AppModel.IconHelper.CreateIconHandleFromImageSource(System.Windows.Media.ImageSource, System.Windows.Size) at MS.Internal.AppModel.IconHelper.GetIconHandlesFromImageSource(System.Windows.Media.ImageSource, IconHandle ByRef, IconHandle ByRef) at System.Windows.Window.UpdateIcon() at System.Windows.Window.SetupInitialState(Double, Double, Double, Double) at System.Windows.Window.CreateSourceWindow(Boolean) at System.Windows.Window.ShowHelper(System.Object) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32) at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate) at System.Windows.Threading.DispatcherOperation.InvokeImpl() at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object) at MS.Internal.CulturePreservingExecutionContext.Run(MS.Internal.CulturePreservingExecutionContext, System.Threading.ContextCallback, System.Object) at System.Windows.Threading.DispatcherOperation.Invoke() at System.Windows.Threading.Dispatcher.ProcessQueue() at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef) at MS.Win32.HwndWrapper.WndProc(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef) at MS.Win32.HwndSubclass.DispatcherCallbackOperation(System.Object) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32) at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate) at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority, System.TimeSpan, System.Delegate, System.Object, Int32) at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr, Int32, IntPtr, IntPtr) at MS.Win32.UnsafeNativeMethods.DispatchMessage(System.Windows.Interop.MSG ByRef) at System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame) at System.Windows.Application.RunDispatcher(System.Object) at System.Windows.Application.RunInternal(System.Windows.Window) at ClientLauncher.App.Main()

在 System.Windows.Media.Imaging.BitmapSource.CriticalCopyPixels(System.Windows.Int32Rect, IntPtr, Int32, Int32) 在 System.Windows.Media.Imaging.BitmapSource.CriticalCopyPixels(System.Windows.Int32Rect, System.Array, Int32, Int32) 在 System.Windows.Media.Imaging.BitmapSource.CopyPixels(System.Windows.Int32Rect, System.Array, Int32, Int32) 在 System.Windows.Media.Imaging.BitmapSource.CopyPixels(System.Array, Int32, Int32)在 MS.Internal.AppModel.IconHelper.CreateIconHandleFromBitmapFrame(System.Windows.Media.Imaging.BitmapFrame) 在 MS.Internal.AppModel.IconHelper.CreateIconHandleFromImageSource(System.Windows.Media.ImageSource, System.Windows.Size) 在 MS.Internal .AppModel.IconHelper.GetIconHandlesFromImageSource(System.Windows.Media.ImageSource, IconHandle ByRef, IconHandle ByRef) 在 System.Windows.Window。UpdateIcon() at System.Windows.Window.SetupInitialState(Double, Double, Double, Double) at System.Windows.Window.CreateSourceWindow(Boolean) at System.Windows.Window.ShowHelper(System.Object) at System.Windows.Threading .ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32) 在 System.Windows.Threading.ExceptionWrapper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate) 在 System.Windows.Threading .DispatcherOperation.InvokeImpl() 在 System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) 在 System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System. Threading.ContextCallback, System.Object, Boolean) 在 System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object) at MS.Internal.CulturePreservingExecutionContext.Run(MS.Internal.CulturePreservingExecutionContext, System.Threading.ContextCallback, System.Object) at System.Windows.Threading.DispatcherOperation.Invoke() at System.Windows.Threading.Dispatcher.ProcessQueue() 在 System.Windows.Threading.Dispatcher.WndProcHook(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef) 在 MS.Win32.HwndWrapper.WndProc(IntPtr, Int32, IntPtr, IntPtr Boolean ByRef) at MS.Win32.HwndSubclass.DispatcherCallbackOperation(System.Object) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32) at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(System.对象、System.Delegate、System.Object、Int32、System.Delegate)在 System.Windows。Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority, System.TimeSpan, System.Delegate, System.Object, Int32) 在 MS.Win32.HwndSubclass.SubclassWndProc(IntPtr, Int32, IntPtr, IntPtr) 在 MS.Win32。 UnsafeNativeMethods.DispatchMessage(System.Windows.Interop.MSG ByRef) 在 System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame) 在 System.Windows.Application.RunDispatcher(System.Object) 在 System.Windows。 ClientLauncher.App.Main() 处的 Application.RunInternal(System.Windows.Window)MSG ByRef) at System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame) at System.Windows.Application.RunDispatcher(System.Object) at System.Windows.Application.RunInternal(System.Windows.Window)在 ClientLauncher.App.Main()MSG ByRef) at System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame) at System.Windows.Application.RunDispatcher(System.Object) at System.Windows.Application.RunInternal(System.Windows.Window)在 ClientLauncher.App.Main()

Then you might be running into a bug like this, except for it could happen on newer OS, like Windows 10. Some of our clients have this kind of crashes on Windows 10 machines.

然后,你可能会运行到像一个bug这样的,除了它可能发生在新的操作系统,如Windows 10,我们的一些客户有这种崩溃在Windows 10机器。

The solution is to simplify the icon file, don't let it contain frames larger than 64*64, or simply use a PNG for window icon instead. That being said, high resolution icons could still be used as app icon.

解决办法是简化图标文件,不要让它包含大于64*64的帧,或者干脆使用PNG作为窗口图标。话虽如此,高分辨率图标仍然可以用作应用程序图标。

回答by MC9000

Here's what I came up with (not elegant, but it works) In the loaded event for the window:

这是我想出的(不优雅,但它有效)在窗口的加载事件中:

Private Sub TileLayout_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
    Me.winTileLayout.Icon = Bitmap2BitmapSource(My.Resources.VisualizerIcon.ToBitmap)
End Sub

And the helper function:

和辅助函数:

Public Shared Function Bitmap2BitmapSource(bmp As System.Drawing.Bitmap) As BitmapSource
    Dim retval As BitmapSource = Nothing
    Dim hBitmap As IntPtr = bmp.GetHbitmap()
    Try
        retval = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions())
    Catch ex As Win32Exception
        retval = Nothing
    Finally
        DeleteObject(hBitmap)
    End Try
    Return retval
End Function

The DeleteObject code:

删除对象代码:

<System.Runtime.InteropServices.DllImport("gdi32.dll")> _
Public Shared Function DeleteObject(hObject As IntPtr) As Boolean
End Function