How to install Window / Application icon in an application configured with Caliburn.Micro

I think I am missing something obvious. But since the main window of my application is UserControl which is started

protected override void OnStartup(object sender, StartupEventArgs e)
{
    DisplayRootViewFor<MainWindowViewModel>();
}

      

in my bootloader, how do I set the icon of the window itself and the application to the toolbar?

+4


source to share


3 answers


  • XAML based solution . Change the base class MainWindowView

    from UserControl

    to Window

    (both in .xaml and in .xaml.cs), then set the Icon

    property or any other window specific properties in the xaml.

  • Code based solution : DisplayRootViewFor<T>

    takes an optional parameters parameter:

    var settings = new Dictionary<string, object>
    {
        { "Icon", new BitmapImage(new Uri("pack://application:,,,/WpfApplication2;component/icon.png")) },
        { "ResizeMode", ResizeMode.NoResize }
    };
    
    DisplayRootViewFor<IShell>(settings);
    
          

    The keys must match the properties of the window that you want to set and the value types must match.



+7


source


// default settings for windowmanager.createwindow



public interface IPropertyKeyValue
{
    string Key { get; }
    object Value { get; }
}
public class PropertyKeyValue : IPropertyKeyValue
{
    public string Key { get; set; }
    public object Value
    {
        get;
        set;
    }
}
public class PropertyKeyValue<TValue> : IPropertyKeyValue
{
     object IPropertyKeyValue.Value { get { return this.Value; } }
    public string Key { get; set; }
    public TValue Value { get; set; }
}
public class IconProperty : PropertyKeyValue<ImageSource>
{

}
 public class WindowManager : Caliburn.Micro.WindowManager
{
    public List<IPropertyKeyValue> DefaultSettings { get { return _defaultSettings; } }
    private readonly List<IPropertyKeyValue> _defaultSettings = new List<IPropertyKeyValue>();

     private void Populate(ref IDictionary<string, object> settings)
    {
        if (DefaultSettings != null && DefaultSettings.Count > 0)
        {
            if (settings == null)
                settings = new Dictionary<String, object>();

            foreach (var prop in DefaultSettings)
            {
                settings[prop.Key] = prop.Value;
            }
        }
    }
    protected override System.Windows.Window CreateWindow(object rootModel, bool isDialog, object context, IDictionary<string, object> settings)
    {
        Populate(ref settings);            
        return base.CreateWindow(rootModel, isDialog, context, settings);
    }
}

//bootstrapper
protected override object GetInstance(Type service, string key)
{
    if (service == typeof(IWindowManager))
        return this.Application.FindResource("wm");
    return base.GetInstance(service, key);
}


/*
 <local:WindowManager x:Key="wm">
                    <local:WindowManager.DefaultSettings>
                        <local:IconProperty Key="Icon" Value="favicon.ico"/>
                    </local:WindowManager.DefaultSettings>
                </local:WindowManager>
*/

      

+1


source


Here is an example of what I am doing. I just put this in the window definition.

<Window x:Class="YourApp.Views.ShellView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:BudgetPlannerMainWPF.Views"
        mc:Ignorable="d" Icon="C:\...Path...\YourIcon.png"
        Title="Your Title" Height="500" Width="910" FontSize="14"
        WindowStyle="SingleBorderWindow" Topmost="True" SizeToContent="Width">

      

0


source







All Articles