嗨,我有一个初学者的问题.我有shell(它是wpf窗口),在这个shell中是屏幕(它是用户控件/视图模型).
我想从视图模型打开新窗口,而不是在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>
Run Code Online (Sandbox Code Playgroud)
使用MEF 导出ChatViewModel.
public interface IChatViewModel
{
}
[Export("ChatScreen",typeof(IChatViewModel))]
public class ChatViewModel
{
}
Run Code Online (Sandbox Code Playgroud)
在视图模型中我有这个方法:
有了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));
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,如果我尝试显示新窗口它不起作用,它只有当我在shell(窗口)中显示新的用户控件时才有效.
我想在skype中实现类似的行为.你有一个带有列表框的主窗口,双击项目并显示新的聊天窗口.
主窗口可以在聊天窗口上用EventAggregator发布,聊天窗口也可以在主窗口上发布.这是我的目标.
我知道我不能在显示新窗口时使用类ShowScreen.我想知道从视图模型创建新窗口的正确方法是什么,并将事件聚合器注入此vie模型.
有什么建议?感谢您的帮助和时间.
你看过WindowManager.Show或WindowManager.ShowDialog吗?Rob在http://caliburnmicro.codeplex.com/wikipage?title=The%20Window%20Manager上有一个示例.您可以将此依赖项作为IWindowManager注入到视图模型中.