【发布时间】:2016-05-30 09:26:28
【问题描述】:
我在我的 MVVM WPF 应用程序中使用 Caliburn.Micro 3。我按照documentation 和提供的示例成功地在我的应用中实现了导航。
但是,我想遵循SOLID principles,并且我认为使用 ShellViewModel 作为 Conductor 违反了单一职责原则。
为了解决这个问题,我创建了一个“服务”来控制我的导航,但是我无法显示 ActiveItem。当我导航时,我将 ViewModel 名称作为字符串而不是与之关联的 View。
public class NavigationService : Conductor<IScreen>, INavigationService
{
public void GoTo<T>() where T : IScreen
{
var viewModel = IoC.Get<T>();
ActivateItem(viewModel);
}
}
我在“ShellViewModel”中使用它。
public class ShellViewModel : PropertyChangedBase
{
private readonly INavigationService _navigationService;
public HomeViewModel(INavigationService navigationService)
{
_navigationService = navigationService;
}
public INavigationService NavigationService => _navigationService;
public void ShowChartPage() => _navigationService.GoTo<TimeSeriesViewModel>();
}
我的 ShellView 中的 ContentControl:
<ContentControl Content="{Binding NavigationService.ActiveItem}" />
我错过了什么吗?
【问题讨论】:
标签: c# wpf mvvm caliburn.micro