I have a need to switch the view being displayed based on a certain condition. I have implemented the switching logic in the constructor of the ViewModel
I am implementing IRegionMemberLifetime on the View and setting KeepAlive to false so that I always get a new instance of the View and the ViewModel.
But for some reason, when I click on the Navigation Button, my breakpoint at KeepAlive never reaches and I get the MainView instead of the WelcomeView.
Could you please help me out?
Here is the code for your reference:
Navigation Button:
<Controls:SignedButton VerticalAlignment="Top" Width="275" Height="45" Foreground="#FFFFFF" LeftSign="<" Text="Back to Accounts" TextSize="20" ButtonBackground="#666666" HoverBackground="#0FBDAC" HoverOpacity="1" Margin="0,25,0,0" Command="{x:Static Infrastructure:ApplicationCommands.NavigateCommand}" CommandParameter="{x:Type Views:MainView}"/> View Model:
public class MainViewModel : ViewModel, IMainViewModel { private readonly IUnityContainer _container; public MainViewModel(IMainView view, IUnityContainer container) : base(view) { _container = container; Accounts = new List<Account>(); View.ViewModel = this; if (Accounts.Any()) return; IView welcomeView = _container.Resolve<IWelcomeViewModel>().View; welcomeView.ViewModel = this; UpdateView(welcomeView); } public IList<Account> Accounts { get; private set; } } View Model Base:
public abstract class ViewModel : IViewModel { protected ViewModel(IView view) { View = view; } protected virtual void OnPropertyChanged(string propertyName) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); } protected void UpdateView(IView view) { View = view; } public event PropertyChangedEventHandler PropertyChanged; public IView View { get; private set; } } View:
public partial class MainView : UserControl, IMainView, IRegionMemberLifetime { public MainView() { InitializeComponent(); } public IViewModel ViewModel { get { return (IMainViewModel) DataContext; } set { DataContext = value; } } public bool KeepAlive { get { return false; } } } Shell View Model:
public class ShellViewModel : ViewModel, IShellViewModel { private readonly IRegionManager _regionManager; public ShellViewModel(IShellView view, IRegionManager regionManager) : base(view) { _regionManager = regionManager; NavigateCommand = new DelegateCommand<object>(Navigate); ApplicationCommands.NavigateCommand.RegisterCommand(NavigateCommand); } private void Navigate(object navigatePath) { if (navigatePath != null) { _regionManager.RequestNavigate(Regions.Main, navigatePath.ToString()); } } public DelegateCommand<object> NavigateCommand { get; private set; } } Thanks a lot for looking into this :)
No comments:
Post a Comment
Send us your comment related to the topic mentioned on the blog