Xamarin: detect page pushed on NavigationRenderer












0















I order to apply some navigationBar properties (like as the background image) for different page, I think to have a condition on my custom NavigationRenderer.



My idea is to have some condition like (in my working code)



   public class CustomNavigationRenderer : NavigationRenderer
{
public override void ViewDidLoad()
{
base.ViewDidLoad();

if (pagePushed is 1)
{
NavigationBar.SetBackgroundImage(new UIImage(), UIBarMetrics.Default);
NavigationBar.ShadowImage = new UIImage();
}

else (ahother page){
var img = UIImage.FromBundle("MyImage");
NavigationBar.SetBackgroundImage(img, UIBarMetrics.Default);
}
}
}


that allows me to have at least a condition to apply a different navigation properties. Another way is to have 2 Navigationrenderer class but I think is not possible.



Any idea to how do that?










share|improve this question



























    0















    I order to apply some navigationBar properties (like as the background image) for different page, I think to have a condition on my custom NavigationRenderer.



    My idea is to have some condition like (in my working code)



       public class CustomNavigationRenderer : NavigationRenderer
    {
    public override void ViewDidLoad()
    {
    base.ViewDidLoad();

    if (pagePushed is 1)
    {
    NavigationBar.SetBackgroundImage(new UIImage(), UIBarMetrics.Default);
    NavigationBar.ShadowImage = new UIImage();
    }

    else (ahother page){
    var img = UIImage.FromBundle("MyImage");
    NavigationBar.SetBackgroundImage(img, UIBarMetrics.Default);
    }
    }
    }


    that allows me to have at least a condition to apply a different navigation properties. Another way is to have 2 Navigationrenderer class but I think is not possible.



    Any idea to how do that?










    share|improve this question

























      0












      0








      0








      I order to apply some navigationBar properties (like as the background image) for different page, I think to have a condition on my custom NavigationRenderer.



      My idea is to have some condition like (in my working code)



         public class CustomNavigationRenderer : NavigationRenderer
      {
      public override void ViewDidLoad()
      {
      base.ViewDidLoad();

      if (pagePushed is 1)
      {
      NavigationBar.SetBackgroundImage(new UIImage(), UIBarMetrics.Default);
      NavigationBar.ShadowImage = new UIImage();
      }

      else (ahother page){
      var img = UIImage.FromBundle("MyImage");
      NavigationBar.SetBackgroundImage(img, UIBarMetrics.Default);
      }
      }
      }


      that allows me to have at least a condition to apply a different navigation properties. Another way is to have 2 Navigationrenderer class but I think is not possible.



      Any idea to how do that?










      share|improve this question














      I order to apply some navigationBar properties (like as the background image) for different page, I think to have a condition on my custom NavigationRenderer.



      My idea is to have some condition like (in my working code)



         public class CustomNavigationRenderer : NavigationRenderer
      {
      public override void ViewDidLoad()
      {
      base.ViewDidLoad();

      if (pagePushed is 1)
      {
      NavigationBar.SetBackgroundImage(new UIImage(), UIBarMetrics.Default);
      NavigationBar.ShadowImage = new UIImage();
      }

      else (ahother page){
      var img = UIImage.FromBundle("MyImage");
      NavigationBar.SetBackgroundImage(img, UIBarMetrics.Default);
      }
      }
      }


      that allows me to have at least a condition to apply a different navigation properties. Another way is to have 2 Navigationrenderer class but I think is not possible.



      Any idea to how do that?







      xamarin navigation custom-controls






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 '18 at 8:09









      doxsidoxsi

      6151234




      6151234
























          1 Answer
          1






          active

          oldest

          votes


















          1














          If you look at the source code for NavigationRenderer here, you will notice there are quite a few methods and callbacks you can take advantage of.



          I would suggest you can do something like this:



          1) Code for your custom NavigationRenderer (iOS project, you will have to do something similar on Android):



          using System.Threading.Tasks;
          using MyProject.iOS;
          using UIKit;
          using Xamarin.Forms;
          using Xamarin.Forms.Platform.iOS;

          [assembly: ExportRenderer(typeof(NavigationPage), typeof(NavRenderer))]
          namespace MyProject.iOS
          {
          public class NavRenderer : NavigationRenderer
          {
          protected override async Task<bool> OnPushAsync(Page page, bool animated)
          {
          var result = await base.OnPushAsync(page, animated);

          if(result)
          {
          if (page is IMyPageType1)
          {
          NavigationBar.SetBackgroundImage(new UIImage(), UIBarMetrics.Default);
          NavigationBar.ShadowImage = new UIImage();
          }

          else if(page is IMyPageType2)
          {
          var img = UIImage.FromBundle("MyImage");
          NavigationBar.SetBackgroundImage(img, UIBarMetrics.Default);
          }
          }

          return result;
          }
          }
          }


          2) Based on the code above, you need to add two interfaces. These should be located in the same project / dll where your Pages are located (all your Xamarin.Forms UI):



              public interface IMyPageType1
          {
          }

          public interface IMyPageType2
          {
          }


          3) Now everything that's remaining is implement the interfaces on the pages where you need it. For example:



              public partial class MyPage1 : ContentPage, IMyPageType1
          {
          //...
          }


          From here, possibilities are endless! You can add for example a method to IMyPageType1 that would return a color, and then inside your renderer, once you know the page being pushed is implementing IMyPageType1, you can call the method and get the color to use.






          share|improve this answer
























          • Nice. Tx! Could you know I can override OnPopViewAsync? I try but it be called after the base pop

            – doxsi
            Nov 26 '18 at 8:51











          • Not sure what the requirement is, but I would start by overriding the method and placing the call to base at the end of it

            – nmilcoff
            Nov 26 '18 at 12:28











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53442795%2fxamarin-detect-page-pushed-on-navigationrenderer%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          If you look at the source code for NavigationRenderer here, you will notice there are quite a few methods and callbacks you can take advantage of.



          I would suggest you can do something like this:



          1) Code for your custom NavigationRenderer (iOS project, you will have to do something similar on Android):



          using System.Threading.Tasks;
          using MyProject.iOS;
          using UIKit;
          using Xamarin.Forms;
          using Xamarin.Forms.Platform.iOS;

          [assembly: ExportRenderer(typeof(NavigationPage), typeof(NavRenderer))]
          namespace MyProject.iOS
          {
          public class NavRenderer : NavigationRenderer
          {
          protected override async Task<bool> OnPushAsync(Page page, bool animated)
          {
          var result = await base.OnPushAsync(page, animated);

          if(result)
          {
          if (page is IMyPageType1)
          {
          NavigationBar.SetBackgroundImage(new UIImage(), UIBarMetrics.Default);
          NavigationBar.ShadowImage = new UIImage();
          }

          else if(page is IMyPageType2)
          {
          var img = UIImage.FromBundle("MyImage");
          NavigationBar.SetBackgroundImage(img, UIBarMetrics.Default);
          }
          }

          return result;
          }
          }
          }


          2) Based on the code above, you need to add two interfaces. These should be located in the same project / dll where your Pages are located (all your Xamarin.Forms UI):



              public interface IMyPageType1
          {
          }

          public interface IMyPageType2
          {
          }


          3) Now everything that's remaining is implement the interfaces on the pages where you need it. For example:



              public partial class MyPage1 : ContentPage, IMyPageType1
          {
          //...
          }


          From here, possibilities are endless! You can add for example a method to IMyPageType1 that would return a color, and then inside your renderer, once you know the page being pushed is implementing IMyPageType1, you can call the method and get the color to use.






          share|improve this answer
























          • Nice. Tx! Could you know I can override OnPopViewAsync? I try but it be called after the base pop

            – doxsi
            Nov 26 '18 at 8:51











          • Not sure what the requirement is, but I would start by overriding the method and placing the call to base at the end of it

            – nmilcoff
            Nov 26 '18 at 12:28
















          1














          If you look at the source code for NavigationRenderer here, you will notice there are quite a few methods and callbacks you can take advantage of.



          I would suggest you can do something like this:



          1) Code for your custom NavigationRenderer (iOS project, you will have to do something similar on Android):



          using System.Threading.Tasks;
          using MyProject.iOS;
          using UIKit;
          using Xamarin.Forms;
          using Xamarin.Forms.Platform.iOS;

          [assembly: ExportRenderer(typeof(NavigationPage), typeof(NavRenderer))]
          namespace MyProject.iOS
          {
          public class NavRenderer : NavigationRenderer
          {
          protected override async Task<bool> OnPushAsync(Page page, bool animated)
          {
          var result = await base.OnPushAsync(page, animated);

          if(result)
          {
          if (page is IMyPageType1)
          {
          NavigationBar.SetBackgroundImage(new UIImage(), UIBarMetrics.Default);
          NavigationBar.ShadowImage = new UIImage();
          }

          else if(page is IMyPageType2)
          {
          var img = UIImage.FromBundle("MyImage");
          NavigationBar.SetBackgroundImage(img, UIBarMetrics.Default);
          }
          }

          return result;
          }
          }
          }


          2) Based on the code above, you need to add two interfaces. These should be located in the same project / dll where your Pages are located (all your Xamarin.Forms UI):



              public interface IMyPageType1
          {
          }

          public interface IMyPageType2
          {
          }


          3) Now everything that's remaining is implement the interfaces on the pages where you need it. For example:



              public partial class MyPage1 : ContentPage, IMyPageType1
          {
          //...
          }


          From here, possibilities are endless! You can add for example a method to IMyPageType1 that would return a color, and then inside your renderer, once you know the page being pushed is implementing IMyPageType1, you can call the method and get the color to use.






          share|improve this answer
























          • Nice. Tx! Could you know I can override OnPopViewAsync? I try but it be called after the base pop

            – doxsi
            Nov 26 '18 at 8:51











          • Not sure what the requirement is, but I would start by overriding the method and placing the call to base at the end of it

            – nmilcoff
            Nov 26 '18 at 12:28














          1












          1








          1







          If you look at the source code for NavigationRenderer here, you will notice there are quite a few methods and callbacks you can take advantage of.



          I would suggest you can do something like this:



          1) Code for your custom NavigationRenderer (iOS project, you will have to do something similar on Android):



          using System.Threading.Tasks;
          using MyProject.iOS;
          using UIKit;
          using Xamarin.Forms;
          using Xamarin.Forms.Platform.iOS;

          [assembly: ExportRenderer(typeof(NavigationPage), typeof(NavRenderer))]
          namespace MyProject.iOS
          {
          public class NavRenderer : NavigationRenderer
          {
          protected override async Task<bool> OnPushAsync(Page page, bool animated)
          {
          var result = await base.OnPushAsync(page, animated);

          if(result)
          {
          if (page is IMyPageType1)
          {
          NavigationBar.SetBackgroundImage(new UIImage(), UIBarMetrics.Default);
          NavigationBar.ShadowImage = new UIImage();
          }

          else if(page is IMyPageType2)
          {
          var img = UIImage.FromBundle("MyImage");
          NavigationBar.SetBackgroundImage(img, UIBarMetrics.Default);
          }
          }

          return result;
          }
          }
          }


          2) Based on the code above, you need to add two interfaces. These should be located in the same project / dll where your Pages are located (all your Xamarin.Forms UI):



              public interface IMyPageType1
          {
          }

          public interface IMyPageType2
          {
          }


          3) Now everything that's remaining is implement the interfaces on the pages where you need it. For example:



              public partial class MyPage1 : ContentPage, IMyPageType1
          {
          //...
          }


          From here, possibilities are endless! You can add for example a method to IMyPageType1 that would return a color, and then inside your renderer, once you know the page being pushed is implementing IMyPageType1, you can call the method and get the color to use.






          share|improve this answer













          If you look at the source code for NavigationRenderer here, you will notice there are quite a few methods and callbacks you can take advantage of.



          I would suggest you can do something like this:



          1) Code for your custom NavigationRenderer (iOS project, you will have to do something similar on Android):



          using System.Threading.Tasks;
          using MyProject.iOS;
          using UIKit;
          using Xamarin.Forms;
          using Xamarin.Forms.Platform.iOS;

          [assembly: ExportRenderer(typeof(NavigationPage), typeof(NavRenderer))]
          namespace MyProject.iOS
          {
          public class NavRenderer : NavigationRenderer
          {
          protected override async Task<bool> OnPushAsync(Page page, bool animated)
          {
          var result = await base.OnPushAsync(page, animated);

          if(result)
          {
          if (page is IMyPageType1)
          {
          NavigationBar.SetBackgroundImage(new UIImage(), UIBarMetrics.Default);
          NavigationBar.ShadowImage = new UIImage();
          }

          else if(page is IMyPageType2)
          {
          var img = UIImage.FromBundle("MyImage");
          NavigationBar.SetBackgroundImage(img, UIBarMetrics.Default);
          }
          }

          return result;
          }
          }
          }


          2) Based on the code above, you need to add two interfaces. These should be located in the same project / dll where your Pages are located (all your Xamarin.Forms UI):



              public interface IMyPageType1
          {
          }

          public interface IMyPageType2
          {
          }


          3) Now everything that's remaining is implement the interfaces on the pages where you need it. For example:



              public partial class MyPage1 : ContentPage, IMyPageType1
          {
          //...
          }


          From here, possibilities are endless! You can add for example a method to IMyPageType1 that would return a color, and then inside your renderer, once you know the page being pushed is implementing IMyPageType1, you can call the method and get the color to use.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 24 '18 at 16:57









          nmilcoffnmilcoff

          908317




          908317













          • Nice. Tx! Could you know I can override OnPopViewAsync? I try but it be called after the base pop

            – doxsi
            Nov 26 '18 at 8:51











          • Not sure what the requirement is, but I would start by overriding the method and placing the call to base at the end of it

            – nmilcoff
            Nov 26 '18 at 12:28



















          • Nice. Tx! Could you know I can override OnPopViewAsync? I try but it be called after the base pop

            – doxsi
            Nov 26 '18 at 8:51











          • Not sure what the requirement is, but I would start by overriding the method and placing the call to base at the end of it

            – nmilcoff
            Nov 26 '18 at 12:28

















          Nice. Tx! Could you know I can override OnPopViewAsync? I try but it be called after the base pop

          – doxsi
          Nov 26 '18 at 8:51





          Nice. Tx! Could you know I can override OnPopViewAsync? I try but it be called after the base pop

          – doxsi
          Nov 26 '18 at 8:51













          Not sure what the requirement is, but I would start by overriding the method and placing the call to base at the end of it

          – nmilcoff
          Nov 26 '18 at 12:28





          Not sure what the requirement is, but I would start by overriding the method and placing the call to base at the end of it

          – nmilcoff
          Nov 26 '18 at 12:28




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53442795%2fxamarin-detect-page-pushed-on-navigationrenderer%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Create new schema in PostgreSQL using DBeaver

          Deepest pit of an array with Javascript: test on Codility

          Fotorealismo