Of the burn.Micro 中 的 WindowManager

Window Manager Profile

Caliburn Micro window manager can create ViewModel through the window, you only need to pass a ViewModel instance to the window manager, window manager will automatically find the corresponding View, and as a window or dialog box opens, before opening can also view of partially dependent configuration attributes.

Window Manager is very important in the CM, the application creates a new window, use the pop-up dialog boxes and other window manager is very convenient. But not the same in CM WindowManager implemented on each platform (WPF, Silverlight, WindowsPhone, WinRT) is. So, I want to fully understand a little difficult, and even the official document did not elaborate. So, for the sake of simplicity, this article only for WindowManager on WPF introduced.

WPF Application Window Manager

To use the window manager, you need to have a window manager object. We usually practice is to configure the IOC in Bootstrapper, create a WindowManager and imported into the IOC container (refer Bootstrapper configuration related articles before learning). IOC code configuration may be such (MEF to herein are exemplary of the container as IOC):

  protected override void Configure()
  {
    container = new CompositionContainer(new AggregateCatalog(AssemblySource.Instance.Select(x => new AssemblyCatalog(x)).OfType<ComposablePartCatalog>()));
 
    CompositionBatch batch = new CompositionBatch(); 
    batch.AddExportedValue<IWindowManager>(new WindowManager());
    batch.AddExportedValue(container); 
    container.Compose(batch);
  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

Then we need to extract the object WindowManager WindowManager of the ViewModel. Specifically, the following:

[Export(typeof(ShellViewModel))]
public class ShellViewModel : PropertyChangedBase
{
private readonly IWindowManager _windowManager;
 
[ImportingConstructor]   //必须加入这个,这是因为用MEF在创建ShellViewModel实例时,有[ImportingConstructor]标记的构造函数的参数会自动使用容器内相应的export对象
public ShellViewModel(IWindowManager windowManager)
{
  _windowManager = windowManager;
}
  ...
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

WindowManager but very simple to use, the following code will create a new ShellViewModel, and find ShellView according to ShellViewModel, then display ShellView window, ShellViewModel will become dataContext ShellView of.

public void NewWindow()
{
   _windowManager.ShowWindow(new ShellViewModel(_windowManager));
}
  • 1
  • 2
  • 3
  • 4

Window Manager to open the windows, dialog boxes and pop-ups, you can just call a different approach can be. You can Caliburn Micro comes HelloWindowManager example, to see more usage WindowManager window manager.

WindowManager of setting parameters

When WindowManager create a window, you can pass parameters to the properties of the new window. This will better enable you according to your needs, finer control over your application. Can refer to the following example:

public void NewWindow()
{
     var settings = new Dictionary<string, object> { { "Topmost", true }, { "Owner", GetView() } };
     //Owner不是依赖属性不能绑定,但在这里设置是可以的
     _windowManager.ShowDialog(new ConfigViewModel(), null, settings);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

The above example, I put the new window Topmost is set to true, Owner window is set to the current ViewModel bound (if the current ViewModel inherits such as ViewAware or Screen, you can call the method to get GetView View object).

Custom WindowManager

In some cases, the window manager to achieve self-definition is useful. If you need to set the properties window in all instances is the same value, use it well. For example, attributes may include icon icon, window states, window size, and custom styles. I found most commonly attribute set in Windows is "SizeToContent". By default, Caliburn Micro is set SizeToContent.WidthAndHeight. This means that the window automatically resizes itself based on its content. Although sometimes you can easily do this, but I found that this can lead to problems: Some layout and set the application's window, by default, will lead to maximizing cross-border.

Creating a custom window manager is very simple. First, add an inheritance to "WindowManager" class, then you can override "EnsureWindow" way to do something like the following:

protected override Window EnsureWindow(object model, object view, bool isDialog)
{
  Window window = base.EnsureWindow(model, view, isDialog); 
  window.SizeToContent = SizeToContent.Manual;
  return window;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

In this method, we first by calling base.EnsureWindow () to create a window instance. Next, you can set up your desired window Properties, and then simply return the window instance.

After just use custom window manager instead of the default window manager, you can achieve the effect you want. For example, you can use the following way to add your custom window manager:

batch.AddExportedValue<IWindowManager>(new AppWindowManager());
  • 1

This will result in all places using the window manager, the internal report CM will adopt your custom window manager.

Reference links

 

Source: https://blog.csdn.net/hustlei/article/details/86699377

Guess you like

Origin www.cnblogs.com/mq0036/p/12444478.html