给MahApps快乐男士一个简单的问题。我已经实现了一个应用程序,它使用了你伟大的metro样式的控件,使用Caliburn.Micro实现了MVVM。新的消息对话框看起来很棒,但目前还没有明确的方法来启动这些对话框,除非我自己编写包装器(我并不反对)。然而,这是不是已经完成了,或者是我遗漏了什么,这样我就可以从视图模型调用消息框而不用大惊小怪了?
耽误您时间,实在对不起。
发布于 2015-06-01 15:52:20
从1.1.3-ALPHA* (将成为1.2.0)起,MahApps提供了一个从VM启动对话框的帮助器,它在多窗口设置中工作:
1)使用窗口中的附加属性将视图模型注册到对话框子系统。
假设您的视图的DataContext设置为您想要启动对话框的视图模型,添加以下属性:
<Controls:MetroWindow
xmlns:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
Dialog:DialogParticipation.Register="{Binding}">
2)抓取/注入DialogCoordinator:
new MainWindowViewModel(DialogCoordinator.Instance);
3)显示视图模型中的对话框。使用"this“作为上下文,这样MahApps就可以将视图模型与正确的窗口关联起来:
_dialogCoordinator.ShowMessageAsync(this, "Message from VM", "MVVM based dialogs!")
发布于 2015-02-08 00:05:56
如果只有一个窗口实例显示,可以尝试如下所示:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Caliburn.Micro;
using MahApps.Metro.Controls;
using MahApps.Metro.Controls.Dialogs;
namespace Busyboy
{
[Export(typeof(IShell))]
class MainViewModel : PropertyChangedBase, IShell
{
public void StartPomodoro()
{
var mainview0 = System.Windows.Application.Current.Windows.OfType<MainView>().FirstOrDefault();
mainview0.ShowInputAsync("New Pomodoro", "Enter a name for new pomodoro session.");
}
}
}
而且,您应该有一种识别每个窗口的方法,以便可以过滤出窗口。请注意包含扩展名的导入"Metro.Controls.Dialogs“。
发布于 2019-11-23 06:36:31
我可以通过首先将对话框的父级设为Conductor<Screen>
来使其正常工作。然后,在启动对话框的一些VM操作中,我简单地执行了以下操作:
public async Task LaunchDialog(MyDialogVM vm)
{
var customDialog = new CustomDialog { Title = "Some Title" };
var view = new MyDialogView{DataContext = vm}; // instance of the view user control
customDialog.Content = view;
// this registers the vm with CaliburnMicro, hence all life-cycle events are available
ActivateItem(vm);
await _dialogCoordinator.ShowMetroDialogAsync(this, customDialog);
}
https://stackoverflow.com/questions/22083213
复制相似问题