adding map control to xamarin project breaks it - c#

I am new to Xamarin (came from C# and WPF)
I build a Xamarin form application using the template provided (VS created it for me)
I add Xamarin.Forms.Map from the nuget and added it to a page like this:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:maps="clr-namespace:Xamarin.Forms.Maps;assembly=Xamarin.Forms.Maps"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="XamarinTest.Views.MapPage"
Title="Map"
>
<!--Title="{Binding Title}"-->
<ContentPage.ToolbarItems>
<!--<ToolbarItem Text="Add" Clicked="AddItem_Clicked" />-->
</ContentPage.ToolbarItems>
<StackLayout Spacing="10">
<maps:Map x:Name="map" />
</StackLayout>
</ContentPage>
Now the application doesn't work and I am getting an error which says the code can not be debugged using this debugger. (I am using UWP platform)
When I commented out the map, it would work and there is no problem.
I tested it on Android and I am getting similar errors when reference to map exist.
What is the problem and how I can fix it?

Thanks to comments, I found that I need to initialize the map in each implementation.
For example UWP we should have this line in MainPage.xaml.cs
Xamarin.FormsMaps.Init("INSERT_AUTHENTICATION_TOKEN_HERE");
This is a good document on how to initialise map on different platforms:
Xamarin.Forms Map Initialization and Configuration

Related

Can Radio Buttons be used in Xamarin.Forms?

I read the Xamarin docs https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/radiobutton and tried to follow the exact syntax given in the doc to include a radiobutton in my project. The code is as follows :
<StackLayout Grid.Column="1" Grid.Row="2">
<widget:RadioButton Text="Cat" />
</StackLayout>
The error I get here is 'invalid type: expected type is 'View', actual type is 'RadioButton' Is there a way to solve this ?
I think what your looking for in Xamarin.Forms is the control <RadioButton../> without the widgetpart.This will create a radiobutton in the native controls for UWP, iOS and Android.
This is the code I use to display radiobuttons.
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="TestButton.MainPage">
<StackLayout HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand">
<RadioButton Text="test radio button"/>
</StackLayout>
</ContentPage>
Beware it is experimental and you will need to add the build flag for it.
build flag for experimental XamarinForms controls
If you want to add the experimental flag for the RadioButton, you have to do so in all your projects (iOS, Android and UWP).I've added images to clarify where exactly (they need to be before the Xamarin.Forms.Forms.Init). This is the code to add:
global::Xamarin.Forms.Forms.SetFlags(new string[] { "RadioButton_Experimental" });
Here's where to add the code in the Android project. MainActivity.cs
Here's where to add the code in the iOS project. AppDelegate.cs
Here's where to add the code in the UWP project. App.xaml.cs

Xamarin conditional xaml based on compilation directive

I need the same as
Does XAML have a conditional compiler directive for debug mode?
but in Xamarin xaml; i've tried the proposed solution but i get this error:
Type mc:Choice not found in xmlns http://schemas.openxmlformats.org/markup-compatibility/2006
I've tried to use xcc https://github.com/firstfloorsoftware/xcc, but it seems not working with .netstandard 2.0 / Xamarin.Forms 4.5
Its little different in XAML of Xamarin. There is Design time namespacing in Xamarin.
You would use markup-compatibility xml name space and mark design namespace as Ignorable
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:ref="clr-namespace:XamUtilities.Views;assembly=XamUtilities"
mc:Ignorable="d"
x:Class="Sample.MainPage">
<ContentPage.Content>
<d:Label Text="This will be displayed only in previewer"/>
<Label Text="This will be displayed in actual app"/>
</ContentPage.Content>
</ContentPage>
In code behind also you can set values
[DesignTimeVisible (true)]
public partial class MainPage : ContentPage
{
public MainPage ()
{
InitializeComponent ();
if (DesignMode.IsDesignModeEnabled)
{
// Previewer only code
//add your bindings in here
}
}
}

Xamarin - WebView Pull To Refresh

I've searched everywhere on google and it seems like this 'Pull To Refresh' feature is only available for ListViews. There are some posts regarding 'Pull To Refresh' for WebView but they are coded in Java and in Android Studio. Is there any easy way to implement this?
You can use #jamesmontemagno's PullToRefreshLayout and include your WebView inside PullToRefreshLayout element:
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="RefreshSample.Views.ScrollViewXamlPage"
xmlns:controls="clr-namespace:Refractored.XamForms.PullToRefresh;assembly=Refractored.XamForms.PullToRefresh"
Title="Xaml Scroll">
<controls:PullToRefreshLayout
IsPullToRefreshEnabled="True"
RefreshCommand="{Binding RefreshCommand}"
IsRefreshing="{Binding IsBusy}"
RefreshColor="Blue">
<WebView>
</WebView>
</controls:PullToRefreshLayout>
</ContentPage>

Xamarin Start-up

I am a newbie in Xamarin. I created a C# cross-platform project, after the project has been created there were several errors I don't know why these errors occurred. How can I fix these errors? When I click build nothing displays. When I click Play my default app does not display.
App.xaml
<?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="App1.App">
<Application.Resources>
</Application.Resources>
</Application>
MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:App1"
x:Class="App1.MainPage">
<StackLayout>
<!-- Place new controls here -->
<Label Text="Welcome to Xamarin.Forms!"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand" />
</StackLayout>
</ContentPage>
MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace App1
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
}
}
Try to reinstall the Xamarin with Visual studio , It seems there has been something missing form installation. You can find the documentation here
https://learn.microsoft.com/en-us/xamarin/cross-platform/get-started/installation/windows
Regards
Right click on your project
Manage Nuuget Packages
Installed tab : Check if Xamarin is intalled, if yes update it, if not install xamarin
Clean and rebuild your project.

Code is not recognizing the name view in xaml (Xamarin Forms Visual Studio 2015)

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="PermeabilidadeSolo.Pages.TipoSoloPage"
Title="Tipo de Solo">
<StackLayout>
<ListView x:Name="lvTipoSolo" ItemSelected="TipoSoloSelected" />
</StackLayout>
</ContentPage>
When I try to use the listview in my file.cs it doesnt work, the file.cs doesnt recognize lvTipoSolo
In my case Visual Studio 2017 created the XAML file with the Custom Tool property set to MSBuild:Compile. After changing it to MSBuild:UpdateDesignTimeXaml it generated the private fields corresponding to the x:Name attributes.

Categories

Resources