Help with CommunityToolkit.MVVM

Jassim Al Rahma 1,596 Reputation points
2023-02-02T22:53:31.4766667+00:00

Hi,

I am trying to use the CommunityToolkit.MVVM but when setting the two values in my sample vcard and color it's not getting Binded later on. the Popup as you can clearly see in the sample.

https://www.softnames.com/temp/MVVMApp.zip

Kindly help..

Thanks,

Jassim

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

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 79,781 Reputation points Microsoft Vendor
    2023-02-03T02:59:33.0466667+00:00

    Hello,

    You can do this by changing type of color property to Microsoft.Maui.Graphics.Color in the MainViewModel.cs like following code.

    public partial class MainViewModel : ObservableObject
    {
        [ObservableProperty]
        private string vcard;
    
        [ObservableProperty]
        private Color color;
    }
    

    Then open your private void OnCounterClicked(object sender, EventArgs e) method of MainPage's background code and change your set color's way like following code.

    private void OnCounterClicked(object sender, EventArgs e)
        { 
           // ((MainViewModel)(this.BindingContext)).Vcard = "yahoo";
            vm.Vcard = "BEGIN:VCARD\nVERSION:3.0\nFN:John Doe\nTEL;TYPE=WORK,VOICE:(555) 555-5555\nEMAIL;TYPE=PREF,INTERNET:johndoe@example.com\nEND:VCARD";
    
            vm.Color = Color.FromArgb("#FFFF00");
    
            this.ShowPopup(new ContactQR(vm));    }
    

    In the end, please add   BindingContext = vm; in the ContactQR's background code.

        public ContactQR(MainViewModel vm)
        {
            InitializeComponent();
            BindingContext = vm;
        }
    

    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.