Using MVVM in .Net Maui

Ronald Rex 181 Reputation points
2022-12-14T17:48:35.4+00:00

Hi Friends. I am new to the MVVM architecture and how to use it with a .Net Maui application. Here is the use case... I have an entry field that I want to bind to a model and whatever the user types in the entry field I want to pass that field to a function which is bound to a field on the view. Any help would be greatly appreciated. Thanks !!!

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

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 75,431 Reputation points Microsoft Vendor
    2022-12-15T06:07:10.79+00:00

    Hello,

    I have an entry field that I want to bind to a model and whatever the user types in the entry field I want to pass that field to a function which is bound to a field on the view

    You can do this by INotifyPropertyChanged and WeakReferenceMessenger, if your MAUI based on the .Net 7. Note: Before using WeakReferenceMessenger, please install CommunityToolkit.Mvvm nugget packages, if your MAUI based on the .Net 6, you could use messages to publish and subscribe it.

    For example, if you bind the Entry's Text like <Entry Text="{Binding MyModel.Name}"></Entry>, your model can do this by INotifyPropertyChanged for this name property like following code. When text is changed in Entry, you could send the WeakReferenceMessenger.

       public class MyModel : INotifyPropertyChanged  
       {  
           private string _name;  
           public string Name  
           {  
               get  
               {  
                   return _name;  
               }  
               set  
               {  
                   if (_name != value)  
                   {  
                       _name = value;  
         
                     
                       NotifyPropertyChanged();  
                   }  
               }  
           }  
           public event PropertyChangedEventHandler PropertyChanged;  
          protected virtual void NotifyPropertyChanged([CallerMemberName] string propertyName = "")  
           {  
         
               //Send this value to your function which is bound to a field on the view.  
               if(propertyName=="Name"){  
                          WeakReferenceMessenger.Default.Send(new SendObjectMessage(value));  
               }  
              
         
               PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));  
           }  
       }  
    

    Before using the WeakReferenceMessenger, please create a class called SendObjectMessage.cs extend ValueChangedMessage

       public class SendObjectMessage : ValueChangedMessage<object>  
       {  
           public SendObjectMessage(object r) : base(r)  
           {  
           }  
       }  
    

    In the end, you can get the value in the function with following code.

       WeakReferenceMessenger.Default.Register<SendObjectMessage>(this, (r, m) =>  
               {  
                  var getValue = m.Value;  
                   Console.WriteLine("this is a weak message" + getValue);  
               });  
    

    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.


0 additional answers

Sort by: Most helpful

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.