i need to link the ispresented property of the flyoutpage with a command in a viewmodel any suggestions please as i think the property is failing

ruben dario rueda pineda 1 Reputation point
2022-07-28T21:33:58.743+00:00

i need to link the property ispresented from flyoutpage with viewmodel; the code I have is:

in the view i have:

   <ImageButton.GestureRecognizers>  
                           <TapGestureRecognizer Command="{Binding CommandBtnSearch}"/>                          
   </ImageButton.GestureRecognizers>  

in the ViewModel i have:

   bool flyoutstate;  
            
           //Propiedades  
     
           public bool FlyoutState  
           {  
               get { return flyoutstate; }  
               set {  
                   if (flyoutstate==value)  
                       { return; }  
                       flyoutstate = value;  
                       OnPropertyChanged();  
                   }  
           }  
     
     
   CommandBtnSearch = new Command(  
                   execute:() =>  
                   {  
                       FlyoutState=true;  
                       OnPropertyChanged();  
                        
                         
                   },  
                   canExecute:() =>  
                   {  
                       return !FlyoutState;  
                   });  

and at the view flyoutPage i have:

   <FlyoutPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"  
                xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
                xmlns:VMbinding="clr-namespace:Prueba.App.ViewModel"  
                IsPresented="{Binding FlyoutState, Mode=TwoWay}">  
     
   <FlyoutPage.BindingContext>  
           <VMbinding:HomePageViewModel/>   
       </FlyoutPage.BindingContext>  

this is my code but I see that it does not work, any help?

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

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 74,491 Reputation points Microsoft Vendor
    2022-07-29T05:20:39.967+00:00

    Hello,​

    i need to link the property ispresented from flyoutpage with viewmode

    You need to bind the same viewmodel for your View and FlyoutPage. After that, you can link the property between FlyoutPage and View

    You created a viewmodel called HomePageViewModel, please create a Singleton Pattern for your HomePageViewModel like following code.

       public static class ViewModelLocator  
           {  
               private static HomePageViewModel _myViewModel = new HomePageViewModel();  
               public static HomePageViewModel HomePageViewModelInstance  
               {  
                   get  
                   {  
                       return _myViewModel;  
                   }  
               }  
           }  
    

    Then remove <FlyoutPage.BindingContext> in your XAML. Setting bindingContext in your <FlyoutPage> background code.

       BindingContext = ViewModelLocator.HomePageViewModelInstance;  
    

    Please set bindingContext in your <FlyoutPage> background code as well. For example, I put <ImageButton> in the ContactsPage, So I add BindingContext like following code.

       public ContactsPage()  
       	{  
       		InitializeComponent();  
         
               BindingContext = ViewModelLocator.HomePageViewModelInstance;  
         
           }  
    

    Best Regards,

    Leon Lu


    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.