Wpf 如何在使用Caliburn.Micro设置的应用程序中设置窗口/应用程序图标

Wpf 如何在使用Caliburn.Micro设置的应用程序中设置窗口/应用程序图标,wpf,caliburn.micro,Wpf,Caliburn.micro,我想我遗漏了一些明显的东西。但是由于我的应用程序的主窗口是由 protected override void OnStartup(object sender, StartupEventArgs e) { DisplayRootViewFor<MainWindowViewModel>(); } 启动时受保护的覆盖无效(对象发送方、StartupEventArgs e) { DisplayRootViewFor(); } 在我的引导程序中,如何设置窗口本身和工具栏中应用程序的

我想我遗漏了一些明显的东西。但是由于我的应用程序的主窗口是由

protected override void OnStartup(object sender, StartupEventArgs e)
{
    DisplayRootViewFor<MainWindowViewModel>();
}
启动时受保护的覆盖无效(对象发送方、StartupEventArgs e)
{
DisplayRootViewFor();
}
在我的引导程序中,如何设置窗口本身和工具栏中应用程序的图标?

  • 基于XAML的解决方案:将
    主窗口视图
    基类从
    用户控件
    更改为
    窗口
    (在.XAML和.XAML.cs中),然后在XAML中正确设置
    图标
    属性或任何其他特定于窗口的属性

  • 基于代码的解决方案DisplayRootViewFor采用可选设置参数:

    var settings = new Dictionary<string, object>
    {
        { "Icon", new BitmapImage(new Uri("pack://application:,,,/WpfApplication2;component/icon.png")) },
        { "ResizeMode", ResizeMode.NoResize }
    };
    
    DisplayRootViewFor<IShell>(settings);
    
    var设置=新字典
    {
    {“图标”,新位图图像(新Uri(“pack://application:,,,,/WpfApplication2;component/icon.png”)))中,
    {“ResizeMode”,ResizeMode.NoResize}
    };
    DisplayRootViewFor(设置);
    
    键应与要设置的窗口属性相对应,并且值类型必须匹配


//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>
*/
公共接口IPropertyKeyValue
{
字符串键{get;}
对象值{get;}
}
公共类PropertyKeyValue:IPropertyKeyValue
{
公共字符串密钥{get;set;}
公共对象价值
{
得到;
设置
}
}
公共类PropertyKeyValue:IPropertyKeyValue
{
对象IPropertyKeyValue.Value{get{返回此.Value;}}
公共字符串密钥{get;set;}
公共TValue值{get;set;}
}
公共类IconProperty:PropertyKeyValue
{
}
公共类WindowManager:Caliburn.Micro.WindowManager
{
公共列表默认设置{get{return\u DefaultSettings;}}
私有只读列表_defaultSettings=new List();
专用void填充(参考IDictionary设置)
{
if(DefaultSettings!=null&&DefaultSettings.Count>0)
{
如果(设置==null)
设置=新字典();
foreach(默认设置中的var prop)
{
设置[属性键]=属性值;
}
}
}
受保护的覆盖System.Windows.Window CreateWindow(对象根模型、布尔isDialog、对象上下文、IDictionary设置)
{
填充(参考设置);
return base.CreateWindow(rootModel、isDialog、context、settings);
}
}
//引导器
受保护的覆盖对象GetInstance(类型服务,字符串键)
{
if(服务==typeof(IWindowManager))
返回此.Application.FindResource(“wm”);
返回base.GetInstance(服务,键);
}
/*
*/

以下是我所做工作的示例。我只是把它放在窗口定义中

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

我对Caliburn一无所知。我想知道它是否能够从项目的属性设置应用程序图标,这是正常的方式?您需要在不进行调试的情况下运行应用程序以查看图标,否则(调试时)它将显示yourApp.vshost.exe的图标(实际上没有图标)。