Set Back Button Title on Xamarin Forms C# - c#

im quite new to c# and Xamarin and I've just encountered a problem. I can't seem to be able to set the back button title on a navigation page. Ive tried using the static SetBackButtonTitle method but it does not seem to set the back button to the title I want which is
'Test' but instead its giving me the 'default' title which is "Back".
// The root page of your application
var content = new ContentPage();
var CompanyName = new Label();
CompanyName.HorizontalTextAlignment = TextAlignment.Center;
CompanyName.Text = "Test";
var NextPage = new Button();
NextPage.Text = "Next Page";
NextPage.Font = Font.SystemFontOfSize(NamedSize.Large);
NextPage.BorderWidth = 1;
NextPage.HorizontalOptions = LayoutOptions.Center;
NextPage.VerticalOptions = LayoutOptions.CenterAndExpand;
var layout = new StackLayout();
layout.VerticalOptions = LayoutOptions.CenterAndExpand;
layout.Children.Add(CompanyName);
layout.Children.Add(NextPage);
content.Content = layout;
var content2 = new ContentPage();
var navigation = new NavigationPage(content);
NavigationPage.SetHasBackButton(navigation,true);
NavigationPage.SetBackButtonTitle(navigation,"Test");
NextPage.Clicked += NextPageClicked;
async void NextPageClicked(object sender, EventArgs e)
{
await navigation.PushAsync(content2);
}
MainPage = navigation;
}

If you want to change the back button's title(at the left in the navigation bar) in the second page, you should call the method SetBackButtonTitle with first page as parameter. So please modify NavigationPage.SetBackButtonTitle(navigation,"Test"); to NavigationPage.SetBackButtonTitle(content,"Test");

For those who use Xamarin.Forms WPF if you want to change back button title you can do this:
protected override void OnDisappearing()
{
base.OnDisappearing();
Title = "";
}
protected override void OnAppearing()
{
base.OnAppearing();
Title = "MyTitle";
}

Related

How to display a pin on maps

I could display the map on my App interface , and now I'm trying to display a Pin of my current position .
I displayed in but with a button Click that shows me another Content Page . but I couldn't display it on the same page even after clicking that button .
If there's a way to show it , on the same page and without clicking any button , I'll be so thankful .
Here's the code of my Clicked_Button Event , This Code works and when I click it displays a new content page with my current position . but what I want it to to show it in my Map Module in the Main Page,
this is the XAML Code for my map (putting it in a grid):
<maps1:Map Grid.Column="1"
Grid.Row="2"
Grid.RowSpan="2"
Grid.ColumnSpan="3"
x:Name="myMap">
I tried to remove " Content = myMap; " from my back-end code to keep it on the main page , but the pin doesn't show up.
protected async void OnButtonClicked_speed(object sender, EventArgs args)
{
var locator = CrossGeolocator.Current;
locator.DesiredAccuracy = 50;
var position = await locator.GetPositionAsync(TimeSpan.FromSeconds(10));
myMap = new Xamarin.Forms.Maps.Map(MapSpan.FromCenterAndRadius(
new Xamarin.Forms.Maps.Position(position.Latitude,position.Longitude),
Distance.FromMiles(0.5)))
{
IsShowingUser = true,
VerticalOptions = LayoutOptions.FillAndExpand
};
var position1 = new Xamarin.Forms.Maps.Position(position.Latitude, position.Longitude);
var pin1 = new Pin
{
Type = PinType.Place,
Position = position1,
Label = "Current Position",
Address = ""
};
myMap.Pins.Add(pin1);
Content = myMap;
}
Content = myMap
this is equivalent to repopulating your current page with a map
so you just move the code out of your OnButtonClicked_speed method.
like this:
in MainPage.xaml.cs
public partial class MainPage : ContentPage
{
private Map myMap;
public MainPage(Position position)
{
InitializeComponent();
myMap.MoveToRegion(MapSpan.FromCenterAndRadius(
new Xamarin.Forms.Maps.Position(position.Latitude, position.Longitude),
Distance.FromMiles(0.5)));
myMap.IsShowingUser = true;
myMap.VerticalOptions = LayoutOptions.FillAndExpand;
var position1 = new Xamarin.Forms.Maps.Position(position.Latitude, position.Longitude);
var pin1 = new Pin
{
Type = PinType.Place,
Position = position1,
Label = "Current Position",
Address = ""
};
myMap.Pins.Add(pin1);
}
public static async Task<MainPage> CreateMainPageAsync()
{
var locator = CrossGeolocator.Current;
locator.DesiredAccuracy = 50;
var position = await locator.GetPositionAsync(TimeSpan.FromSeconds(10));
MainPage page = new MainPage(position );
return page;
}
}
in App.xaml.cs
public partial class App : Application
{
public App()
{
InitializeComponent();
}
protected override async void OnStart()
{
namespace.MainPage main = await namespace.MainPage.CreateMainPageAsync();
MainPage = main;
}
}

How can I see css changes without restarting xamarin app

I'm building a xamarin app where I'm using a Preprocessed Razor Template as a webview. I used this guide to set it up: https://developer.xamarin.com/guides/cross-platform/advanced/razor_html_templates/
It's working great and all but every time I do some css changes I have to restart the app to see the css changes. Is there another way to do this so I don't have to restart the app every time?
I tried adding a button which reloads the webView but that did not work. This is my activity:
public class ItemWebViewActivity : Activity
{
WebView webView;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
var itemId = Intent.GetIntExtra("itemId", 0);
SetContentView(Resource.Layout.ItemWebView);
webView = FindViewById<WebView>(Resource.Id.webView);
var dataService = new AzureDataService();
var item = dataService.GetItem(itemId);
Button refreshButton = (Button)FindViewById(Resource.Id.refreshButton);
refreshButton.Click += ReloadPage;
var decodedDesc = WebUtility.HtmlDecode(item.Description);
var vm = new Item()
{
Artist = item.Artist,
Title = item.Title,
Picture = item.Picture,
Description = decodedDesc
};
var template = new ItemWebView() { Model = vm };
var page = template.GenerateString();
webView.LoadDataWithBaseURL("file:///android_asset/",page, "text/html", "charset=UTF-8",null);
}
private void ReloadPage(object sender, EventArgs e)
{
webView.Reload();
}
}
Would appreciate any help :)

