嗨,我有一个初学者的问题。我有外壳(它是 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 模型的正确方法是什么。
有什么建议吗?感谢您的帮助和时间。