Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introducing Simplified Navigation and Dialog Handling with XAML Commands #2988

Conversation

AbderrahmaneAhmam
Copy link

Hello Prism,

I hope this message finds you well. I'm thrilled to present a new feature that enhances the navigation and dialog handling process in Prism WPF. This enhancement allows developers to seamlessly manage navigation and dialog operations directly through XAML commands, eliminating the need to manually add handlers in the ViewModel.

Here's a breakdown of the changes I've made:

New Files Added:

  • CloseDialogCommandParameter: Essential parameters for closing a dialog.

  • NavigateToViewCommandParameter: Essential parameters for navigating to a view.

  • ShowDialogCommandParameter: Essential parameters for showing a dialog.

  • DialogCommands: Commands for dialog operations.

    • ShowDialogCommand: Shows a dialog using ShowDialogCommandParameter.
      • Single paramter

        <Button
            Margin="5,0"
            VerticalAlignment="Center"
            Command="{x:Static prism:DialogCommands.ShowDialogCommand}"
            CommandParameter="{Binding ElementName=TextParam, Path=Text, Converter={prism:ShowDialogParameterConverter DialogName='TestDialogView', Key='Message'}}"
            Content="Open Dialog" />
      • Multi parameters

       <Button
           Margin="5,0"
           VerticalAlignment="Center"
           Command="{x:Static prism:DialogCommands.ShowDialogCommand}"
           CommandParameter="{Binding ElementName=TextParam, Path=Text, Converter={prism:ShowDialogParameterConverter, Key='Message'}}"
           Content="Open Dialog">
         <Button.CommandParameter>
           <MultiBinding Converter="{prism:ShowDialogParameterConverter DialogName='TestDialogView'}">
             <Binding
                 Converter="{prism:KeyValueParamConverter Key='Message'}"
                 ElementName="TextParam"
                 Path="Text" />
             <Binding Converter="{prism:KeyValueParamConverter Key='SubMessage'}">
               <Binding.Source>
                 <system:String>Test</system:String>
               </Binding.Source>
             </Binding>
           </MultiBinding>
         </Button.CommandParameter>
       </Button>
    • CloseDialogCommand: Closes a dialog using CloseDialogCommandParameter.
      <Button
          Margin="5,0"
          VerticalAlignment="Center"
          Command="{x:Static prism:DialogCommands.ShowDialogCommand}"
          CommandParameter="{Binding ElementName=TextParam, Path=Text, Converter={prism:ShowDialogParameterConverter DialogName='TestDialogView', Key='Message'}}"
          Content="Open Dialog" />
  • NavigationCommands: Main commands for navigation.

    • GoBackCommand: Navigates back in a region using a string parameter representing the region name.
      <Button
          Margin="5,0"
          VerticalAlignment="Center"
          Command="{x:Static prism:NavigationCommands.GoBackCommand}"
          CommandParameter="MainRegion"
          Content="Go Back" />
    • GoForwardCommand: Navigates forward in a region using a string parameter representing the region name.
      <Button
          Margin="5,0"
          VerticalAlignment="Center"
          Command="{x:Static prism:NavigationCommands.GoForwardCommand}"
          CommandParameter="MainRegion"
          Content="Go Back" />
    • NavigateToCommand: Navigates to a view in a specific region using NavigateToViewCommandParameter.
      • Single paramter

        <Button
            Margin="5,0"
            VerticalAlignment="Center"
            Command="{x:Static prism:NavigationCommands.NavigateToCommand}"
            CommandParameter="{Binding YourParameter, Converter={prism:ParamsNavigationConverter RegionName='MainRegion', TargetView='PageAView', Key='Message'}}"
            Content="Open page" />
      • Multi parameters

        <Button
            Margin="5,0"
            VerticalAlignment="Center"
            Command="{x:Static prism:NavigationCommands.NavigateToCommand}"
            Content="Page B with parameters">
            <Button.CommandParameter>
                <MultiBinding Converter="{prism:ParamsNavigationConverter RegionName='MainRegion', TargetView='PageBView'}">
                    <Binding
                        Converter="{prism:KeyValueParamConverter Key='Message'}"
                        ElementName="TextParam"
                        Path="Text" />
                    <Binding Converter="{prism:KeyValueParamConverter Key='SubMessage'}">
                        <Binding.Source>
                            <system:String>Test</system:String>
                        </Binding.Source>
                    </Binding>
                </MultiBinding>
            </Button.CommandParameter>
        </Button>

Benefits:

Simplifies navigation and dialog operations.
No need to create additional handlers in the ViewModel for common operations.
Enhances code readability and maintainability.

Testing:

I'm open to adding specific tests for this feature. Please guide me on where you would recommend including them.

Future Plans:

If this feature is accepted, I plan to extend the same functionality for MAUI and Xamarin in a modular and standardized way.
I believe this feature will significantly improve the developer experience and streamline common operations. If you have any feedback or suggestions, please let me know. I'm more than willing to address any concerns or make further improvements.

Thank you for your time and consideration. Looking forward to your feedback!

Best regards.

…g global commands

- DialogCommands
ShowDialog
CloseDialog
- NavigationCommands
NavigateToCommand
GoBackCommand
GoForwardCommand
@dansiegel
Copy link
Member

Thanks for the PR @AbderrahmaneAhmam

I'm going to close this PR for now, however please don't let it discourage you from following up with future PRs. A couple of things to help you here...

  1. Before opening a PR with a new API please be sure to start a discussion or open an issue to propose a new API. This will keep you from investing your precious time on a feature or implementation that we aren't likely to merge.
  2. We would love to discuss your ideas for enhancing the API for WPF. Keep in mind we already have some XAML helpers for Xamarin.Forms and .NET MAUI for Navigation and Dialogs. As a big theme of Prism 9.0 is unifying the API I would suggest looking at what we are already doing there for inspiration on API design for WPF and Uno Platform. It isn't always possible to match the API from one platform to another however the goal is always to try to match as close as we can. https://github.com/PrismLibrary/Prism/blob/master/src/Maui/Prism.Maui/Dialogs/Xaml/ShowDialogExtension.cs

@dansiegel dansiegel closed this Oct 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants