By default create a new instance a the view before to navigate to it or reuse the existing one (discriminate by view type) if it has already been created.
Used to automatically wire the DataContext of a view to an instance of a ViewModel using a standard naming convention.
MyView.xaml
<!-- automatically wire the DataContext of MyView to an instance of MyApp.ViewModels.MyViewModel --><UserControlx:Class="MyApp.Views.MyView"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:viewModels="clr-namespace:MyApp.ViewModels"xmlns:prism="http://prismlibrary.com/"mc:Ignorable="d"d:DataContext="{d:DesignInstance viewModels:MyViewModel}"prism:ViewModelLocator.AutoWireViewModel="True">
This convention assumes:
that ViewModels are in the same assembly as the view types
that ViewModels are in a .ViewModels child namespace
that Views are in a .Views child namespace
that ViewModel names correspond with View names and end with ViewModel
Parameter can't be of value type (int, double, bool, etc). Use instead the equivalent nullable type.
RaiseCanExecuteChanged
privateboolisEnabled;
publicbool IsEnabled
{
get => isEnabled;
set
{
SetProperty(refisEnabled, value);
SubmitCommand.RaiseCanExecuteChanged();
}
}
ObservesProperty
Whenever the value of the supplied property changes, the DelegateCommand will automatically call RaiseCanExecuteChanged to notify the UI of state changes.
If your CanExecute is the result of a simple Boolean property, you can eliminate the need to declare a CanExecute delegate, and use the ObservesCanExecute method instead. ObservesCanExecute will not only send notifications to the UI when the registered property value changes but it will also use that same property as the actual CanExecute delegate.
publicvoidOnInitialized(IContainerProvidercontainerProvider)
{
varviewA = containerProvider.Resolve<ViewA>();
varcontentRegion = this.regionManager.Regions["ContentRegion"];
contentRegion.Add(viewA);
contentRegion.Add(anotherView); // add another view to the region
contentRegion.Activate(anotherView); // tell the region to display this view
contentRegion.Deactivate(anotherView); // tell the region not to display this view anymore
}
Navigation
Move between views in a region based on url.
By default create a new instance a the view before to navigate to it or reuse the existing one (discriminate by view type) if it has already been created.
ModuleAModule.cs
publicvoidRegisterTypes(IContainerRegistrycontainerRegistry)
{
// register the ViewA under the string key ViewA (name of the view by default)
containerRegistry.RegisterForNavigation<ViewA>();
containerRegistry.RegisterForNavigation<ViewB>();
// associate a view-model while register
containerRegistry.RegisterForNavigation<ViewA, ViewAViewModel>();
}
<ButtonCommand="{Binding NavigateCommand}"CommandParameter="ViewA"Content="Navigate to A" /><ButtonCommand="{Binding NavigateCommand}"CommandParameter="ViewB "Content="Navigate to B" />
REM ModuleA post build eventxcopy /y "$(TargetDir)$(TargetName)$(TargetExt)" "$(SolutionDir)$(SolutionName)\$(OutDir)Modules\"
REM xcopy /y C:\PrismNetCore\ModuleA\bin\Debug\net5.0-windows\ModuleA.dll C:\PrismNetCore\PrismNetCore\bin\Debug\net5.0-windows\Modules\
the event instance keeps a strong reference to the subscriber instance, thereby not allowing it to get garbage collected.
false
default value. The event maintains a weak reference to the subscriber instance, thereby allowing the garbage collector to dispose the subscriber instance when there are no other references to it. When the subscriber instance gets collected, the event is automatically unsubscribed.