1

I'm working on Visual Studio 2013 WPF Application using Mordern UI Template. When I set form icon then debug, error appear:

that is my source code :

<mui:ModernWindow x:Class="DieuBeni002.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mui="http://firstfloorsoftware.com/ModernUI"
    Title="mui" IsTitleVisible="True"
    LogoData="F1 M 24.9015,43.0378L 25.0963,43.4298C 26.1685,49.5853 31.5377,54.2651 38,54.2651C 44.4623,54.2651 49.8315,49.5854 50.9037,43.4299L 51.0985,43.0379C 51.0985,40.7643 52.6921,39.2955 54.9656,39.2955C 56.9428,39.2955 58.1863,41.1792 58.5833,43.0379C 57.6384,52.7654 47.9756,61.75 38,61.75C 28.0244,61.75 18.3616,52.7654 17.4167,43.0378C 17.8137,41.1792 19.0572,39.2954 21.0344,39.2954C 23.3079,39.2954 24.9015,40.7643 24.9015,43.0378 Z M 26.7727,20.5833C 29.8731,20.5833 32.3864,23.0966 32.3864,26.197C 32.3864,29.2973 29.8731,31.8106 26.7727,31.8106C 23.6724,31.8106 21.1591,29.2973 21.1591,26.197C 21.1591,23.0966 23.6724,20.5833 26.7727,20.5833 Z M 49.2273,20.5833C 52.3276,20.5833 54.8409,23.0966 54.8409,26.197C 54.8409,29.2973 52.3276,31.8106 49.2273,31.8106C 46.127,31.8106 43.6136,29.2973 43.6136,26.197C 43.6136,23.0966 46.127,20.5833 49.2273,20.5833 Z"          
    ContentSource="/Pages/Home.xaml" Icon="logo.png" >

<mui:ModernWindow.MenuLinkGroups>
    <mui:LinkGroup DisplayName="welcome">
        <mui:LinkGroup.Links>
            <mui:Link DisplayName="home" Source="/Pages/Home.xaml" />
        </mui:LinkGroup.Links>
    </mui:LinkGroup>
    <mui:LinkGroup DisplayName="settings" GroupKey="settings">
        <mui:LinkGroup.Links>
            <mui:Link DisplayName="software" Source="/Pages/SettingsPage.xaml" />
        </mui:LinkGroup.Links>
    </mui:LinkGroup>
</mui:ModernWindow.MenuLinkGroups>

<mui:ModernWindow.TitleLinks>
    <mui:Link DisplayName="settings" Source="/Pages/SettingsPage.xaml" />
    <mui:Link DisplayName="help" Source="https://github.com/firstfloorsoftware/mui" />
</mui:ModernWindow.TitleLinks>

When I delete "Icon" attribute, debug works fine but no icon appear in the Windows taskbar. Please help me

J0rdAn
  • 131
  • 3
  • 14
  • 2
    What is the error? – rmojab63 Feb 15 '17 at 11:42
  • First sanity check: Is the logo.png in correct directory? Does it exist? Perhaps you should add the logo as a resource and use from there? – KettuJKL Feb 15 '17 at 11:48
  • Already done sanity check – J0rdAn Feb 15 '17 at 11:52
  • Error : Exception of type 'System.Windows.Markup.XamlParseException' appeared in PresentationFramework.dll More Informations : 'Given Value on 'System.Windows.Baml2006.TypeConverterMarkupExtension' raised an exception.' line '7' line position '9'. – J0rdAn Feb 15 '17 at 11:56
  • You might need to write the path as relative to the root of the project, e.g. `"/logo.png"` – Martin Zikmund Feb 15 '17 at 12:01
  • And is the PNG file actually added as a Resource or Content file of the solution? – Martin Zikmund Feb 15 '17 at 12:06
  • PNG file added as a Ressource – J0rdAn Feb 15 '17 at 12:11
  • When debugging, did you look at the "View Detail..." information from the Exception popup? There could be something handy in there. I've found that if I add an image to the Resource.resx directly (Add Image button), I get the same exception you did, and the details include "Could not find part of the path [pathtoicon]". However, if I add the image as an 'existing item' to any folder in my project structure, then drag that file from said folder into the Resources file, it works without error as the window icon. – TiberiumFusion Feb 15 '17 at 13:05

1 Answers1

2

I found it easier to set Icon property in codebehind.

E.g.

public partial class MainWindow 
    : ModernWindow
{
    public MainWindow()
    {
        InitializeComponent();
        Icon = DieuBeni002.Properties.Resources.logo.ToImageSource();
    }
}

We need to convert logo, because it is returned as System.Drawing.Bitmap type and Icon expects System.Windows.Media.ImageSource type.

internal static class IconUtilities
{
    public static ImageSource ToImageSource(this Bitmap bitmap)
    {
        if (bitmap == null) throw new ArgumentNullException("bitmap");

        return Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
    }
}

P.S. To convert from icon type you can use suggestion explained here.

Community
  • 1
  • 1
Sergey Yakovlev
  • 366
  • 3
  • 8