michaelscodingspot / WPF_MVVMC

A WPF framework for navigation between pages with MVC-like pattern
MIT License
64 stars 18 forks source link

Navigation Drawer #7

Closed LucaFagan closed 5 years ago

LucaFagan commented 5 years ago

Hi, I'm trying to use your library to create an application in outlook style with a left drawer for changing view and a content region with the content of the selected view. When user changes view I call ExecuteNavigation but I don't want every selected view in the stack, is it right to call ClearHistory before ExecuteNavigation?

Thanks

michaelscodingspot commented 5 years ago

Hi,

Why would you not want the view in the history stack? History saves page names and the ViewBag, but not the instance of View or ViewModel. Navigation-Parameter can be saved or not according to history mode.

If for whatever reason you'll want to clear history anyway, you can do it after ExecuteNavigation:

public class MyController : Controller
{
    public void Page(object parameter)
    {
        ExecuteNavigation();
        ClearHistory();
    }

EDIT: Ignore this advice, best NOT to clear history after ExecuteNavigation

While testing it, I found a small bug that "CanExecute" of GoBackCommand isn't refreshed after clear history. I'll release a fix today, so please update NuGet

michaelscodingspot commented 5 years ago

Released version 1.1.7 to fix the "CanExecute" issue after "ClearHistory"

LucaFagan commented 5 years ago

Hi, thanks for the reply.

What I want is that every view has only its sub pages in the history stack.

For example: in the left drawer I have selection for view1, view2, view3. Initially the user is in view1. He select a view1 item and move to that item page. When user select view2, the view2 page is displayed. I don't want the use can go back to view1.item. In other words a user can go back only if he is in a sub page of a view and the root of the history has to be that view page. Are you sure I have to clear the history after ExecuteNavigation and not before ?

michaelscodingspot commented 5 years ago

After some more thought, you are probably right, clearing the history after will also remove the current view, which doesn't make sense. So best to clear before.

Getting back to your question, it sounds like you need nested controllers. Each drawer will include a controller of its own. So in the main window/view:

<Button Command="{mvvmc:NavigateCommand ControllerID='Main', Action='Page1'}">View1</Button>
<Button Command="{mvvmc:NavigateCommand ControllerID='Main', Action='Page2'}">View2</Button>
<Button Command="{mvvmc:NavigateCommand ControllerID='Main', Action='Page3'}">View3</Button>

<mvvmc:Region ControllerID="Main" />

Now for each view, you will have an additional Region. For example, in Page1View.xaml:

<mvvmc:Region ControllerID="First" />

Now create a new folder called "First", pleace a "FirstController" inside and add inner pages. The back button you create should refer to the current drawer's controller. For example:

<Button Margin="5" Command="{mvvmc:GoBackCommand ControllerID='First'}">Back</Button>

There's an example to this if you clone the GitHub repo.

Hope this helps.

LucaFagan commented 5 years ago

Yes, that's how I thought to do. I'll have a look to the example.

Thanks.