Prism for Xamarin.Forms - Passing Navigation Parameters

แชร์
ฝัง
  • เผยแพร่เมื่อ 4 ส.ค. 2024
  • When navigating throughout your Prism for Xamarin.Forms applications, it is often required to pass parameters from one View to another. Prism allows you to pass navigation parameters with three simple steps:
    1. Create the navigation parameters
    2. Get the navigation parameters
    3. Read the navigation parameter values
    Passing parameters to the next View/ViewModel can be done using an overload of the INavigationService.NavigateAsync method. This overload accepts a NavigationParameters object that can be used to supply data to the next View. The NavigationParameters object can accept any arbitrary object as a value.
    Getting the navigation parameters from the target View/ViewModel is done by implementing the INavigationAware interface. The OnNavigatedFrom, OnNavigatingTo, and OnNavigatedTo methods each represent the different phases of the navigation process and expose the INavigationParameters object to you.
    You read the INavigationParameters values by using either the index of the collection, the GetValue/GetValues method, or by using the TryGetValue method.
    Twitter: / brianlagunas
    Blog: brianlagunas.com
    GitHub: github.com/brianlagunas
    Prism Library: prismlibrary.github.io/
    Become a Patron: / prismlibrary
    Visit our Sponsor: bit.ly/prism-infragistics

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

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

    Thanks a lot Brian. This has saved my evening.

  • @Styx1x
    @Styx1x 5 ปีที่แล้ว +1

    another great video. Easy to understand and excellent examples

  • @muhammadwaqasaziz4054
    @muhammadwaqasaziz4054 4 ปีที่แล้ว

    Awesome, Awesome, Awesome, Awesome, Awesome JOB MAN!

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

    Thank you very much for this vedio!

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

      Thank you very much for watching

  • @lopezcr0202
    @lopezcr0202 4 ปีที่แล้ว +1

    thanks for the video :)

  • @g00dvibes47
    @g00dvibes47 5 ปีที่แล้ว

    Brian, thank you for these videos, they are a Godsend; I inherited a project at my current company that uses Xamarin with Prism and it is super unfamiliar yet made straightforward with these videos. Having some issues wrapping my head around some things in this unfamiliar environment, but maybe I can send you a ping for video ideas? Would be a huge help! Thanks again

    • @BrianLagunas
      @BrianLagunas  5 ปีที่แล้ว +1

      Right now, the plan is to cover all the basic topics then move into topic requests. Patron topic requests will have priority, so I recommend becoming a patron so your suggestions is one of the first to be covered. www.patreon.com/prismlibrary

  • @user-im7jx6wo3t
    @user-im7jx6wo3t 5 ปีที่แล้ว +1

    Great Job! Well Done! Vozmi s polki pirojok!

  • @coderblog
    @coderblog 4 ปีที่แล้ว +1

    Thanks, I like it!

    • @BrianLagunas
      @BrianLagunas  4 ปีที่แล้ว

      Thanks for watching

    • @coderblog
      @coderblog 4 ปีที่แล้ว +1

      @@BrianLagunas I will watch all of your Prism videos! Because I find that the Prism is great so I am starting to learn it, thanks! XDD

    • @BrianLagunas
      @BrianLagunas  4 ปีที่แล้ว

      @@coderblog great! Let me know if you have any questions

    • @coderblog
      @coderblog 4 ปีที่แล้ว

      @@BrianLagunas I will, thanks a lot :)

  • @rezamohamed2112
    @rezamohamed2112 5 ปีที่แล้ว

    @Brian Lagunas when I simulate on the iOS simulator, i'm unable to get the Title 'Hello from Main Page' to render immediately on navigation to View A - works just fine on Android however.

    • @BrianLagunas
      @BrianLagunas  5 ปีที่แล้ว

      Hard to say. If you feel you found a bug, please report it on the Prism github repo with a sample app to reproduce the issue.

    • @rezamohamed2112
      @rezamohamed2112 5 ปีที่แล้ว

      @@BrianLagunas i'm just recreating the one line demo on these videos verbatim. I can't imagine its a bug - just trying to understand if it should work or not on iOS, and possibly trying to figure out why its giving issues on my end.

  • @zsoltnagy2227
    @zsoltnagy2227 4 ปีที่แล้ว

    Hi. Are you doing in the same way if you want to pass back data(object) from the Second page to the first page?

    • @BrianLagunas
      @BrianLagunas  4 ปีที่แล้ว

      Yes, call gobackasync and pass your parameters

  • @Nk54fr
    @Nk54fr 4 ปีที่แล้ว

    Hello Brian, thanks for the video. Do you know why if I create myself a NavigationParameters with the key "__NavigationMode" to set the navigationMode to New myself because I handle the navigation from MasterDetailPage. I need the __NavigationMode to make the call to OnNavigationFrom and OnNavigatedTo from the MasterDetailPage otherwise, I can't have the Initialize/OnNavigatedTo/OnNavigatedFrom methods working. I need this because I have some pages you can use from the MasterDetailPage or from the HomePage (the default page on the MasterDetailPage. It's just a gridView with some tiles of my favorite pages. They trigger a navService.NavigateTo whereas the Master section set a new DetailPage with a NavigationPage and the desired page. I hope I made myself clear. Anyway, thanks again for this awesome framework !

    • @Nk54fr
      @Nk54fr 4 ปีที่แล้ว

      Ok I found it : just cast the NavigationParameters as INavigationParametersInternal, then on the cast, perform the add and it will work :)
      Exemple given : ((INavigationParametersInternal)navigationParameters).Add("__NavigationMode", NavigationMode.New);

    • @BrianLagunas
      @BrianLagunas  4 ปีที่แล้ว

      Glad you got it figured out.

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

    Can we able to add collection in Navigated to implemented interface

    • @BrianLagunas
      @BrianLagunas  5 ปีที่แล้ว +1

      Yes. It's no different than any other app you would write.

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

    Thanks for the video.
    Can someone help me with this issue:
    OnNavigatedTo( ) is executed once the page is added to stack, so when I navigate pages forward then PopBack to the original page, all binding data are not shown in the page bcs the ViewModel does not execute OnNavigatedTo() function as the page already in the stack. So how to keep the data shown in the page OR how to pass parameters directly to ViewModel constructor ?

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

      When going back in the nav stack the pages do not loose their binding context. OnNavigatedTo gets called both on a forward nav and a back nav. You must have code in your OnNavigatedTo method that is changing state but not considering a back nav operation.

  • @crashnnburn
    @crashnnburn 5 ปีที่แล้ว

    Hello, I have a quick question. How do I access the ViewModel from the code behind. I need to do stuff in the code behind (refresh a listview) while updating viewmodel variables. Anyone know?

    • @BrianLagunas
      @BrianLagunas  5 ปีที่แล้ว +1

      If you absolutely must access the VM from code behind just cast the BindingContext. Var vm = (MyViewModel)BindingContext

    • @crashnnburn
      @crashnnburn 5 ปีที่แล้ว +1

      @@BrianLagunas Thanks for the fast response. This did the trick! The only other thing I had to do in order for it to work is to use it in the OnAppearing method. If you try to use it in the constructor it won't work because the view is created before the ViewModel. Thanks again!

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

    A really nice video. However, I am confused as to why the OnNavigatedTo in another ViewModel is not triggered. I have already added the breakpoint as well. The rest of the code is working fine (for instance, data binding).

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

      Hard to say. Usually this indicates an error in code somewhere. Possibly you still have a throw NotImplementedException in another method somewhere

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

      @@BrianLagunas Thanks for the reply. Yes, I solved it yesterday. Indeed it was due to the throw new NotImplementedException. I didnt delete that earlier.

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

    Two Questions,
    1) are there any Tabbed navigation examples?
    2) I was attempting to set up the Resourced Dictionaries like I had before swapping to Prism and I get "A value of type 'Setter' cannot be added to a collection or dictionary of type 'IList` App.xamarin Prism Xamarin." Since you don't do any styling in any of the applications and there's no styling in any of the sample projects, should I assume styling isn't possible? I know that sounds strange but I ask seriously as I have see no examples of it with Prism and I'm getting these errors. One of the guys I work with does Prism on the WPF apps but he didn't know and hadn't seen in before.

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

      Yes, we have plenty of samples. Just check out the sample repo: github.com/PrismLibrary/Prism-Samples-Forms
      Yes, you can style your Prism apps just like any other XF app. Prism does not do anything different or limit you in any way.

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

      @@BrianLagunas Do any of the samples have any styling? I couldn't find any. They were all also in 4.7, when I tried them in 4.8 I got errors. I know Gradient brushes are experimental to Xamarin but I've had them for years in other projects and don't really want to give them up.

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

      Prism has no impact on styling. It literally has nothing to do with styling.

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

      @@BrianLagunas In your first video we replace the application.xaml markup with the prism:PrismApplication markup. After that nothing worked in Xam Forms 4.8. In Forms 4.7 it would run but would break if I had Styles with Setters in the Application.Resources.

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

      @@andrewbeeman9083 I've never had an issue with this. Like I said, Prism does not impact styling in anyway. If anything breaks it's going to be a Xamarin issue, or perhaps you are doing something incorrect.