Pass value from FlyoutItem

Lloyd Sheen 1,476 Reputation points
2023-04-11T17:44:17.4966667+00:00

In my app I can navigate to a page and pass an object which that page is to work with. It also has a FlyoutItem which will navigate to that page (but with no object to deal with). The page has a button which will show a popup allowing the user to select an object to deal with. At present I can cause the method which the button will call but if I have gotten there with an object passed thru the following : await Shell.Current.GoToAsync($"{nameof(TeamsPage)}", navigationParameter); it will have as the background the display of the object sent as navigationParameter and will show the popup which is not necessary. If I use the FlyoutItem the user will at present have to click the button to get the popup to select an item to display. Is there a way to send (lets say a string as a parameter so that the ApplyQueryAttributes method is called (it is called using the above code) so that I will know that an item needs to be selected. The best way would be that the FlyoutItem has a property which can be set so that the app knows to show the popup without a button click.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,231 questions
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 39,391 Reputation points Microsoft Vendor
    2023-04-13T03:04:42.1666667+00:00

    Hello,

    By referring to the official documentation, we could see that there are two types of components in MAUI's flyout, flyoutitem and menuitem. Among them, menuitem allows custom click events or commands and passes parameters.

    Therefore, for your needs, it is more recommended to use to menuitem, so that you could use await Shell.Current.GoToAsync ($"{nameof(TeamsPage)}", navigationParameter) in the event to pass parameters, please refer to Menu items for more details.

    The MenuItem class has a Clicked event, and a Command property. Therefore, MenuItem objects enable scenarios that execute an action in response to the MenuItem being tapped.

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Jack1 0 Reputation points
    2023-04-12T06:40:38.7566667+00:00

    It sounds like you want to pass a parameter to a FlyoutItem in Xamarin Forms. According to Stack Overflow, you can use Xamarin.Essentials: Preferences to store the data at Main Shell and get/use it in the 'page1' and 'page2'. In Main Shell, store the data: public FlyoutMainShell (string name) { InitializeComponent (); Preferences.Set ("uesr_name", name); } In other page, get the value: var name = Preferences.Get("uesr_name", string.Empty); You can also use query parameters to pass data between pages. The navigation needs to have items that will navigate to the same page, but should pass a query parameter which the page will then use to filter the data on the page. Check here

    0 comments No comments