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;
}
}
Related
I'm trying to center a map on a custom location with the following code:
mapView = new MapKit.MKMapView();
mapView.ZoomEnabled = true;
mapView.ScrollEnabled = true;
mapDelegate = new GeoFleet.iOS.MapDelegate();
mapView.Delegate = mapDelegate;
var region = new MapKit.MKCoordinateRegion(
new CLLocationCoordinate2D(latitude: 43.6114099, longitude: 7.0817283),
new MapKit.MKCoordinateSpan(0.00899321605918731, 0.0124209769840958)
);
this.mapView.Region = region;
Unfortunately, this displays me the map of France; not the country's south east location I'm requesting.
How can I debug this kind of problem?
EDIT:
You can go to the location with the following link.
I've tried this other call which does neither work:
MapKit.MKCoordinateRegion.FromDistance(new CLLocationCoordinate2D(latitude: 43.6114099, longitude: 7.0817283), 2000, 2000);
this works for me
public partial class ViewController : UIViewController
{
MKMapView map;
UIButton btn;
public ViewController(IntPtr handle) : base(handle)
{
map = new MKMapView(this.View.Bounds);
this.View.AddSubview(map);
btn = new UIButton(UIButtonType.RoundedRect);
btn.SetTitle("GO!!!",UIControlState.Normal);
btn.TouchDown += Btn_TouchDown;
btn.Frame = new CoreGraphics.CGRect(100,200,50,50);
this.View.AddSubview(btn);
}
private void Btn_TouchDown(object sender, EventArgs e)
{
this.map.Region = new MapKit.MKCoordinateRegion(
new CLLocationCoordinate2D(latitude: 43.6114099, longitude: 7.0817283),
new MapKit.MKCoordinateSpan(0.00899321605918731, 0.0124209769840958)
);
}
}
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";
}
Can anybody help me with this code in my Xamarin project. I am trying to set a loading wheel (to signify that an action is happening and to let the user know to wait) when the "Login" button is clicked. For some reason since the function is asynchronous the loading wheel is never set to visible when the API code is run. It just fails to show up when I click login, however, it still does the login function.
// Defined up above in the file
var loginButton = new Button
{
Text = "Login",
};
loginButton.BackgroundColor = Color.Navy;
loginButton.TextColor = Color.White;
loginButton.Clicked += OnLoginButtonClicked;
async void OnLoginButtonClicked(object sender, EventArgs e)
{
loadingWheel.IsVisible = true;
try
{
var restUrl = "*******";
var content = string.Empty;
using (var client = new HttpClient())
{
string body = "{\"UserName\":\"" + usernameEntry.Text + "\", \"Password\":\"" + passwordEntry.Text + "\"}";
var contentType = new StringContent(body, Encoding.UTF8, "application/json");
var result = client.PostAsync(restUrl, contentType).Result;
content = await result.Content.ReadAsStringAsync();
}
if (content.ToLower() != "false")
{
var menuPage = new MenuPage();
NavigationPage = new NavigationPage(new HomePage());
RootPage = new Views.MainPage();
RootPage.Master = menuPage;
RootPage.Detail = NavigationPage;
MainPage = RootPage;
}
else
{
messageLabel.Text = "Username or password incorrect. Please try again.";
passwordEntry.Text = string.Empty;
}
}
catch (Exception ex)
{
messageLabel.Text = "Please check the internet connection for the connectivity.";
}
}
If I comment out the entire try block then the loading wheel does show up. It just does not work with the code in there.
Can anybody help me solve this problem? Thanks.
I think you can try with BeginInvokeOnMainThread
Device.BeginInvokeOnMainThread (() => {
loadingWheel.IsVisible = true;
});
UPDATE
I have also create this REPO... it works without BeginInvodeOnMainThread
public class MyPage6 : ContentPage
{
ActivityIndicator _ac = new ActivityIndicator { IsVisible = false, IsRunning = false };
public MyPage6()
{
Button b = new Button {Text = "Press for ActivityIndicator" };
b.Clicked += B_Clicked;
Content = new StackLayout
{
Children = {
_ac,
b,
new Label { Text = "Hello ContentPage" }
}
};
}
async void B_Clicked(object sender, EventArgs e)
{
_ac.IsRunning = true;
_ac.IsVisible = true;
await Task.Delay(2000);
_ac.IsRunning = false;
_ac.IsVisible = false;
}
}
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 :)
I have a entry control added using c# to my xamarin.forms app. I also have a toolbaritem, on click of which I would like to save the data which is being entered by user.
I cannot access the text value in click event, I get this error:
Error 20 The name 'txtTest' does not exist in the current context
Here is my code sample:
public SettingsPage()
{
ToolbarItem Settings = new ToolbarItem();
Settings.Name = "Settings";
Settings.Clicked += OnClick_Settings;
Settings.Order = ToolbarItemOrder.Primaru;
ToolbarItems.Add(Settings);
loadData();
}
async public void loadData()
{
Label lblTest = new Label { Text = "Test", FontAttributes = FontAttributes.Bold };
Entry txtTest = new Entry();
StackLayout stLTest = new StackLayout
{
Padding = new Thickness(10, 0, 0, 0),
Children ={
lblTest,
txtTest
}
};
Content = stTest
}
async private void OnClick_Settings(object sender, EventArgs e)
{
var test= txtTest.Text;
}
In my OnClick_Settings, I cannot find text value.
you have txtTest in loadData() method.
you should move it out of there.
Do this
Entry txtTest;
async public void loadData()
{
Label lblTest = new Label { Text = "Test", FontAttributes = FontAttributes.Bold };
txtTest = new Entry();
StackLayout stLTest = new StackLayout
{
Padding = new Thickness(10, 0, 0, 0),
Children ={
lblTest,
txtTest
}
};
Content = stTest
}