Dmitry
Dmitry

Reputation: 95

Custom navigation bar using Xamarin forms with prism broken

I'm using Prism Library for Xamarin.Forms.

And I'm going to create custom navigation bar via Control template. (Reason of creating custom navigation bar - I didn't find solution to make navigation bar transparent for display background image, also I will probably customize my navigation bar and add some controls on it).

<ControlTemplate x:Key="NavigationPageTemplate">
    <AbsoluteLayout BackgroundColor="Transparent">

        <Image AbsoluteLayout.LayoutBounds="0,0,1,1"
               AbsoluteLayout.LayoutFlags="All"
               Aspect="AspectFill"
               Source="{TemplateBinding BackgroundImageEx}" />

        <ContentView Padding="0,50,0,0"
                     AbsoluteLayout.LayoutBounds="0,0,1,1"
                     AbsoluteLayout.LayoutFlags="All">
            <ContentPresenter />
        </ContentView>

        <!--Navigation bar started here -->
        <ContentView AbsoluteLayout.LayoutBounds="0,0,1,AutoSize"
                     AbsoluteLayout.LayoutFlags="PositionProportional, WidthProportional"
                     BackgroundColor="Transparent">
            <ContentView.Padding>
                <OnPlatform x:TypeArguments="Thickness"
                            Android="10"
                            iOS="10, 20, 10, 0" />
            </ContentView.Padding>

            <controls:ImageButton Command="{TemplateBinding GoBackCommand}"
                                  HeightRequest="30"
                                  HorizontalOptions="StartAndExpand"
                                  Source="ic_back.png"
                                  WidthRequest="30">

            </controls:ImageButton>

        </ContentView>
    </AbsoluteLayout>
</ControlTemplate>

And my problem is to process back button press with Prism Navigation. I've tried to process click on MyApp.xaml.cs file.

private void Button_OnClicked(object sender, EventArgs e)
{
    NavigationService.GoBackAsync();
}

And it seems to have different navigation stack because it shows after press my first page.

I had Navigation this way:

Navigate("FirstPage"); -> Navigate(MasterDetail/NavigationPage/ViewA) -> Navigate("ViewB") ViewB - uses Control template.

When I click custom back button on ViewB NavigationService back me to FirstPage. It is incorrect for me. I should back to ViewA!

Another question Should first page be saved when we change App.MainPage?

Upvotes: 2

Views: 1319

Answers (2)

Dmitry
Dmitry

Reputation: 95

See the discussion of described problem on https://github.com/PrismLibrary/Prism/issues/1262

Upvotes: 1

TeoVr81
TeoVr81

Reputation: 1009

To navigate back from ViewA to FirstPage you can intercept the back event and go back again if a variable is passed with a specific value from the ViewB page. Code Example:

Sender:

var navigationParams = new NavigationParameters();
navigationParams.Add("yourVariableName", "YourVariableValue");
_navigationService.GoBackAsync(navigationParams);

Receiver:

public void OnNavigatedTo(NavigationParameters parameters)
        {
  string myVar = null;
            if (parameters.ContainsKey("yourVariableName"))
            {
                myVar = (string)parameters["yourVariableName"];
            }
  if(myVar=="YourVariableValue"){
     NavigationService.GoBackAsync();
  }
}

I don't understand your second question.

Upvotes: 0

Related Questions