问题描述
说明
我在版本 4 中使用 Caliburn.Micro 并尝试从用户控件导航到另一个页面。 ShellView.xaml 有一个 <ContentControl x:Name="ActiveItem" />
元素和所有导航方法,如 DashboardView()
、GcsImportView()
... 只要我在 ShellView.xaml 中就可以工作。但是,如果我从用户控件(在 ContentControl 内)调用,则什么也不会发生。我什至没有得到错误。我可以多次按下按钮而没有任何反应。我希望有人能在这里帮助我。
更新
即使我尝试使用这些 site 中的代码,它也不起作用。在调试时,ActiveItem 值将被更改。这看起来像一个错误?
Shellviewmodel.cs
namespace GCS.viewmodels
{
public class Shellviewmodel : Conductor<object>//,IHandle<GcsImportProgressviewmodel>
{
private string _version = "v. " + Assembly.GetExecutingAssembly().GetName().Version.ToString();
public string Version
{
get { return _version; }
}
public Shellviewmodel(/*IEventAggregator eventAggregator*/)
{
DashboardView();
//eventAggregator.SubscribeOnUIThread(this);
}
public void DashboardView() => ActivateItemAsync(new Dashboardviewmodel());
public void GcsImportView() => ActivateItemAsync(IoC.Get<GcsImportviewmodel>());
public void GcsExportView() => ActivateItemAsync(new GcsExportviewmodel());
public void ChangeView<T>() => ActivateItemAsync(IoC.Get<T>());
//public Task HandleAsync(GcsImportProgressviewmodel message,CancellationToken cancellationToken)
//{
// throw new NotImplementedException();
//}
}
}
用户控件构造器
public GcsImportviewmodel(Shellviewmodel shell,IGcsRepository gcsRepository/)
{
filePath = "Bitte GCS Excel Datei auswählen";
databases = new ObservableCollection<GcsTable>(gcsRepository.GetAllTables());
selectedDatabase = databases.FirstOrDefault();
this.gcsRepository = gcsRepository;
}
用户控制改变视图的方法
public void ClickImport()
{
shell.ChangeView<GcsImportProgressviewmodel>();
}
解决方法
您似乎是在 ChangeView
的不同实例上调用 ShellViewModel
。
您应该在引导程序中将视图模型注册为单例:
container.Singleton<ShellViewModel,ShellViewModel>();