[英]Adding icon on WPF XAML window causes error/crash VS2012
我在為XAML窗口指定圖標時發現了WPF XAML中的錯誤,嘗試運行該程序會在該行上生成錯誤消息:
System.Windows.Application.LoadComponent(Me, resourceLocater)
發生了XamlParseException
在'System.Windows.Baml2006.TypeConverterMarkupExtension'上提供了一個異常值。 行號“5”和行位置“100”。
我已經將圖標設置為Copy To Output Directory
Always Create
,沒有運氣。
我將圖標從Resource
更改為EmbeddedResource
- 也沒有運氣。
我將它添加到項目的Resources
中 - 仍然沒有運氣。
我已經驗證了文件,它的位置是100%。
窗口的XAML正確,名稱正確,路徑正確。
Icon="Resources/VisualizerIcon.ico"
Stumped - 在線論壇上說復制到輸出目錄是解決方案,但是,在構建解決方案之后,只復制了文件夾(即使我明確設置要復制的ICO文件)。
任何人?
首先,我很高興你找到了解決方案。 似乎這里並不是很多人試圖找到他們問題的解決方案,他們等待其他人找到他們。
話雖如此,為什么不使用項目設置進行設置(右鍵單擊您的項目,然后選擇“屬性”)?
如截圖所示:
我發現它更簡單,無需代碼,而且有效......
只需將Resources compile屬性設置為Resources即可。 然后用
<Image width="80" Height="80" Source="Resources/my_image.png"/>
或窗口的圖標
<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">
這是我提出的(不優雅,但它的工作原理)在窗口的加載事件中:
Private Sub TileLayout_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
Me.winTileLayout.Icon = Bitmap2BitmapSource(My.Resources.VisualizerIcon.ToBitmap)
End Sub
和助手功能:
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
DeleteObject代碼:
<System.Runtime.InteropServices.DllImport("gdi32.dll")> _
Public Shared Function DeleteObject(hObject As IntPtr) As Boolean
End Function
嘗試檢查內部異常。 如果呼叫站點看起來像:
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。在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 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)
然后,你可能會運行到像一個bug 這樣的,除了它可能發生在新的操作系統,如Windows 10,我們的一些客戶有這種崩潰在Windows 10機器。
解決方案是簡化圖標文件,不要讓它包含大於64 * 64的幀,或者只是使用PNG作為窗口圖標。 話雖這么說,高分辨率圖標仍然可以用作應用程序圖標。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.