简体   繁体   English

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

[英]Adding icon on WPF XAML window causes error/crash VS2012

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.' 在'System.Windows.Baml2006.TypeConverterMarkupExtension'上提供了一个异常值。 Line number '5' and line position '100'. 行号“5”和行位置“100”。

I've set the icon to Always Create on the Copy To Output Directory with no luck. 我已经将图标设置为Copy To Output Directory Always Create ,没有运气。
I changed the icon from Resource to EmbeddedResource - no luck there either. 我将图标从Resource更改为EmbeddedResource - 也没有运气。
I added it to the Resources for the project - still no luck. 我将它添加到项目的Resources中 - 仍然没有运气。
I've verified the file and it's location 100%. 我已经验证了文件,它的位置是100%。
The XAML of the window is correct, the name is correct, the path is correct. 窗口的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). Stumped - 在线论坛上说复制到输出目录是解决方案,但是,在构建解决方案之后,只复制了文件夹(即使我明确设置要复制的ICO文件)。

Anyone? 任何人?

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: 如截图所示: 在此输入图像描述

I find it is simpler, no code needed, and it works ... 我发现它更简单,无需代码,而且有效......

You just need to set the build action for the actual image/icon to Resource as shown in the properties window.. 您只需将实际图像/图标的构建操作设置为资源,如属性窗口中所示。

在此输入图像描述

I know it's an old post, just wanted to share a dummy error.. 我知道这是一个老帖子,只是想分享一个假的错误..

Just set the Resources compile property to Resources. 只需将Resources compile属性设置为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">

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: DeleteObject代码:

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

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.O System.Windows.Media.Imaging.BitmapSource.CriticalCopyPixels(System.Windows.Int32Rect,System.Array,Int32,在System.Windows.Media.Imaging.BitmapSource.CopyPixels(System.Array,Int32,Int32)的System.Windows.Media.Imaging.BitmapSource.CopyPixels(System.Windows.Int32Rect,System.Array,Int32,Int32)处的Int32)在MS.Internal的MS.Internal.AppModel.IconHelper.CreateIconHandleFromBitmapFrame(System.Windows.Media.Imaging.BitmapFrame)上的MS.Internal.AppModel.IconHelper.CreateIconHandleFromImageSource(System.Windows.Media.ImageSource,System.Windows.Size)系统中的System.Windows.Window.UpdateIcon()处于System.Windows.Window.SetupInitialState(Double,Double,Double,Double)的.AppModel.IconHelper.GetIconHandlesFromImageSource(System.Windows.Media.ImageSource,IconHandle ByRef,IconHandle ByRef) System.Windows.Window.ShowHelper(System.O)中的.Windows.Window.CreateSourceWindow(Boolean) bject) 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.Wn bject)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。在System.Threading.ExecutionContext.Run上的System.Windows.Threading.DispatcherOperation.InvokeImpl()处的System.Whows.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext,System.Threading.ContextCallback,System.Object,Boolean)中委派)。 System.Threading.ExecutionContext,System.Threading.ContextCallback,System.Object,Boolean)位于MS.Internal.CulturePreservingExecutionContext的System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext,System.Threading.ContextCallback,System.Object)。在System.Windows.Threading.Dispatcher的System.Windows.Threading.Dispatcher.ProcessQueue()上的System.Windows.Threading.DispatcherOperation.Invoke()中运行(MS.Internal.CulturePreservingExecutionContext,System.Threading.ContextCallback,System.Object) .Wn dProcHook(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的MS.Win32.HwndSubclass.DispatcherCallbackOperation(System.Object)上的MS.Win32.HwndWrapper.WndProc(IntPtr,Int32,IntPtr,IntPtr,Boolean ByRef)中的dProcHook(IntPtr,Int32,IntPtr,IntPtr,Boolean ByRef) System.Windows上的System.Windows.Threading.ExceptionWrapper.TryCatchWhen(System.Object,System.Delegate,System.Object,Int32,System.Delegate)中的.Threading.ExceptionWrapper.InternalRealCall(System.Delegate,System.Object,Int32)位于MS.Win32的MS.Win32.HwndSubclass.SubclassWndProc(IntPtr,Int32,IntPtr,IntPtr)的.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority,System.TimeSpan,System.Delegate,System.Object,Int32) System.Windows上System.Windows.Application.RunDispatcher(System.Object)的System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame)中的.UnsafeNativeMethods.DispatchMessage(System.Windows.Interop.MSG ByRef) ClientLauncher.App.Main()中的.Application.RunInternal(System.Windows.Window)

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. 解决方案是简化图标文件,不要让它包含大于64 * 64的帧,或者只是使用PNG作为窗口图标。 That being said, high resolution icons could still be used as app icon. 话虽这么说,高分辨率图标仍然可以用作应用程序图标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM