Reputation: 95
i have code to integrate custom title bar on all pages using content control using shell view.
here is my code:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="SmartEntry.DashBoard">
Shell.TitleView = new Lable(Application.Current.Resources["TopBar"]);
<ContentPage.ToolbarItems>
<ToolbarItem Icon="logout_icon.png" Order="Primary" Priority="1" Clicked="ToolbarItem_Clicked"/>
</ContentPage.ToolbarItems>
<ContentPage.Content>
<StackLayout>
.....
and here is my app.xaml code:
public App()
{
InitializeComponent();
MainPage = new NavigationPage(new SmartEntry.LoginPage1());
var page = App.Current.MainPage.Navigation.NavigationStack.Last();
switch(page.ToString())
{
case "DashBoard.xaml":
lbltitle.Text = "DASHBOARD";
break;
case "GenerateBarcode.xaml":
lbltitle.Text = "WIFI SETTINGS";
break;
case "ManageDevice.xaml":
lbltitle.Text = "ADD DEVICE";
break;
case "MyProfile.xaml":
lbltitle.Text = "MY PROFILE";
break;
case "RegistrationUser.xaml":
lbltitle.Text = "MEMBER REGISTRATION";
break;
case "WifiSettings1.xaml":
lbltitle.Text = "WIFI SETTINGS";
break;
}
//MainPage = CrossConnectivity.Current.IsConnected ? (Page)new WifiSettings1() : new WifiSettings1();
}
here is app.xaml markup :
<?xml version="1.0" encoding="utf-8" ?>
<Application xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="SmartEntry.App">
<Application.Resources>
<Label x:Name="lbltitle" TextColor="White" HorizontalOptions="Start" VerticalOptions="StartAndExpand" x:Key="TopBar"></Label>
</Application.Resources>
</Application>
here dynamically i change title of lbltitle control. but no with success.
is there anyone that resolve my issue.
Upvotes: 1
Views: 931
Reputation: 897
With MVVM Bindings! Xamarin Forms is built to work with MVVM out of the box, and if you aren't already I really recommend reading up on the architecture.
Basically, every Screen (View) has a corresponding Functionality Handling Class (ViewModel). This ViewModel is used to update the view via a Binding. A Binding is a special two way property that allows you to get and set a value on the View as soon as the value is updated on either the screen or by your code in the ViewModel. As such, in your View Model, you can create a property called Title and bind it to your front end XAML using
<Application.Resources>
<Label Text="{Binding Title}" />
</Application.Resources>
This should get your started - https://nicksnettravels.builttoroam.com/mvvm-navigation/
Upvotes: 1