-
I have a few commands on my I am hoping to add an event handler to the regionNavigationService.Navigated += RegionNavigationService_Navigated;
private void RegionNavigationService_Navigated(object sender, RegionNavigationEventArgs e)
{
this.MyCommand.RaiseCanExecuteChanged();
} The event doesn't seem to be firing though. I use the this.regionManager.RequestNavigate("ContentRegion", "MyView"); Any ideas on why the Navigated event isn't working for me? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I found a solution to my problem. In my MainWindowViewModel public DelegateCommand LoadedCommand { get; private set; }
public MainWindowViewModel(IRegionManager regionManager, IRegionNavigationService regionNavService)
{
this.regionManager = regionManager;
this.regionNavigationService = regionNavigationService;
this.LoadedCommand = new DelegateCommand(this.Loaded);
}
public void Loaded()
{
this.regionNavigationService.Region = this.regionManager.Regions["ContentRegion"];
this.regionNavigationService.Navigated += RegionNavigationService_Navigated;
this.regionNavigationService.RequestNavigate("LoginView");
}
private void RegionNavigationService_Navigated(object sender, RegionNavigationEventArgs e)
{
// This is the main thing I needed
this.SomeOtherCommand.RaiseCanExecuteChanged();
} MainWindow.xaml <Window
...
xmlns:i="http://schemas.microsoft.com/xaml/behaviors" />
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<i:InvokeCommandAction Command="{Binding LoadedCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
... LoginViewModel.cs public class LoginViewModel : BindableBase, INavigationAware
{
private IRegionNavigationService regionNavigationService;
public DelegateCommand CompleteLoginCommand { get; private set; }
public LoginViewModel()
{
this.CompleteLoginCommand = new DelegateCommand(this.CompleteLogin);
}
public void CompleteLogin()
{
this.regionNavigationService.RequestNavigate("DashboardView");
}
public void OnNavigatedTo(NavigationContext navigationContext)
{
// Only use the navigation service from the context, do not inject it!
this.regionNavigationService = navigationContext.NavigationService;
}
public bool IsNavigationTarget(NavigationContext navigationContext)
{
return true;
}
public void OnNavigatedFrom(NavigationContext navigationContext) { }
} |
Beta Was this translation helpful? Give feedback.
I found a solution to my problem.
In my
MainWindowViewModel
I inject theIRegionManager
&IRegionNavigationService
. In the view, I bind theLoaded
event to aLoadedCommand
, which sets the Region on theIRegionNavigationService
, sets up an event handler for theNavigated
event, and then navigates to the initial view of the app. Then, when viewmodels need to navigate, I get a reference to the sameIRegionNavigationService
from theNavigationContext
parameter from theINavigationAware.NavigatedTo
function.MainWindowViewModel