2

嗨,我有一个初学者的问题。我有外壳(它是 wpf 窗口),在这个外壳中是屏幕(它是一个用户控件/视图模型)。

我想从视图模型中打开新窗口,而不是在 shell 中显示用户控件。

所以我创建了新窗口 - ChatView

<Window x:Class="Spirit.Views.ChatView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:extToolkit="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit.Extended" Title="ChatView" Height="545" Width="763">
    <Grid Margin="4,4,4,4">
     </Grid>
</Window>

使用 MEF导出ChatViewModel

 public interface IChatViewModel
    {

    }

    [Export("ChatScreen",typeof(IChatViewModel))]
    public class ChatViewModel
    {

    }

在视图模型中我有这个方法:

使用 ShowScreen 类帮助我 Mr.Marco Amendola。它看起来像这样:

public class ShowScreen : IResult
    {
        readonly Type _screenType;
        readonly string _name;

        [Import]
        public IShellViewModel Shell { get; set; }

        Action<object> _initializationAction = screen => { };

        public ShowScreen InitializeWith<T>(T argument) 
        {
            _initializationAction = screen =>
                                        {
                                            var initializable = screen as IInitializable<T>;
                                            if (initializable != null)
                                                initializable.Initialize(argument);
                                        };
        return this;
        } 

        public ShowScreen(string name)
        {
            _name = name;
        }

        public ShowScreen(Type screenType)
        {
            _screenType = screenType;
        }

        public void Execute(ActionExecutionContext context)
        {
            var screen = !string.IsNullOrEmpty(_name)
                ? IoC.Get<object>(_name)
                : IoC.GetInstance(_screenType, null);

            _initializationAction(screen);

            Shell.ActivateItem(screen);
            Completed(this, new ResultCompletionEventArgs());
        }

        public event EventHandler<ResultCompletionEventArgs> Completed = delegate { };

        public static ShowScreen Of<T>()
        {
            return new ShowScreen(typeof(T));
        }
    }

我的问题是如果我尝试显示新窗口它不起作用,它只有在我在 shell(窗口)中显示新的用户控件时才起作用。

我想实现类似Skype的行为。您有一个带有列表框的主窗口,您双击项目并显示新的聊天窗口。

主窗口可以使用 EventAggregator 在聊天窗口上发布,聊天窗口也可以在主窗口上发布。这是我的目标。

我知道我不能在显示新窗口时使用 ShowScreen 类。我想知道从视图模型创建新窗口并将事件聚合器注入此 vie 模型的正确方法是什么。

有什么建议吗?感谢您的帮助和时间。

4

2 回答 2

6

你看过 WindowManager.Show 或 WindowManager.ShowDialog 吗?Rob 在http://caliburnmicro.codeplex.com/wikipage?title=The%20Window%20Manager有一个示例。您可以将此依赖项作为 IWindowManager 注入到您的视图模型中。

于 2010-12-21T15:05:38.077 回答
2

我正在使用这个。也许可以保存一个关于“代码在哪里?”的问题。

对话助手:

public class DialogHelper
{
    public void ShowDialog<T>(params Object[] param) where T : class
    {
        var windowManager = new WindowManager();
        T viewModel = Activator.CreateInstance(typeof(T), param) as T;

        windowManager.ShowWindow(viewModel);
    } 
}

使用方法:
无构造函数参数:

Dialog.ShowDialog<TestTableViewModel>();

使用构造函数参数:

Dialog.ShowDialog<TestTableViewModel>(dt);

请注意,我没有使用 MEF

于 2012-07-09T10:45:20.160 回答