C# MetroWindow.RightWindow动态创建的MetroWindow中的命令
如何通过Caliburn.Micro IWindowManager Show方法在动态创建的MetroWindow中绑定/创建MetroWindow.RightWindow命令 例如,我创建了一个自定义iWindows管理器实现,以始终创建MetroWindows而不是默认窗口。因此,无论何时从Caliburn创建新窗口,它都将是MetroWindow实例 我有一个通过iWindows管理器动态创建窗口的逻辑: ChatManagerC# MetroWindow.RightWindow动态创建的MetroWindow中的命令,c#,wpf,caliburn.micro,mahapps.metro,C#,Wpf,Caliburn.micro,Mahapps.metro,如何通过Caliburn.Micro IWindowManager Show方法在动态创建的MetroWindow中绑定/创建MetroWindow.RightWindow命令 例如,我创建了一个自定义iWindows管理器实现,以始终创建MetroWindows而不是默认窗口。因此,无论何时从Caliburn创建新窗口,它都将是MetroWindow实例 我有一个通过iWindows管理器动态创建窗口的逻辑: ChatManager public class ChatManager : ICh
public class ChatManager : IChatManager
{
private readonly IChatWindowSettings chatWindowSettings;
private readonly IWindowManager windowManager;
private readonly IChatFactory chatFactory;
private IDictionary<WeakReference, WeakReference> chats;
public ChatManager(IChatWindowSettings chatWindowSettings, IWindowManager windowManager, IChatFactory chatFactory)
{
this.chatWindowSettings = chatWindowSettings;
this.windowManager = windowManager;
this.chatFactory = chatFactory;
chats = new Dictionary<WeakReference, WeakReference>();
}
public void OpenFor(ISender sender)
{
var settings = chatWindowSettings.Create();
var viewModel = CreateOrGetViewModel(sender);
windowManager.ShowWindow(viewModel, null, settings);
}
private IChat CreateOrGetViewModel(ISender sender){//code...}
我怎样才能完成这样的事情呢?这里有一些解决你问题的想法 呼叫样本
var view = new MainWindow(new ChatViewModel() { ChatName = "Chat name" });
view.Show();
模型样品
public class ChatViewModel
{
public string ChatName { get; set; }
private ICommand chatCommand;
public ICommand ChatCommand
{
get
{
return chatCommand
?? (chatCommand = new SimpleCommand() {
CanExecutePredicate = o => true,
ExecuteAction = o => MessageBox.Show("Hurray :-D")
});
}
}
}
窗口代码隐藏
public partial class MainWindow : MetroWindow
{
public MainWindow(ChatViewModel chatViewModel)
{
this.DataContext = chatViewModel;
InitializeComponent();
}
}
窗口xaml
<Controls:MetroWindow x:Class="MahAppsMetroSample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"
Title="MainWindow"
GlowBrush="{DynamicResource AccentColorBrush}"
Height="350"
Width="525">
<Controls:MetroWindow.RightWindowCommands>
<Controls:WindowCommands>
<Button Content="{Binding ChatName}"
Command="{Binding ChatCommand}" />
</Controls:WindowCommands>
</Controls:MetroWindow.RightWindowCommands>
<Grid>
<!-- the content -->
</Grid>
</Controls:MetroWindow>
简单命令
public class SimpleCommand : ICommand
{
public Predicate<object> CanExecutePredicate { get; set; }
public Action<object> ExecuteAction { get; set; }
public bool CanExecute(object parameter)
{
if (CanExecutePredicate != null)
return CanExecutePredicate(parameter);
return true; // if there is no can execute default to true
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter)
{
if (ExecuteAction != null)
ExecuteAction(parameter);
}
}
公共类SimpleCommand:ICommand
{
公共谓词CanExecutePredicate{get;set;}
公共操作执行{get;set;}
公共布尔CanExecute(对象参数)
{
如果(CanExecutePredicate!=null)
返回CanExecutePredicate(参数);
返回true;//如果没有,可以执行默认值为true
}
公共事件事件处理程序CanExecuteChanged
{
添加{CommandManager.RequerySuggested+=value;}
删除{CommandManager.RequerySuggested-=value;}
}
public void Execute(对象参数)
{
if(ExecuteAction!=null)
执行(参数);
}
}
希望有帮助我不喜欢这个,因为它没有使用IWindowManager,所以视图模型项目会有视图引用,我当然会避免。我的ChatView.xaml是一个用户控件,但看看您的xaml示例,我修改了ChatView.xaml以从MetroWindow继承,并且可以正常工作,所以现在我添加了命令并绑定到ChatViewModel。。。为什么我以前没试过?
public class SimpleCommand : ICommand
{
public Predicate<object> CanExecutePredicate { get; set; }
public Action<object> ExecuteAction { get; set; }
public bool CanExecute(object parameter)
{
if (CanExecutePredicate != null)
return CanExecutePredicate(parameter);
return true; // if there is no can execute default to true
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter)
{
if (ExecuteAction != null)
ExecuteAction(parameter);
}
}