Tell ActivityIndicator to show yourself and then navigate to another page

On Xamarin.Forms Project, I am trying to tellActivityIndicator to show yourself and then navigate to another page
activityIndicatorObject = new ActivityIndicator()
{
HorizontalOptions = LayoutOptions.CenterAndExpand,
Color = Color.Red,
IsVisible = false,
IsRunning = false,
IsEnabled = false,
BindingContext = this,
};
ViewBookButton = new Button{ Text = "view book" };
ViewBookButton.Clicked += ViewBook;
protected void ViewBook(Object sender,EventArgs e)
{
//Go To View Book Page
activityIndicatorObject .IsVisible = true;
activityIndicatorObject .IsRunning = true;
activityIndicatorObject .IsEnabled = true;
activityIndicatorObject .SetBinding(ActivityIndicator.IsVisibleProperty, "IsBusy");
this.IsBusy = true;
Navigation.PushAsync(new BookPage());
this.IsBusy = false;
}
ActivityIndicator doesn't showing? How I can make it showing?
As I understand you tell ActivityIndicator show yourself and then navigate to another page. And new page hasn't own ActivityIndicator. I would like to suggest you make your hard operations in OnAppearing method of BookPage.
protected override void OnAppearing()
{
base.OnAppearing();
activityIndicator.IsVisible = true;
// Some hard operations
activityIndicator.IsVisible = false;
}
Or use binding instead of activityIndicator.IsVisible = true;

How to show toast after performing some functionality in windows phone 8

I want to show something like toast after some functionality performed. i-e I have a save button and I want that when it pressed then a toast should be shown with the text Record Saved etc. I read posts that show toasts are only for back-ground agents. I know someone will give me good guidance. please specify some code.
Thanks
You can use the Toast Prompt from the Coding4Fun Toolkit to perform a toast notification via code. After referencing the toolkit (ideally via NuGet) you can use it like this:
ToastPrompt toast = new ToastPrompt();
toast.Title = "Your app title";
toast.Message = "Record saved.";
toast.TextOrientation = Orientation.Horizontal;
toast.MillisecondsUntilHidden = 2000;
toast.ImageSource = new BitmapImage(new Uri("ApplicationIcon.png", UriKind.RelativeOrAbsolute));
toast.Show();
I prefer ProgressIndicator in my apps but you can use Popup or ToastPrompt.
Sample project.
// popup member
private Popup popup;
// creates popup
private Popup CreatePopup()
{
// text
TextBlock tb = new TextBlock();
tb.Foreground = (Brush)this.Resources["PhoneForegroundBrush"];
tb.FontSize = (double)this.Resources["PhoneFontSizeMedium"];
tb.Margin = new Thickness(24, 32, 24, 12);
tb.Text = "Custom toast message";
// grid wrapper
Grid grid = new Grid();
grid.Background = (Brush)this.Resources["PhoneAccentBrush"];
grid.Children.Add(tb);
grid.Width = this.ActualWidth;
// popup
Popup popup = new Popup();
popup.Child = grid;
return popup;
}
// hides popup
private void HidePopup()
{
SystemTray.BackgroundColor = (Color)this.Resources["PhoneBackgroundColor"];
this.popup.IsOpen = false;
}
// shows popup
private void ShowPopup()
{
SystemTray.BackgroundColor = (Color)this.Resources["PhoneAccentColor"];
if (this.popup == null)
{
this.popup = this.CreatePopup();
}
this.popup.IsOpen = true;
}
// shows and hides popup with a delay
private async void ButtonClick(object sender, RoutedEventArgs e)
{
this.ShowPopup();
await Task.Delay(2000);
this.HidePopup();
}
using Windows.UI.Notifications;
var toastXmlContent = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);
var txtNodes = toastXmlContent.GetElementsByTagName("text");
txtNodes[0].AppendChild(toastXmlContent.CreateTextNode("First Line"));
txtNodes[1].AppendChild(toastXmlContent.CreateTextNode("Second Line" ));
var toast = new ToastNotification(toastXmlContent);
var toastNotifier = ToastNotificationManager.CreateToastNotifier();
toastNotifier.Show(toast);

Slow app changing page in WP8

I have an app with a ListBox; when the user taps an item i launch a new page that invoke a MVVM method and the fills its ListBox. When the user taps the first ListBox it does not change page immediately, waits 2-3 seconds and then shows the new page and it is not responsive. How can i activate the new page, showing the ProgressIndicator and fill the new list?
Here is the code for the second list:
protected override async void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
int id;
if (NavigationContext.QueryString.ContainsKey("id"))
{
id = Convert.ToInt16(NavigationContext.QueryString["id"]);
SystemTray.SetIsVisible(this, true);
SystemTray.SetOpacity(this, 0.5);
progressIndicator = new ProgressIndicator();
progressIndicator.IsVisible = true;
progressIndicator.IsIndeterminate = true;
SystemTray.SetProgressIndicator(this, progressIndicator);
var x=await App.viewModel.MyDBMethod(id);
this.DataContext = App.viewModel;
this.mPivot.SelectedIndex = 0;
progressIndicator.IsVisible = false;
progressIndicator.IsIndeterminate = false;
}
}
}

Categories