I have a simple Stacklayout that shows Buttons.
I want to be able to let children remove itself from the stacklayout.
this project is just for testing purposes, so every button is linked to the same event-handler.
private void Button_Pressed_1(object sender, EventArgs e)
{
stack.Children.RemoveAt(stack.Children.Count - 1);
}
everything's fine until one button removes itself, then the following unsupported error appears:
Unhandled Exception:
System.NotSupportedException: Unable to activate instance of type
Xamarin.Forms.Platform.Android.AppCompat.ButtonRenderer from native
handle 0xbfb79bfc (key_handle 0x3e59524).
Has anyone an idea how to accomplish this? Since it's a nonSupportedException a simple try & catch didn't do the job
EDIT:
I got it working, i registered the eventhandler to the Pressed-Event. Appearently that was the problem, when using the Clicke-Event everything works just fine.
What 's version of Xamarin which you are using? I tried with Xamarin.Form 3.0.0.561731 and it worked well.
Please check my code as bellow:
Xaml page:
<?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:RemoveItSelf"
x:Class="RemoveItSelf.MainPage">
<StackLayout x:Name="stack">
<!-- Place new controls here -->
<Label Text="Welcome to Xamarin.Forms!"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand" />
<Label Text="Welcome to Xamarin.Forms!"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand" />
<Button Text="Remove" Clicked="PressMeButton_Clicked"></Button>
</StackLayout>
</ContentPage>
Xaml.cs page:
namespace RemoveItSelf
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private void PressMeButton_Clicked(object sender, EventArgs e)
{
//stack.Children.RemoveAt(0);
stack.Children.RemoveAt(stack.Children.Count - 1);
}
}
}
Related
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
I'm creating a cross platform app with Xamarin for iOS and Android which uses the camera. I didn't want to use the MediaPicker to access the camera because I wanted the app to display other information while taking a picture. I therefore used CameraView. I wrote the app and tested it with an Android emulator which was running on Android 11. However, I didn't think to test the app for previous versions of Android. The targeted version of Android is 11, but the minimum android version which works is 5.1. Therefore, the app functions on a phone which uses Android 9, but there is a bug when I try and take a picture. The method which is called upon when the user wants to take a picture is the following:
private void CaptureImage(object sender, EventArgs e)
{
xctCameraView.Shutter();
}
When I try and take a picture using Android 9, an error is thrown: System.NullReferenceException
Message=Object reference not set to an instance of an object.
I understand there may be some differences between android 9 and 11 and therefore the code doesn't work, but I've been searching for an alternative with no outcome. Can anyone help? Thanks!
EDIT:
<?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:xct="http://xamarin.com/schemas/2020/toolkit" Title="Prenez une photo" BackgroundColor="Transparent"
x:Class="App1.CameraViewPage">
<Grid x:Name="myGrid">
<xct:CameraView
x:Name="xctCameraView"
CaptureMode="Photo"
MediaCaptured="MediaCaptured"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"/>
<StackLayout VerticalOptions="EndAndExpand" Spacing="2" Padding="6">
<Button WidthRequest="70" HeightRequest="70" CornerRadius="40" BackgroundColor="Transparent" BorderColor ="White" BorderWidth="8" Clicked="CaptureImage" x:Name="captureBtn" HorizontalOptions="Center" VerticalOptions="Center"/>
</StackLayout>
<StackLayout VerticalOptions="StartAndExpand" Spacing="2" Padding="6">
<Label x:Name="labelOfMine" x:FieldModifier="public"
Text="" BackgroundColor="LightYellow">
</Label>
</StackLayout>
</Grid>
</ContentPage>
I use the code below to take a picture on Android 9 on emulator. It works. You could refer to it.
<StackLayout>
<xct:CameraView
Grid.Column="0"
x:Name="xctCameraView"
CaptureMode="Photo"
FlashMode="Off"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand" />
<Button Clicked="Button_Clicked"></Button>
</StackLayout>
Code behind:
private void Button_Clicked(object sender, EventArgs e)
{
xctCameraView.Shutter();
}
Permissions:
<uses-permission android:name="android.permission.CAMERA" />
this is my first time to start coding in mobile app (Xamarin) using C# and I'm trying to create button when I click it, Message Appear with some Information's I wrote this code in .xaml file :
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="App1.MainPage">
<ContentPage.Content>
<StackLayout>
<Button Text="click Me"
VerticalOptions="CenterAndExpand"
HorizontalOptions="Center"
Clicked="OnButtonClicked"/>
<Label x:Name ="lblMessage" FontSize="Large"></Label>
</StackLayout>
</ContentPage.Content>
</ContentPage>
and I wrote this in c# file :
namespace App1
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private void OnButtonClicked(object sender, EventArgs e)
{
lblMessage.Text = "save";
}
}
}
The error i had it's in lblMessage variable in c# file it's :
The name 'lblMessage' does not exist in the current context
I do it from youtube channel and this is was exactly what he did, but it's worked with him and gives an error with me, any idea or help ?
1.Clean your project first
2.Rebuild it, change your XAML file properties (Build Action) from Embedded Resource to C# complier, Rebuild your project (will throw errors).
3.Switch back your XAML file properties to Embedded Resource, Rebuild.
Refer to this link: https://forums.xamarin.com/discussion/96447/the-name-initializecomponent-does-not-exist-in-the-current-context
I am using this code to show image from an URL
.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="LandAHand.VolunteerView">
<ContentPage.Content>
<AbsoluteLayout BackgroundColor="Maroon">
<Image x:Name="backgroundImage" AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="All" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Aspect="AspectFill" />
</AbsoluteLayout>
</ContentPage.Content>
</ContentPage>
.cs
using System;
using System.Collections.Generic;
using Xamarin.Forms;
namespace LandAHand
{
public partial class VolunteerView : ContentPage
{
public VolunteerView()
{
InitializeComponent();
backgroundImage.Source = new UriImageSource
{
Uri = new Uri("https://s9.postimg.org/aq1jt3fu7/handshake_87122244_std.jpg"),
CachingEnabled = true,
CacheValidity = new TimeSpan(5, 0, 0, 0)
};
}
}
}
This code is successfully working with iOS but it is not working with Android.
Well you can do this thing easier with your Xaml just make your xaml like this
<Image x:Name="backgroundImage" Source="https://s9.postimg.org/aq1jt3fu7/handshake_87122244_std.jpg" AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="All" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Aspect="AspectFill"/>
And remove the code related in the code behind. The cashing is enabled by default for 24 hours
It's not working because you use a https url.
For fix it, you should be configure your Android Project like this :
In Project Options > Android Options, click Advanced Options
HttpClient implementation : choose Android
SSL/TLS implementation : choose Native TLS 1.2+
https://learn.microsoft.com/en-us/xamarin/android/app-fundamentals/http-stack?tabs=windows
And update all Xamarin.Android packages
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