Xamarin forms navigation - c#

I am having trouble with Xamarin forms navigation, as in, it does not work at all.
My code :
appname.cs
using System;
using Xamarin.Forms;
namespace List
{
public class App : Application {
public App () {
MainPage = new NavigationPage (new HomePage ());
}
}
}
homepage.xaml.cs
using System;
using Xamarin.Forms;
using System.ComponentModel;
using System.Globalization;
namespace List
{
public partial class HomePage : ContentPage {
public HomePage () {
InitializeComponent ();
}
public void OnButtonClicked (object sender, EventArgs args) {
Navigation.PushAsync (new DetailsPage ());
}
}
}
My intention is obviously to open a main page, have a button on there that goes to a second page upon press.
However this happens instead whenever the button is clicked:
error message
Could not load type 'UIKit.UIView_UITextField' from assembly 'Xamarin.Forms.Platform.iOS'.
Can someone tell me what I'm doing wrong here?
Edit - code for DetailsPage, since this could be source of error:
using System;
using System.Collections.Generic;
using Xamarin.Forms;
namespace List
{
public partial class DetailsPage : ContentPage
{
public DetailsPage ()
{
InitializeComponent ();
}
}
}
Edit 2 - code for Xaml pages (thanks for the help everyone, I really appreciate it):
Homepage.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:List" x:Class="List.HomePage">
<ContentPage.Title>
1
</ContentPage.Title>
<Button Text="Click Me!" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" Clicked="OnButtonClicked" />
</ContentPage>
DetailsPage.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" x:Class="List.DetailsPage">
<Label Text="LOL"/>
</ContentPage>

In your xaml, the first element needs to be a layout (if you have multiple elements). I would try something similar to below and see if anything changes.
HomePage.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:List" x:Class="List.HomePage" Title="1">
<StackLayout>
<Button Text="Click Me!" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" Clicked="OnButtonClicked" />
</StackLayout>
</ContentPage>
DetailsPage.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" x:Class="List.DetailsPage">
<StackLayout>
<Label Text="LOL"/>
</StackLayout>
</ContentPage>
Also, I noticed you were setting your title like using ContentPage.Title 1 ContentPage.Title. This is unnecessary. Just set the title directly in your ContentPage tag as I showed in the above example.

Well, I was having the same problem as you. I came across this solution of the xamarin guide.
It seems that in app.cs you need to call the using Xamarin.Forms.Xaml;
Right above your namespace insert [assembly:XamlCompilation(XamlCompilationOptions.Compile)]
But I really can't tell why. I'm Looking for the reason. Try to see if it resolves. In my case went well. Sorry about the bad english and the bad edit of the post, kind of new to this. But comparing all your code, it was missing this piece for you too, maybe this is the reason.
EDIT (12/01/2017) - Add
i found this, that might explain why was not working, seems like the function that we are using is part of a new set for Xamarin that helps him to became faster.
I can be wrong, if I am, please correct me someone that knows better.

Navigation.PushModalAsync(new DetailsPage());
Reference: http://developer.xamarin.com/guides/cross-platform/xamarin-forms/introduction-to-xamarin-forms/#Navigation

Related

MAUI websocket without MVVM

I am trying to learn how to create mobile apps using MAUI and have seen many MVVM examples which I find incomplete. For this reason, I thought I should try to learn MAUI using CodeBehind first before moving onto MVVM. I have completey the MAUI section from the Microsoft Learn website, but it didn't cover displaying websocket data in XAML.
I have the following:
XAML:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="CodeBehindTicker.MainPage">
<ScrollView>
<VerticalStackLayout>
<Label
Text="Ticker value"
x:Name="lblTicker" />
<Button
x:Name="btnGetTicker"
Text="Get Ticker"
Clicked="btnGetTicker_Clicked"/>
</VerticalStackLayout>
</ScrollView>
</ContentPage>
CodeBehind:
using Binance.Net.Clients;
using Binance.Net.Objects;
namespace CodeBehindTicker;
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private void btnGetTicker_Clicked(object sender, EventArgs e)
{
var socketClient = new BinanceSocketClient(new BinanceSocketClientOptions { });
socketClient.SpotStreams.SubscribeToBookTickerUpdatesAsync("BTCUSDT", data => {
lblTicker.Text = data.Data.BestAskPrice.ToString();
Console.WriteLine(data.Data.BestAskPrice.ToString());
});
socketClient.UnsubscribeAllAsync();
}
}
When I click the button, the websocket data displays in realtime in the console output, but the label only displays the very first data returned and does not automatically update as the data in the console output updates instantly.
Any idea why? For now, I just want to stick to basic CodeBehind without using MVVM.
My guess is since you are using A WebSocket which probably has some async code the text is not changed on the main thread.
All you need to do is something like this:
MainThread.BeginInvokeOnMainThread(() =>
lblTicker.Text = data.Data.BestAskPrice.ToString());
Hope this helps

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
}
}
}

Target Invocation exception in Xamarin Forms Android when using french culture with font attributes

I used the labels in my xamarin page and set FontAttributes as like below code in my xaml page. By default (US culture) no issues. When i change my android device culture as french i am getting System.Reflection.TargetInvocationException. If i remove FontAttributes, it is working fine. I can't debug or get details from output window. Anyone please help on this.
My Code
<Label x:Name="continentLabel" FontAttributes="Bold"/>
I created a simple xamarin forms project and edited the xaml file as you have posted, I also added two RESX files:AppResources.resx and AppResources.fr.resx, then I set the simulator's language setting to French. It works all right. I think you may missed something, so I post my code here:
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:App34"
x:Class="App34.MainPage">
<StackLayout>
<!-- Place new controls here -->
<Label Text="Welcome to Xamarin.Forms!"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand" />
<Label x:Name="String1"
FontAttributes="Bold"
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 App34
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
String1.Text = AppResources.String1;
}
}
}

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.

Proper way to use android map renderer?

What is the proper object to inherit from when trying to add a Tap handler on a map on android? I've tried with MapView, MapRenderer, MapActivity, Overlay to name a few.
CustomMapRenderer.cs
Totally wrong, but showing what I've been trying.
using Xamarin.Forms;
using nuMaps;
using nuMaps.Droid;
using Android.Gms.Maps;
using System;
using Xamarin.Forms.Maps.Android;
using Xamarin.Forms.Platform.Android;
using Android.App;
using Xamarin.Forms.Maps;
using Android.Widget;
using Android.GoogleMaps;
namespace nuMaps.Droid
{
public class CustomMapRenderer MapActivity
{
public override bool OnTouchEvent (Android.Views.MotionEvent e, Android.GoogleMaps.MapView mapView)
{
Console.WriteLine (e.Action);
return base.OnTouchEvent (e, mapView);
}
}
}
MapPage.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:nuMaps;assembly=nuMaps"
x:Class="nuMaps.MapPage">
<ContentPage.Content>
<AbsoluteLayout>
<local:CustomMap
x:Name="MyMap"
IsShowingUser="true"
MapType="Hybrid"
AbsoluteLayout.LayoutBounds="0, 0, 1, 1"
AbsoluteLayout.LayoutFlags="All"
/>
</AbsoluteLayout>
</ContentPage.Content>
</ContentPage>
I implemented simple extended map control to test custom map renderer. You can have a look here. Not sure if this is that you need. The only purpose of this class is to provide tap location for all three platforms. It's pure sample code, not for production, works fine for me but if you have any problems please tell me.

Categories

Resources