简体   繁体   中英

Caliburn.Micro Navigation from Usercontrol over ShellViewModel

Description

I use the Caliburn.Micro in version 4 and try to navigatie from a Usercontrol to another page. The ShellView.xaml has a <ContentControl x:Name="ActiveItem" /> element and all navigationmethods like DashboardView() , GcsImportView() ... work aslong I am inside the ShellView.xaml. But if I call from a Usercontrol (inside the ContentControl) nothing happens. I don´t even get a error. I can push the Button thousend times without any respond. I hope somebody can help me here.

Update

Even if I try the code from these site it doesn´t work. On debugging the ActiveItem value will be changed. That looks like a bug?

在此处输入图片说明

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();
        //}
    }
}

UserControl Constructor

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;
        }

Usercontrol Method to change the view

public void ClickImport()
{
    shell.ChangeView<GcsImportProgressViewModel>();
}

It seems like you are calling ChangeView on a different instance of the ShellViewModel .

You should register the view model as a singleton in your bootstrapper:

container.Singleton<ShellViewModel, ShellViewModel>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM