2

我有以下 ViewModel,我正在使用 Caliburn Micro。IWindowManager 实例已正确解析,并且所有代码都可以正常工作。如 TODO 注释所示,我需要获取对当前窗口的引用,以便切换 AlwaysOnTop 属性。我怎样才能做到这一点?

namespace CaliburnWizardPlay
{
[Export(typeof(DropWindowViewModel))]
public class DropWindowViewModel : PropertyChangedBase, IHaveDisplayName
{
    private readonly IWindowManager windowManager;

    [ImportingConstructor]
    public DropWindowViewModel(IWindowManager windowManager)
    {
        this.windowManager = windowManager;
    }

    public string DisplayName
    {
        get { return "Main Window"; }
        set { }
    }        

    public bool AlwaysOnTop
    {
        get { return Settings.Default.DropWindowAlwaysOnTop; }
        set
        {
            Settings.Default.DropWindowAlwaysOnTop = value;
            Settings.Default.Save();
            NotifyOfPropertyChange(() => AlwaysOnTop);
            //todo: toggle the AOT attribute of the window
        }
    }

    public void FileDropped(DragEventArgs eventArgs)
    {
        if (eventArgs.Data.GetDataPresent(DataFormats.FileDrop))
        {
            string[] droppedFilePaths = eventArgs.Data.GetData(DataFormats.FileDrop, true) as string[];
            foreach (string path in droppedFilePaths)
            {
                MessageBox.Show(path);
            }

            windowManager.ShowWindow(new WizardViewModel());
        }
    }

}

}

4

1 回答 1

3

您可以使用该方法的settings参数在创建的窗口上使用包含属性名-值对的字典ShowWindow设置任何属性(例如Topmost ):

windowManager.ShowWindow(new WizardViewModel(), 
    settings: new Dictionary<string,object> { {"Topmost", AlwaysOnTop} });

如果您想更改Topmost已创建窗口的属性,我会看到三个选项(按优先顺序):

  1. 在 上创建一个AlwaysOnTop属性WizardViewModel并将视图模型存储在私有字段中并将 委托AlwaysOnTopWizardViewModel

    private WizardViewModel wizardViewModel;
    
    public void FileDropped(DragEventArgs eventArgs)
    {
        //...
        wizardViewModel = new WizardViewModel()
        windowManager.ShowWindow(wizardViewModel);
    }
    
    public bool AlwaysOnTop
    {
         get { return Settings.Default.DropWindowAlwaysOnTop; }
         set
         {
             //...
             if (wizardViewModel != null)
                 wizardViewModel.AlwaysOnTop = value;
         }
    }
    

    在您看来,您可以将WizardViewModel'AlwaysOnTop属性绑定到窗口的TopMost属性。

  2. 您可以使用Application.Windows来检索窗口。例如Name,使用设置字典设置创建的窗口的属性,然后:

    windowManager.ShowWindow(new WizardViewModel(), 
        settings: new Dictionary<string,object> 
            { {"Topmost", AlwaysOnTop}, {"Name", "WizardWindow"} });
    
    public bool AlwaysOnTop
    {
         get { return Settings.Default.DropWindowAlwaysOnTop; }
         set
         {
             //...
             var wizardViewModel = Application.Current.Windows.OfType<Window>()
                 .SingleOrDefault(w => w.Name == "WizardWindow");
             if (wizardViewModel != null)
                 wizardViewModel.AlwaysOnTop = value;
         }
    }
    
  3. 从 派生WindowManager并将其注册到您的中Bootstrapper,然后您可以覆盖CreateWindowEnsureWindow方法以将创建的窗口存储在某处设置附加属性等。

于 2012-08-05T17:18:38.053 回答