简体   繁体   中英

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:

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

XamlParseException occurred

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

I've set the icon to Always Create on the Copy To Output Directory with no luck.
I changed the icon from Resource to EmbeddedResource - no luck there either.
I added it to the Resources for 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.

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).

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. 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:

<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 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 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()

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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