我正在尝试在我的项目中使用Mahapps对话框但是在我的ViewModel中从ReactivUI命令触发时无法使它们工作。在视图的XAML中,我已经注册了对话框。
xmlns:dialogs="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
dialogs:DialogParticipation.Register="{Binding}"
我还有一个绑定到ShowDialog命令的按钮。
this.BindCommand(viewModel, vm => vm.ShowDialog, x => x.button);
最后,在我的ViewModel中,我有RxUI命令和dialogcoordinator实例集。
public MainWindowViewModel(IDialogCoordinator dialogCoordinator)
{
_dialogCoordinator = dialogCoordinator;
ShowDialog = ReactiveCommand.CreateFromTask(async () =>
{
await _dialogCoordinator.ShowMessageAsync(this, "Message from VM", "MVVM based dialogs!");
});
ShowDialog.ThrownExceptions.Subscribe(ex => Console.WriteLine(ex.ToString()));
}
无论我尝试过什么,它总是抛出相同的错误
System.InvalidOperationException: Context is not registered. Consider using DialogParticipation.Register in XAML to bind in the DataContext.
我不确定是否还有其他任何东西需要让对话框正常工作,或者我是否只是错误地使用RxUI中的命令
答案 0 :(得分:0)
对我来说,使用 ContentRendered
事件没有任何作用;我仍然遇到异常 InvalidOperationException: Context is not registered. Consider using DialogParticipation.Register in XAML to bind in the DataContext
。
我的解决方案基于 Kent Boogaart 的 book samples。
查看:
using System.Reactive;
using System.Reactive.Disposables;
using System.Windows;
using MahApps.Metro.Controls;
using MahApps.Metro.Controls.Dialogs;
using ReactiveUI;
using Splat;
namespace ReactiveUIMahAppsDialog
{
public partial class ChildView
{
public ChildView()
{
InitializeComponent();
this.WhenActivated(disposables =>
{
ViewModel ??= Locator.Current.GetService<ChildViewModel>() ?? new ChildViewModel();
this.BindCommand(ViewModel,
viewModel => viewModel.LogInUser,
view => view.Test)
.DisposeWith(disposables);
this.ViewModel.Message
.RegisterHandler(async interaction =>
{
MetroWindow window = (MetroWindow) Window.GetWindow(this);
await window.ShowMessageAsync("test", "test");
interaction.SetOutput(Unit.Default);
})
.DisposeWith(disposables);
});
}
}
}
<reactiveUi:ReactiveUserControl
x:Class="ReactiveUIMahAppsDialog.ChildView"
x:TypeArguments="viewModels:ChildViewModel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:reactiveUi="http://reactiveui.net"
xmlns:viewModels="clr-namespace:ReactiveUIMahAppsDialog"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Button x:Name="Test" Content="Show Dialog" Width="100" Height="100"/>
</Grid>
</reactiveUi:ReactiveUserControl>
在 MetroWindow
中托管反应式用户控件。
视图模型:
using System.Reactive;
using System.Reactive.Linq;
using ReactiveUI;
namespace ReactiveUIMahAppsDialog
{
public class ChildViewModel : ReactiveObject
{
public ChildViewModel()
{
Message = new Interaction<Unit, Unit>();
LogInUser = ReactiveCommand.CreateFromTask(async _ => await Message.Handle(Unit.Default));
}
public ReactiveCommand<Unit, Unit> LogInUser { get; }
public Interaction<Unit, Unit> Message { get; }
}
}
可以在here找到完整代码。