Decoupled Code with MessagingCenter in Xamarin.Forms

แชร์
ฝัง
  • เผยแพร่เมื่อ 12 ก.ค. 2024
  • With MessagingCenter in Xamarin.Forms you gain a publish-subscriber system out of the box. With this you can send messages across your application and execute code while doing so. This allows you to loosely couple your code in certain areas, but it comes with a couple of gotchas!
    In this video we will learn what it's all about and what to be aware of.
    💝 Join this channel to get access to perks:
    th-cam.com/channels/ral.html...
    🛑 Don't forget to subscribe to my channel for more amazing content: th-cam.com/users/GeraldVerslui...
    🐦 Follow me on Twitter: / jfversluis
    🔗 Links
    Sample code Repo: github.com/jfversluis/XFMessa...
    MessagingCenter Docs: docs.microsoft.com/xamarin/xa...
    Databinding Playlist: • Everything You Need on...
    Xamarin Forms Repo: github.com/xamarin/Xamarin.Forms
    ⏱ Timestamps
    0:00 Intro
    0:36 Sample App Outline
    1:25 Implement Sample App UI
    4:10 Implement Sample Code
    4:30 Explore MessagingCenter APIs
    6:13 Important Note on Memory Leaks!
    6:58 Implement MessagingCenter.Subscribe
    13:30 Implement MessagingCenter.Send
    15:00 Run Sample App And See Results!
    16:08 Important Note On Duplicate Subscriptions!
    17:08 Outro
    🎥 Video edited with Camtasia (ref): techsmith.z6rjha.net/AJoeD
    🙋‍♂️ Also find my...
    Twitter: / jfversluis
    Blog: blog.verslu.is
    Twitch: / jfversluis
    All the rest: jfversluis.dev
    #XamarinForms #MessagingCenter #Xamarin #pubsub
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 88

  • @TheNewfieSoldier
    @TheNewfieSoldier 3 ปีที่แล้ว +9

    I'm in a software intern position, and these tutorials are absolutely lifesaving, keep it up!

    • @jfversluis
      @jfversluis  3 ปีที่แล้ว +2

      Wow Ian! Thank you so much for letting me know! I really appreciate that. I love helping upcoming developers get going. I hope you'll do well!

  • @draevyn
    @draevyn 2 ปีที่แล้ว +1

    Exactly what I was looking for, huge help! Thank you!

    • @jfversluis
      @jfversluis  2 ปีที่แล้ว

      Perfect, glad to be of help!

  • @zulfikarelran1340
    @zulfikarelran1340 ปีที่แล้ว

    I have subscribed you channel man! 😁
    Please keep continue your works, your channel is very useful.

    • @jfversluis
      @jfversluis  ปีที่แล้ว

      Thank you friend 🫡 and welcome!

  • @rodrigosantosbarbosa629
    @rodrigosantosbarbosa629 2 ปีที่แล้ว

    Hi Gerald! Thank you for this video!! 🙂

    • @jfversluis
      @jfversluis  2 ปีที่แล้ว

      You're most welcome!

  • @sps014
    @sps014 3 ปีที่แล้ว

    Great content as always

  • @yuriyholembyovskyy1223
    @yuriyholembyovskyy1223 3 ปีที่แล้ว +2

    Hi Gerald.
    Great content, as always))
    It would be very nice to see the comparison (features and options) of different navigation services. For example from Prism, Mvvm Light, TinyMvvM, or a custom navigation service.
    But without Shell navigation service

    • @jfversluis
      @jfversluis  3 ปีที่แล้ว

      Great suggestion! But a lot of work. I'd love to do it, but not sure if I can find the time anytime soon :)

  • @winkstreaming2164
    @winkstreaming2164 2 ปีที่แล้ว +1

    Thanks for this video.
    One thing that would be nice is if you could do a video on MVVM, but look at how MVVM is also really about apps being springy. For example the M V is also about the app always being responsive and the VM is really where the work is done. Looking at how MVVM is more from a threading an operations perspective not that silly M V VM that everyone always tries to explain.

  • @alexanderm8169
    @alexanderm8169 3 ปีที่แล้ว

    Great Content, keep up the work!
    I'd love it if you could consider if you could go through an example on how to tackle libraries when you wish to for example make a scrollview horizontal and vertical and that is zoomable. Only if that is something that is intrssting for you

    • @jfversluis
      @jfversluis  3 ปีที่แล้ว +1

      Hi Alexander! Thanks so much and thanks for the subscribe, appreciate it!
      Not sure what you mean... Are you looking for a library that offers a control that has a horizontal and vertical orientation and can be zoomed? Do I understand you correctly then? What is your use case for this if I may ask?

    • @alexanderm8169
      @alexanderm8169 3 ปีที่แล้ว

      @@jfversluis sorry for being super unclear, I was specifically mentioning zooming and scrollview because these two wont go hand in hand in xamarin forms because one will override the other unless you use a library and create a custom render. An example could be a detailpage with an image and some text and once the image is clicked to zoom into it with a pinch.
      I'd be super happy if you'd consider do a video on this where you show us the fundamentals of using libraries. Downsides and upsides. Like you get a nice feature but it's not 100 percent it will update to the new release that's coming 😅.

  • @k.koustas
    @k.koustas 3 ปีที่แล้ว +1

    Hi Gerald,
    Congratulations on for your work, I love it.
    Would you consider to create a tutorial about Push Notifications?

    • @jfversluis
      @jfversluis  3 ปีที่แล้ว +2

      Why thank you! Lovely to hear.
      I've had the push notification request a good number of times now :D I definitely want to do it, but it will be multi-part and more work and I can't seem to find the time just yet... I was thinking Azure NotificationHub, would that work for you?

    • @k.koustas
      @k.koustas 3 ปีที่แล้ว +1

      @@jfversluis Of course this is an excellent alternative. I am looking forward to that.
      Thank you very much

  • @dreamhunter2895
    @dreamhunter2895 3 ปีที่แล้ว +3

    Sir, Please help to make one tutorial for Mobile Database Synchronization with a REST API

    • @jfversluis
      @jfversluis  3 ปีที่แล้ว +1

      That's a tough one to crack :D I think everyone struggles with that one ;)

  • @hadirotafoua5381
    @hadirotafoua5381 2 ปีที่แล้ว +1

    wow this is nice .. Please can you make a video with xamarin/signalR

  • @trevorkobilo2480
    @trevorkobilo2480 3 ปีที่แล้ว +1

    Great stuff. Would you know whether the MessagingCenter also works in Prism for Xamarin.Forms?

    • @jfversluis
      @jfversluis  3 ปีที่แล้ว

      Thank you! I wouldn't know why it shouldn't work. You might want to check if they have something similar though. I can imagine they would have and then to have a better integration you might want to use that.

  • @pindrusekral5428
    @pindrusekral5428 ปีที่แล้ว

    Thanks you, i read whole microsoft documentation and i still waisted 2.5 hours until you saved me.

    • @jfversluis
      @jfversluis  ปีที่แล้ว

      Sorry you wasted some time but still glad I was able to help!

  • @JuanSebastianR21
    @JuanSebastianR21 3 ปีที่แล้ว

    Always a great support, maybe something with signal R

    • @jfversluis
      @jfversluis  3 ปีที่แล้ว +1

      SignalR is awesome stuff! I'll see about that one :)

  • @andrewtruckle1695
    @andrewtruckle1695 3 ปีที่แล้ว +1

    Slowly getting my head around this one! Thanks. Question. I have used the constructor in each page to create a send / subscribe pair. For this to work I have to define the two pages first, in reverse order, so that page 2 was already "listening" when page 1 read in the data and "sent" it. Working. I guess that is acceptable approach. The text that you had as "tick" can this be anything or must it be a complicated unique string like a GUID?

    • @jfversluis
      @jfversluis  3 ปีที่แล้ว +1

      Can be anything as long as it's unique

    • @andrewtruckle1695
      @andrewtruckle1695 3 ปีที่แล้ว

      @@jfversluis Unique within my app? Or wider scope?

    • @jfversluis
      @jfversluis  3 ปีที่แล้ว +1

      Within your app. It doesn’t go anywhere. Just see it as the address that you write on a postcard. With this string it knows where to deliver

  • @LobsterHarry
    @LobsterHarry 3 ปีที่แล้ว +1

    I am using the MessagingCenter already, especially in a kind of global ErrorHandling. Very useful, but I am not sure, if it is good style to use it. Aren´t there any disadvantages ?

    • @jfversluis
      @jfversluis  3 ปีที่แล้ว +1

      It's all relative and maybe in this case also dependant on if you're working on something alone or with a team. The only real disadvantage I see immediately is said loose coupling. You'll have to CTRL+F the actual message name to see where the subscriptions are and how they behave. Since those are based on strings, it's an easy thing to break but hard to track down :)
      So is it good style? I think a lot of people tend to avoid this if they can, but if it works for you, then it's totally fine!

  • @jean-baptistedioli1692
    @jean-baptistedioli1692 2 ปีที่แล้ว

    Yes, it's an interesting question about when to use the messaging center and when to use the dependency center. Did you already make the video?

    • @jfversluis
      @jfversluis  2 ปีที่แล้ว +2

      I didn't make that one yet! Let me see if I can get to that :)

  • @alexanderspiegel6747
    @alexanderspiegel6747 3 ปีที่แล้ว

    Unfortunately, TH-cam does not allow to like the video more than once. But I would consider it as useful feature :-) Why ? There are 3 reasons:
    1st "like" is for short and professional presentation of the topic
    2nd "like" is for comparing advantages and disadvantages of the presented feature - message center this time
    3rd "like" is for sense of humor. I believe it is very important as well.
    So here is my "like", Gerald. Unfortunately, only one.

    • @jfversluis
      @jfversluis  3 ปีที่แล้ว

      Wow! Thank you Alexander! I appreciate you taking the time for letting me know, this is what I do it for. Maybe not multiple likes, but make sure you tell everyone so I can grow those subscribers then!

    • @alexanderspiegel6747
      @alexanderspiegel6747 3 ปีที่แล้ว +1

      @@jfversluis I will do my best to share the info about your channel, but can’t promise immediate feedback

    • @jfversluis
      @jfversluis  3 ปีที่แล้ว +1

      @@alexanderspiegel6747 haha no worries, any effort is appreciated!

  • @macmcmillen6282
    @macmcmillen6282 2 ปีที่แล้ว

    Late to the party so apologies if too late to post a question. How do you handle updating a page on Xamarin iOS using the OnAppearing method when that method gets called AFTER the page renders? On Android OnAppearing gets called before the page renders, so no issue there. I'm working with a Xamarin project I inherited from a previous development team. No MVVM was used. All code behind. Hope you can help. Thanks!

    • @jfversluis
      @jfversluis  2 ปีที่แล้ว +1

      Never too late to post a question but not sure if this is the ideal place to do it ;) Maybe this Xamarin Community Toolkit effect can be of service: docs.microsoft.com/en-us/xamarin/community-toolkit/effects/lifecycleeffect

    • @macmcmillen6282
      @macmcmillen6282 2 ปีที่แล้ว

      @@jfversluis Thanks very much. I greatly appreciate the reply/advice.

  • @checox1909
    @checox1909 3 ปีที่แล้ว

    Hii Gerald, Nice video, I want to ask you something, some days ago I was been working on an app using Xamarin Forms, I don't remember what, but I was going to use MC to do something and my boss stopped me and told me "Don't use that, is not a good code", it's not MC a good code, isn't? when can I use that and when not?

    • @jfversluis
      @jfversluis  3 ปีที่แล้ว +1

      Hey Steven!
      I think I get what he means. I'm not sure if I mention that in this video, but I only also use it as a last resort. Using MessagingCenter causes a lot of undiscoverable code. Since you subscribe through just a string identifier you never get a good overview of where the messages are being sent or received. That might cause undesired behavior that is hard to diagnose or debug. Often times the MessagingCenter is not even needed and there is other, better ways. I don't think there is any official guidance, but use it only when reaaaaally necessary :)

    • @checox1909
      @checox1909 2 ปีที่แล้ว

      @@jfversluis I did this question long time ago, 11 months to be exact and you answered me and I didn’t realize, sorry, but reading your answer now, I understand so much better why not use it when is not necessary, thanks god I didn’t use it at that time also 😅😅😅 and I found other ways to avoid using MC

  • @sbusisozamowakhe4470
    @sbusisozamowakhe4470 2 ปีที่แล้ว

    hi , i need help with a solution on how to send different messages from the main form to...lets say to different users saved on another xemarin form

    • @jfversluis
      @jfversluis  2 ปีที่แล้ว

      Sorry, not sure what you mean by this. If you want to send more message, repeat this process for different objects

  • @xamdev5975
    @xamdev5975 3 ปีที่แล้ว

    I have done this 'Unsubscribe' it after it get called once but still it get called twice. Do we have any work around for this?

    • @jfversluis
      @jfversluis  3 ปีที่แล้ว

      Kind of hard to say without any code but it calls as often as it is subscribed. Set breakpoints and see how many times it passes through there and how to make it right

  • @matteoc7084
    @matteoc7084 3 ปีที่แล้ว

    If I only need to subscribe once where is the best place to put the code?

    • @jfversluis
      @jfversluis  3 ปีที่แล้ว

      That totally depends on your code :) the constructor is usually a good place that’s only executed once

  • @fdmomtaz
    @fdmomtaz 3 ปีที่แล้ว +1

    me: spending 2 hours putting a "message service" together so I can pass data around
    Gerald: you can send a message to a subscriber
    me: why didn't I just search Google??!?!? WHY??
    LOL

    • @jfversluis
      @jfversluis  3 ปีที่แล้ว +1

      Next time search my channel first 😌

  • @kanishkranjan7459
    @kanishkranjan7459 3 ปีที่แล้ว +1

    Sir plzzz make a video on how to make alarm in android. Similarly to Samsung clock. plzzzzz sir

    • @jfversluis
      @jfversluis  3 ปีที่แล้ว

      Appreciate the idea, but I don’t think that will happen anytime soon. Sorry :(

    • @kanishkranjan7459
      @kanishkranjan7459 3 ปีที่แล้ว

      @@jfversluis Okay

  • @datadrop1745
    @datadrop1745 2 ปีที่แล้ว

    will you please make up a detailed tutorial about Rest API(with Access tokens) get put patch delete methods for developing a xamarin forms app using MVVM approach
    It can be a video play list.
    The reason behind I am asking this, every IT solution company now-a-days is using this approach.

    • @jfversluis
      @jfversluis  2 ปีที่แล้ว

      Please don’t post the same question under multiple videos

  • @mohamadrezamohamadi9915
    @mohamadrezamohamadi9915 ปีที่แล้ว

    Hello, please make a tutorial about sending and receiving messages in Maui

    • @mohamadrezamohamadi9915
      @mohamadrezamohamadi9915 ปีที่แล้ว

      I researched this topic and found out that it is not possible to receive messages in maui and only send messages. If I am correct, please give me the answer.

    • @jfversluis
      @jfversluis  ปีที่แล้ว

      What kind of messages?

    • @mohamadrezamohamadi9915
      @mohamadrezamohamadi9915 ปีที่แล้ว

      @@jfversluis Send a message via phone number(SMS)

    • @mohamadrezamohamadi9915
      @mohamadrezamohamadi9915 ปีที่แล้ว

      @@jfversluis Is it possible to add sms receiving to my application in maui or to be able to read sms from phone messages?

    • @jfversluis
      @jfversluis  ปีที่แล้ว

      Anything you can do with a native Android app, you can also do with .NET MAUI

  • @phamthanhduong74
    @phamthanhduong74 3 ปีที่แล้ว

    can i use messagecenter class to page ?

    • @jfversluis
      @jfversluis  3 ปีที่แล้ว

      A page is just another class, so sure :)

    • @phamthanhduong74
      @phamthanhduong74 3 ปีที่แล้ว

      @@jfversluis i call firebase messing service to page show display alert, i do same as you but get error cannot convert firebase messing to xamarin.form

    • @jfversluis
      @jfversluis  3 ปีที่แล้ว

      @@phamthanhduong74 ah between Firebase and Xamarin you might have some conversion to do indeed :)

  • @moatasemkremed5395
    @moatasemkremed5395 3 ปีที่แล้ว +1

    MVVM Please 🙏

    • @jfversluis
      @jfversluis  3 ปีที่แล้ว +1

      Did you see this one by James? th-cam.com/video/Pso1MeX_HvI/w-d-xo.html

    • @alexmidnayt159
      @alexmidnayt159 3 ปีที่แล้ว

      MVVM & MessagingCenter?) realy?

    • @jfversluis
      @jfversluis  3 ปีที่แล้ว

      Not sure what you mean there Alex?

    • @alexmidnayt159
      @alexmidnayt159 3 ปีที่แล้ว

      @@jfversluis No? what about this - using MC will broke MVVM pattern logic!

    • @jfversluis
      @jfversluis  3 ปีที่แล้ว +1

      Absolutely! But I think this person asks for a video about MVVM not about a combination of MVVM and MessagingCenter. At least that’s not clear.
      Additionally, with the Xamarin paradigm there is more places where you can use the MessagingCenter that go way beyond the MVVM pattern that is only applied on your shared logic

  • @EdRawrDev5813
    @EdRawrDev5813 3 ปีที่แล้ว +1

    you should use UwU instead of :-))

    • @jfversluis
      @jfversluis  3 ปีที่แล้ว

      Help me out... UwU?

    • @EdRawrDev5813
      @EdRawrDev5813 3 ปีที่แล้ว

      @@jfversluis it's like :-)) but for young fellas , more fresh and kawaii

    • @jfversluis
      @jfversluis  3 ปีที่แล้ว +1

      I had to Google it. Now I feel old...

    • @EdRawrDev5813
      @EdRawrDev5813 3 ปีที่แล้ว

      @@jfversluis you are priceless sir Gerald ☠️🖤

  • @csababorbely3453
    @csababorbely3453 ปีที่แล้ว

    is there any prerequisites for xamarin in this messagincenter? im devoloping a maui app and im surprised why xamarin.from isnt "found" ....

    • @jfversluis
      @jfversluis  ปีที่แล้ว

      Xamarin and Xamarin.Forms as a name is gone. The MessagingCenter is still in .NET MAUI, but deprecated. The performance is horrible have a look at the MVVM Toolkit one

    • @csababorbely3453
      @csababorbely3453 ปีที่แล้ว

      shit ok thse are nuget packagese. but should i take every? or just core is enough for messaging

    • @csababorbely3453
      @csababorbely3453 ปีที่แล้ว

      @@jfversluis thank you!