Caliburn.Micro - 在新窗口中设置焦点

时间:2013-08-18 20:22:43

标签: wpf mvvm caliburn.micro

我的应用是多个Window观看次数之间的最高UserControl切换。它的行为是当用户在窗口外点击时关闭(更常见的是当它失去焦点时),并在用户点击系统托盘图标时再次显示。

单击托盘图标后显示窗口时,我无法获得焦点。问题是窗口显示没有焦点,当用户点击外面时它不会隐藏。用户必须先进入窗口,然后单击外部以触发Window的Deactivated事件。

我可以使用文档中最基本的示例重现问题。我在下面展示了我可以产生的问题的最基本表示。

我尝试过很多不同的东西,但都没有表现出任何不同的行为。例如,我尝试在视图模型中调用OnViewLoaded处理程序中的视图Focus()并停用视图模型,而不是在Close操作中关闭窗口。我也尝试过suggestion看似同样的问题。

非常感谢任何提示或帮助如何做到这一点。

[Export(typeof(ShellViewModel))]
public class ShellViewModel : Conductor<object>
{
    IWindowManager windowManager;

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

        ShowPageOne();
    }

    public void ShowPageOne()
    {
        ActivateItem(new PageOneViewModel());
    }

    public void ShowPageTwo()
    {
        ActivateItem(new PageTwoViewModel());
    }

    public void Close()
    {
        this.TryClose();

        // Using this to simulate the user clicking on a system tray icon
        var timer = new Timer();
        timer.Tick += (s, e) =>
        {
            windowManager.ShowWindow(this);
            timer.Stop();
        };
        timer.Interval = 1000;
        timer.Start();
    }
}

我的ShellView是:

<Window x:Class="PopupTest.ShellView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:tc="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"
         xmlns:cal="clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro"
         Width="300" Height="400"
         cal:Message.Attach="[Event Deactivated] = [Action Close]"
         Topmost="True">

<StackPanel>
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
        <Button x:Name="ShowPageOne" Content="Show Page One" />
        <Button x:Name="ShowPageTwo" Content="Show Page Two" />
    </StackPanel>

    <ContentControl x:Name="ActiveItem" />
</StackPanel>

我的两个视图模型是:

public class PageOneViewModel : Caliburn.Micro.Screen { }

public class PageTwoViewModel : Caliburn.Micro.Screen { }

观点是:

<UserControl x:Class="PopupTest.PageOneView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <TextBlock x:Name="bob" FontSize="32">Page One</TextBlock>
</UserControl>

0 个答案:

没有答案