我目前正在使用C#WPF + PRISM 6.0开发应用程序。现在我想将数据导入助手实现为模态对话框。此对话框必须是可导航的,因此我在其中定义了一个区域,在用户单击“下一个”或“上一个”之后将显示助理页面。
将使用IDialogService显示模式对话框。
我的问题是,模态对话框区域未解析,因此我无法向其添加助手视图。这是我正在尝试的
在我的模块中我正在注册应用程序视图和视图模型并尝试将视图添加到区域
Container.RegisterType<IAppView, AppView>();
Container.RegisterType<IAppViewModel, AppViewModel>();
IViewModel viewModel = Container.Resolve<IViewModel>();
var region = RegionManager.Regions.FirstOrDefault(r => r.Name == "MainAppRegion");
if (region != null)
{
region.Add(viewModel.View);
}
Container.RegisterType<IModalDialogView, ModalDialogView>();
Container.RegisterType<IModalDialogViewModel, ModalDialogViewModel>();
# here the region is null, and i can't add the ModalDialogView to it
IViewModel viewModel = Container.Resolve<IModalDialogViewModel>();
region = RegionManager.Regions.FirstOrDefault(r => r.Name == "ModalDialogCenterRegion");
if (region != null)
{
region.Add(viewModel.View);
}
模态对话框是一个Window,因为IDialogService需要一个窗口。
这是实施此类助手的正确方法吗?
谢谢和问候,
迈克尔
答案 0 :(得分:1)
这里是我的IModalDialogView(ImportDialogView)和IModalDialogViewModel(ImportDialogViewModel)的源代码
下面的界面定义是一个建筑工地。我派生自两个接口(IModalDialogViewModel和IInteractionRequestAware),因为我不确定我将使用哪种模式对话框或哪种模式更适合我的需要。到目前为止,两者都不会破坏功能。
IImportDialogViewModel.cs
using System.Collections.Generic;
using Infrastructure.Interfaces;
using Model;
using MvvmDialogs;
using Prism.Interactivity.InteractionRequest;
using Prism.Regions;
namespace Modules.Interfaces.ImportAssistant
{
public interface IImportDialogViewModel : IViewModel, IModalDialogViewModel, IInteractionRequestAware
{
IEnumerable<Data> ImportedData { get; set; }
IRegionManager RegionManager { get; set; }
}
}
importdialogview.xaml
<UserControl x:Class="Modules.View.ImportAssistant.ImportDialogView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:inf="clr-namespace:Infrastructure;assembly=Infrastructure" xmlns:local="clr-namespace:Modules.View.ImportAssistant"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:md="https://github.com/fantasticfiasco/mvvm-dialogs"
xmlns:prism="http://prismlibrary.com/" xmlns:ImportAssistant="clr-namespace:Modules.ViewModel.ImportAssistant"
Width="1200" Height="600"
mc:Ignorable="d" Loaded="ImportDialogView_OnLoaded"
d:DataContext="{d:DesignInstance d:Type=ImportAssistant:ImportDialogViewModel}"
md:DialogServiceViews.IsRegistered="True">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="80" />
<RowDefinition Height="*" />
<RowDefinition Height="40" />
</Grid.RowDefinitions>
<Label Grid.Row="0"
Content="{Binding Title, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
FontSize="24"
Style="{StaticResource TitleLabelStyle}" />
<ContentControl x:Name="AssistantDialogContentControl" Grid.Row="1"
prism:RegionManager.RegionName="{x:Static inf:RegionNames.ImportAssistantMain}" />
<Grid Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0" HorizontalAlignment="Left"
Orientation="Vertical">
<Button Command="{Binding AbortAssistantCommand}"
Content="Abort"
Style="{StaticResource DefaultDialogButton}" />
</StackPanel>
<StackPanel Grid.Column="1" HorizontalAlignment="Right"
Orientation="Horizontal">
<Button Command="{Binding PreviousCommand}"
Content="Previous"
Style="{StaticResource DefaultDialogButton}" />
<Button Command="{Binding NextCommand}"
Content="Next"
Style="{StaticResource DefaultDialogButton}" />
<Button Command="{Binding FinishCommand}"
Content="Finish"
Style="{StaticResource DefaultDialogButton}" />
</StackPanel>
</Grid>
</Grid>
</UserControl>
Importdialogview.xaml.cs
using System.Windows;
using System.Windows.Controls;
using Infrastructure.Interfaces;
using Modules.Interfaces.ImportAssistant;
using Prism.Regions;
using Infrastructure;
using Microsoft.Practices.ServiceLocation;
using System.Linq;
namespace Modules.View.ImportAssistant
{
/// <summary>
/// Interaction logic for ImportDialogView.xaml
/// </summary>
public partial class ImportDialogView : UserControl, IImportDialogView
{
public ImportDialogView(IImportDialogViewModel viewModel)
{
InitializeComponent();
ViewModel = viewModel;
viewModel.View = this;
var regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
if (regionManager.Regions.Any(r => r.Name == RegionNames.ImportAssistantMain) == false)
{
RegionManager.SetRegionName(AssistantDialogContentControl, RegionNames.ImportAssistantMain);
RegionManager.SetRegionManager(AssistantDialogContentControl, regionManager);
}
}
public IViewModel ViewModel
{
get { return (IImportDialogViewModel)DataContext; }
set { DataContext = value; }
}
private void ImportDialogView_OnLoaded(object sender, RoutedEventArgs e)
{
}
}
}
importdialogviewmodel.cs
using Model;
using Modules.View.ImportAssistant;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Input;
using Infrastructure.Interfaces;
using Modules.Interfaces.ImportAssistant;
using Infrastructure;
using Prism.Interactivity.InteractionRequest;
namespace Modules.ViewModel.ImportAssistant
{
public class ImportDialogViewModel : BindableBase, IImportDialogViewModel
{
public IRegionManager RegionManager { get; set; }
private string _title;
public string Title
{
get { return _title; }
set
{
SetProperty(ref _title, value, nameof(Title));
}
}
public IView View { get; set; }
public IEnumerable<Data> ImportedData { get; set; }
private INotification _notification;
public INotification Notification
{
get { return _notification; }
set
{
var confirmation = value as ImportConfirmation;
if (confirmation != null)
{
//SetProperty(ref _notification, value, nameof(Notification));
_notification = confirmation;
RaisePropertyChanged(nameof(Notification));
}
}
}
public Action FinishInteraction { get; set; }
private bool? _dialogResult;
public bool? DialogResult
{
get { return _dialogResult; }
set
{
if (_dialogResult != value)
{
SetProperty(ref _dialogResult, value, nameof(DialogResult));
}
}
}
public ICommand AbortAssistantCommand { get; set; }
public ICommand NextCommand { get; set; }
public ICommand PreviousCommand { get; set; }
public ICommand FinishCommand { get; set; }
public ImportDialogViewModel(IImportDialogView view, IRegionManager r)
{
RegionManager = r;
View = view;
View.ViewModel = this;
InitializeCommands();
}
private void InitializeCommands()
{
AbortAssistantCommand = new DelegateCommand(() => { DialogResult = false; });
NextCommand = new DelegateCommand(() =>
{
var importDialogMainRegion = RegionManager.Regions.FirstOrDefault(r => r.Name == RegionNames.ImportAssistantMain);
if (importDialogMainRegion != null)
{
var firstActiveView = importDialogMainRegion.ActiveViews.FirstOrDefault();
string viewType = null;
if (firstActiveView is IImportStartView)
{
viewType = typeof(FileSelectionView).FullName;
}
else if (firstActiveView is FileSelectionView)
{
viewType = typeof(DataSelectionView).FullName;
}
if (string.IsNullOrEmpty(viewType) == false)
{
RegionManager?.RequestNavigate(RegionNames.ImportAssistantMain, viewType);
}
}
});
PreviousCommand = new DelegateCommand(() =>
{
var importDialogMainRegion = RegionManager.Regions.FirstOrDefault(r => r.Name == RegionNames.ImportAssistantMain);
if (importDialogMainRegion != null)
{
var firstActiveView = importDialogMainRegion.ActiveViews.FirstOrDefault();
}
});
FinishCommand = new DelegateCommand(() =>
{
DialogResult = true;
FinishInteraction?.Invoke();
});
}
}
}