microsoft / TemplateStudio

Template Studio accelerates the creation of new WinUI 3, WPF, and UWP apps using a wizard-based experience.
Other
2.65k stars 459 forks source link

NavigationService should support common save scenarios #4702

Closed mstasak closed 11 months ago

mstasak commented 11 months ago

Is your feature request related to a problem? Please describe.

I am trying to build a multi-page app, with one page "Tasks" implementing a master-detail table editor. I need to support saving, when the user edits a task detail and then either closes the app, navigates to another page, or navigates to another task. The first two seem unsupported. By navigation, of course, I mean navigation between tabs on a page, or between pages within an app.

Describe the solution you'd like

I'd like two new events, deliverable to a page or its view controller. First, OnUnloading, to be delivered to the page/view or associated viewcontroller prior to disconnecting that page/view from the UI, whether due to navigating away or closing the app. Second, OnNavigating, to be delivered to the page prior to unloading and navigating to another page. Both should probably offer a cancel EventArgs property, although undefined behavior would be acceptable if the app is force-closed..

Also, if possible, it would be wonderful to have a Page.SaveAllBindingUpdates() method which saves updates from all 2-way-bound (or rarely, view->VC bound) controls back to their viewcontroller source properties. Perhaps also a Page.HasPendingBindingUpdates() method.

I really like Template Studio, it is powerful and flexible, but it doesn't seem to support typical save behaviors in a data editing app. Minimally, I want to either automatically save changes prior to navigation or app close activities, or suspend them while offering a save/discard/cancel dialog to the user.

Describe alternatives you've considered

No response

Additional context

Assume the Tasks page includes details TextBox controls which only save binding data on loss of focus.

Applies to the following platforms:

mrlacey commented 11 months ago

A page already has an OnNavigatingFrom method (which supports cancellation) and OnNavigatedFrom.

Historically, things like saving data have been left to the developer and not included in TemplateStudio because there are many potential variations and uncertainties.

Because the above will vary by app (and even within a single app), it's not deemed appropriate for TempalteStudio to include a single way of doing this. Additionally, it's not practical to provide multiple options due to the practicalities of creating and supporting them or presenting them in a way that would make it easy for people using the wizard to be confident in knowing what they would get based on their selections.

The official Windows Developer blog also includes a recent post that may be useful/relevant.

mstasak commented 11 months ago

Thank you. Page.OnNavigatingFrom does work with some caveats. Some other method would be needed to detect app closing. e.Cancel = True will prevent navigating to another tab (NavigationViewItem) in a single page app with tab navigation, although the NavigationView will then be out of sync, reflecting the attempted change as if it were not canceled. The navigation attempt does not remove focus from an active TextBox, so it will not update its ViewController data source under the default binding update trigger mode for TextBoxes (UpdateSourceTrigger=LostFocus). Setting AllowFocusOnInteraction="True" on the NavigationView and NavigationViewItems does not help this. I've seen kludgy fixes involving changing the focused control prior to navigating, but wanted to avoid this (too hackish and might lose cursor and scroll positions). I guess I'll continue wrestling with this.

mrlacey commented 11 months ago

@mstasak your ideas for changes to the way navigation works and issues with updating bindings may be better addressed by changes to the actual WinUI Framework, as they aren't things that can be changed as part of TemplateStudio. Discussion or issues there may be a better place to see if what you're after could be made possible.: https://github.com/microsoft/microsoft-ui-xaml