2013-11-23 37 views
3

我發現了一個bug在WPF XAML指定用於XAML窗口的圖標時,試圖運行程序生成就行的錯誤消息:添加圖標導致錯誤/崩潰VS2012

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

發生XamlParseException

在'System.Windows.Baml2006.TypeConverterMarkupExtension'上提供值引發異常。'行號「5」和行位置「100」。

我已將圖標設置爲Always CreateCopy To Output Directory沒有運氣。
我將圖標從Resource更改爲EmbeddedResource - 也沒有運氣。
我將它添加到項目Resources - 仍然沒有運氣。
我已驗證該文件,它的位置100%。
窗口的XAML是正確的,名稱是正確的,路徑是正確的。

Icon="Resources/VisualizerIcon.ico" 

難倒 - 網上論壇說要複製到輸出目錄的解決方案,但是,構建解決方案後,僅該文件夾複製(即使我明確地設置要複製的ICO文件)。

有人嗎?

+0

注意:我清理了項目,ICO文件現在顯示在BIN文件夾中,但仍然出現與上述相同的錯誤。 – MC9000

+0

您是否看過:[msdn page](http://msdn.microsoft.com/zh-cn/library/system.windows.application.loadcomponent(v = vs.110).aspx) – Noctis

+0

錯誤的URI地址 – MC9000

回答

0

這就是我想出了(不優雅,但它的作品) 在該窗口的加載事件:

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 
3

首先,我很高興你找到了解決方案。似乎在這裏沒有多少人真的試圖找到解決他們的問題,他們等待別人找到他們。說了這麼多,你爲什麼不使用項目設置來設置它(右鍵點擊你的項目,然後選擇「屬性」)???????????????????????????????????

正如在這個截圖: enter image description here

我發現這是簡單的,沒有代碼需要,和它的作品...

+0

這是我嘗試的第一件事(我使用上面顯示的技術添加了ico和另一個嵌入式資源),但仍然使用相同的錯誤消息進行了崩潰。去搞清楚。 – MC9000

+0

這很奇怪......你確定你的類型沒問題?並做了平常的清潔,重建,向木雞神祈禱它會建好嗎? – Noctis

+0

我可能錯過了一些東西,這是我第一次進入WPF。 – MC9000

1

只需設定資源編譯屬性的資源。然後用

<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">

1

你只需要爲實際的圖像/圖標資源生成操作如圖所示在屬性窗口中..

enter image description here

我知道這是舊的文章,只是想分享一個虛擬的錯誤..

0

嘗試檢查內部異常。如果調用網站看起來像:

在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) 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.ImageS (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.ExecutionCon text.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 MS.Internal.CulturePreservingExecutionContext.Run(MS.Internal.CulturePreservingExecutionContext,System.Threading.ContextCallback,System。)System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext,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,Int 32,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) 在ClientLauncher.App.Main()

你就可能會運行到像this的錯誤,除了它可能發生在新的操作系統,如Windows 10.有些鄰我們的客戶在Windows 10機器上出現這種崩潰。

解決方案是簡化圖標文件,不要讓它包含大於64 * 64的幀,或者直接使用PNG作爲窗口圖標。話雖如此,高分辨率的圖標仍然可以用作應用圖標。