Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set a Window/Application icon in a application set up with Caliburn.Micro

I think I am missing something obvious. But since the main window of my Application is a UserControl that is being launched by

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

in my bootstrapper how do I set the Icon of the window itself and of the application in the toolbar?

like image 643
PlTaylor Avatar asked Dec 26 '22 01:12

PlTaylor


2 Answers

  • XAML based solution: Change your MainWindowView base class from UserControl to Window (both in .xaml and in .xaml.cs), then set your Icon property or any other window-specific properties right in xaml.

  • Code based solution: DisplayRootViewFor<T> takes an optional settings 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 should correspond to the window properties you want to set, and the value types have to match.

like image 130
Frank Avatar answered Dec 27 '22 21:12

Frank


//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>
*/
like image 31
lobster.vi Avatar answered Dec 27 '22 21:12

lobster.vi