问题
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?
回答1:
XAML based solution: Change your
MainWindowView
base class fromUserControl
toWindow
(both in .xaml and in .xaml.cs), then set yourIcon
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.
回答2:
//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>
*/
回答3:
Heres an example of what I do. I just put it 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">
来源:https://stackoverflow.com/questions/27227892/how-do-i-set-a-window-application-icon-in-a-application-set-up-with-caliburn-mic