MAUI: Change Label.Text using DisplayPromptAsync() - c#

I want to change the Content of a Label through the Pop-pup DisplayPromptAsync() which allows you to type text and save the text to use, but when I try to apply it does not work.
Label in XAML:
<Label Grid.Row="2" Text="UserName" x:Name="LabelName" FontAttributes="Bold" TextColor="White" HorizontalTextAlignment="Center" Margin="0,-45,0,0" FontSize="22"/>
Button in XAML:
<Button Grid.Column="1" WidthRequest="115" HeightRequest="45" Text="Name" TextColor="White" Margin="200, 2 ,200, -274" CornerRadius="19" Clicked="NameClicked"/>
Code-behind of Button:
private async void NameClicked(object sender, EventArgs e)
{
var ResultName = await DisplayPromptAsync("Insira seu Nome", "Favor inserir seu Nome","Ok");
LabelName.Text = ResultName;
}
I hoped that as soon as he kept the text in the variable could already assign and change quietly, only the code has no error is not working.

I create a demo and here is the code in .xaml. It works well on my side.
<VerticalStackLayout
Spacing="25"
Padding="30,0"
VerticalOptions="Center">
<Label Text="UserName" x:Name="LabelName" FontSize="22"/>
<Button Clicked="NameClicked"/>
</VerticalStackLayout>
Here is the code in .cs file.
private async void NameClicked(object sender, EventArgs e)
{
var ResultName = await DisplayPromptAsync("Insira seu Nome", "Favor inserir seu Nome", "Ok");
LabelName.Text = ResultName.ToString();
}

Related

Code so that when clicking on a button, an image is displayed in the same window

Good afternoon, I would like your help regarding this.
Code in C# Xamarin Net Maui, so that when clicking on a button, an image is displayed on the same screen.
I put images and example code
<ScrollView>
<VerticalStackLayout BackgroundColor="White">
<Label Text="a" BackgroundColor="BurlyWood" ></Label>
<Label Text="" TextColor="Black" ></Label>
<Label Text="b" BackgroundColor="BurlyWood" ></Label>
<Label Text="" TextColor="Black" ></Label>
<Label Text="c" BackgroundColor="BurlyWood" ></Label>
<Label Text="" TextColor="Black" ></Label>
<Label Text="d " BackgroundColor="BurlyWood" ></Label>
<Label Text="" TextColor="Black" ></Label>
<Button x:Name="changeimage" Text="changeimage[enter image description here](https://i.stack.imgur.com/cK6um.png)" BackgroundColor="Aqua" Clicked="changeimage_Clicked" ></Button>
<Image></Image>
</VerticalStackLayout>
</ScrollView>
public partial class Pagina12 : ContentPage
{
public Pagina12()
{
InitializeComponent();
}
private void Cambiarimagen_Clicked(object sender, EventArgs e)
{
}
}
that when clicking on the button called "change image" an image is displayed
Thank you very much in advance
first assign an x:Name to the Image
<Image x:Name="myImage />
then in your click handler
private void Cambiarimagen_Clicked(object sender, EventArgs e)
{
myImage.Source = ImageSource.FromUri("http://blah.com");
}
see the docs for different ways to load images

Swipeitem doesnt work when open the app and press the button

The swipeview doesnt work when I open the app and press accept button but it works after when I open and close the detail page.
xaml shows list of events from sql and show swipeitem
<CollectionView x:Name="eventview"
BackgroundColor="Transparent"
ItemSizingStrategy="MeasureAllItems"
SelectionMode="Single"
SelectionChanged="eventview_SelectionChanged"
EmptyView="No Pending events">
<SwipeView.RightItems>
<SwipeItems SwipeBehaviorOnInvoked="Close">
<SwipeItem Text="Decline" BackgroundColor="red"/>
<SwipeItem Text="Accept" BackgroundColor="Green" Invoked="SwipeItem_Invoked"/>
</SwipeItems>
</SwipeView.RightItems>
<Grid Padding="10">
<StackLayout>
<Label Text="{Binding Name}" VerticalOptions="Center" FontSize="Small"/>
<Label Text="{Binding Date}" VerticalOptions="Center" FontSize="Small"/>
<Label Text="{Binding stats}" VerticalOptions="Center" FontSize="Small"/>
</StackLayout>
</Grid>
</SwipeView>
</DataTemplate>
</CollectionView.ItemTemplate>
cs shows click on the item that will open detail page and when click on the swipeitem
private async void eventview_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
lastSelection = e.CurrentSelection[0] as Event;
var eventss = e.CurrentSelection.FirstOrDefault() as Event;
var eventpage = new Eventdetailpage();
eventpage.BindingContext = eventss;
await Navigation.PushAsync(eventpage);
}
private async void SwipeItem_Invoked(object sender, EventArgs e)
{
if (lastSelection != null)
{
lastSelection.stats = "Accepted";
await App.Database.UpdateventAysnc(lastSelection);
eventview.ItemsSource = await App.Database.querypendingevent();
accpetedevents.ItemsSource = await App.Database.queryacceptevent();
}
}
if lastSelection is null, then your if won't execute and nothing will happen
you should be able to get the current item from the sender's BindingContext
var item = (SwipeItem)sender;
lastSelection = (Event)item.BindingContext;

Display a page with extended information

I have the following functionality given below:
When clicking on details, i want the text that is displayed on the contentview shall be displayed on the new detailspage that is created by push async. How can I send parameters containing the info given in the content, e.g. title, category and description.
I have a tap gesture recognizer:
<Label.GestureRecognizers>
<TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"/>
</Label.GestureRecognizers>
That then leads to creating a new page with the code behind:
private async void TapGestureRecognizer_Tapped(object sender, EventArgs e)
{
await Navigation.PushAsync(new DetailsPage(user), true);
}
So how shall I bind the labels containing title, description and category to display in the new page that is created upon click?
Here's the xaml code behind the labels:
<StackLayout>
<StackLayout Orientation="Horizontal">
<BoxView Color="LightGray"
WidthRequest="90"
HeightRequest="90"
HorizontalOptions="Center"
VerticalOptions="Center"/>
<StackLayout Padding="0">
<Label x:Name="Title" Text="Title"
FontSize="22"
FontFamily="Grotesk"
TextColor="Black"
Margin="10, -2, 0, 0"
VerticalOptions="Fill" />
<Label x:Name="Category" Text="Category"
FontSize="16"
FontFamily="Grotesk"
TextColor="Gray"
Margin="10, -2, 0, 0"
VerticalOptions="Fill" />
<Label x:Name="Desc" Text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. etc.etc"
Margin="10, -4, 0, 0"
FontSize="15"
FontFamily="Latow"
VerticalOptions="Start"
MaxLines="3"/>
</StackLayout>
</StackLayout>
use the Label's BindingContext
private async void TapGestureRecognizer_Tapped(object sender, EventArgs e)
{
var label = (Label)sender;
// you will need to cast it to the correct type
var item = (MyClass)label.BindingContext;
// then pass it to your page
await Navigation.PushAsync(new DetailsPage(user,item), true);
}

Xamarin forms Change and enable image button

Sample UI design
Does anyone know how to change Imagebutton source? Refer to the picture link, when user click on step1 (MainPage), it will redirect user to another page (SecondPage). Once user click on "done" button in SecondPage, I want to change my imagebutton source and at the same time, enable click event. I am not sure not to call the function from one page to another.
(P.S my btnclickcount doesn't seems to be working sometimes... I only want to record the start date time on first button click)
public partial class MainPage : ContentPage
{
public string mainpagevalue;
int offlinecount = 0;
int onlinecount = 0;
public MainPage()
{
InitializeComponent();
}
private void btnOffline_Clicked(object sender, EventArgs e)
{
offlinecount++;
txtOfflineStatus.Text = "IN PROGRESS";
Navigation.PushAsync(new SecondPage(this, lblEndDT, txtOfflineStatus, btnOnline, btnMH));
if (offlinecount == 1)
{
string currentDT = DateTime.Now.ToString();
lblStartDT.Text = currentDT;
}
}
private void btnOnline_Clicked(object sender, EventArgs e)
{
onlinecount++;
txtOnlineStatus.Text = "IN PROGRESS";
Navigation.PushAsync(new ThirdPage(this, lblOnlineEndDT, btnTS, txtOnlineStatus));
if (onlinecount == 1)
{
string onlinestartDT = DateTime.Now.ToString();
lblOnlineStartDT.Text = onlinestartDT;
}
}
Third Page
public partial class ThirdPage : ContentPage
{
Label wopLblOnlineEndDT;
MainPage mainpage;
ImageButton btntroubleshoot;
Label wolblOnlineStatus;
public ThirdPage()
{
InitializeComponent();
}
public ThirdPage(MainPage woPage, Label lblOnlineEndDT, ImageButton btnTS, Label lblOnlineStatus)
{
InitializeComponent();
mainpage = woPage;
wopLblOnlineEndDT = lblOnlineEndDT;
btntroubleshoot = btnTS;
wolblOnlineStatus = lblOnlineStatus;
}
private void BtnDone_Clicked(object sender, EventArgs e)
{
string edt = DateTime.Now.ToString();
wopLblOnlineEndDT.Text = edt;
mainpage.mainpagevalue = wopLblOnlineEndDT.Text;
btntroubleshoot.Source = "troubleshooting";
btntroubleshoot.IsEnabled = true;
wolblOnlineStatus.Text = "COMPLETED";
wolblOnlineStatus.TextColor = Color.FromRgb(0, 214, 54);
Navigation.PopAsync();
}
}
XAML
<ImageButton x:Name="btnOffline" IsEnabled="True" Source="#drawable/offlinetool.png" Grid.Row="1" Grid.Column="1" BackgroundColor="Transparent" Clicked="btnOffline_Clicked"/>
<Label Text="Offline Tool" Grid.Row="2" Grid.Column="1" Margin="15,0,0,0"/>
<ImageButton Source="#drawable/material.png" Grid.Row="4" Grid.Column="1" BackgroundColor="Transparent"/>
<Label Text="Material Handler" Grid.Row="5" Grid.Column="1" />
<Image Source="#drawable/Picture1.png" Grid.Row="6" Grid.ColumnSpan="6" BackgroundColor="Transparent"/>
<Label Text="Start Date Time:" Grid.Row="1" Grid.Column="3"/>
<Label Text="End Date Time:" Grid.Row="1" Grid.Column="3" Margin="7,40,0,0"/>
<Label Text="Status:" Grid.Row="2" Grid.Column="3" Margin="58,0,0,0" />
<Label x:Name="txtOfflineStatus" Text="NOT STARTED" TextColor="Red" Grid.Column="4" Grid.Row="2"/>
<Label x:Name="lblStartDT" Text="" Grid.Column="4" Grid.Row="1"/>
<Label x:Name="lblEndDT" Text="-" Grid.Column="4" Grid.Row="1" Margin="0,40,0,0"/>
<Label Text="Start Date Time:" Grid.Row="4" Grid.Column="3" />
<Label Text="End Date Time:" Grid.Row="4" Grid.Column="3" Margin="7,40,0,0"/>
<Label Text="Status:" Grid.Row="5" Grid.Column="3" Margin="58,0,0,0" />
<Label Text="NOT STARTED" TextColor="Red" Grid.Column="4" Grid.Row="5"/>
<!--#Online Tool-->
<ImageButton x:Name="btnOnline" Source="#drawable/ot.png" Grid.Row="8" Grid.Column="1" BackgroundColor="Transparent" Clicked="btnOnline_Clicked" IsEnabled="False"/>
<Label Text="Online Tool" Grid.Row="9" Grid.Column="1" Margin="19,0,0,0"/>
<ImageButton x:Name="btnMH" Source="#drawable/mh.png" Grid.Row="11" Grid.Column="1" BackgroundColor="Transparent" Clicked="imgbtnMH_Clicked" IsEnabled="False"/>
<Label Text="Material Handler" Grid.Row="12" Grid.Column="1"/>
<Image Source="#drawable/Picture1.png" Grid.Row="13" Grid.ColumnSpan="6" BackgroundColor="Transparent"/>
<Label Text="Start Date Time:" Grid.Row="8" Grid.Column="3" />
<Label Text="End Date Time:" Grid.Row="8" Grid.Column="3" Margin="7,40,0,0"/>
<Label Text="Status:" Grid.Row="9" Grid.Column="3" Margin="58,0,0,0" />
<Label Text="NOT STARTED" TextColor="Red" Grid.Column="4" Grid.Row="9"/>
<Label x:Name="lblOnlineStartDT" Text="00:00:00" Grid.Column="4" Grid.Row="8"/>
<Label x:Name="lblOnlineEndDT" Text="" Grid.Column="4" Grid.Row="8" Margin="0,40,0,0"/>
<Label x:Name="txtOnlineStatus" Text="NOT STARTED" TextColor="Red" Grid.Column="4" Grid.Row="9"/>
You can pass the reference of imageButton to the SecondPage too and change the Source in the done method. Let me show you how to do it:
In the MainPage xaml, create an ImageButton named MyImageButton:
<StackLayout>
<!-- Place new controls here -->
<Label x:Name="lblStartDT" Text="startTest"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand"/>
<Button Text="go to secondPage" Clicked="btnOffline_Clicked" HorizontalOptions="Center"
VerticalOptions="CenterAndExpand"/>
<Label x:Name="lblEndDT" Text="endTest"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand" />
<ImageButton x:Name="MyImageButton" Source="Images.png" IsEnabled="False"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand" />
</StackLayout>
In code behind, pass MyImageButton to the SecondPage:
private void btnOffline_Clicked(object sender, EventArgs e)
{
//Pass the parametere you need when you go to SecondPage
//Navigation.PushAsync(new SecondPage(this, lblEndDT));
Navigation.PushAsync(new SecondPage(this, lblEndDT, MyImageButton));
string currentDT = DateTime.Now.ToString();
lblStartDT.Text = currentDT;
}
In the SecondPage, get the reference of myImageBtn and modify it in the done method:
public partial class SecondPage : ContentPage
{
Label MainPagelblEndDT;
MainPage mainPage;
ImageButton myImageBtn;
public SecondPage()
{
InitializeComponent();
}
public SecondPage(MainPage mainP,Label lblEndDT)
{
InitializeComponent();
//Get the lblEndDT reference here
MainPagelblEndDT = lblEndDT;
//Get the MainPage reference here
mainPage = mainP;
}
public SecondPage(MainPage mainP, Label lblEndDT, ImageButton imageBtn)
{
InitializeComponent();
//Get the lblEndDT reference here
MainPagelblEndDT = lblEndDT;
//Get the MainPage reference here
mainPage = mainP;
//Get the ImageButton reference here
myImageBtn = imageBtn;
}
private void Button_Clicked(object sender, EventArgs e)
{
string edt = DateTime.Now.ToString();
//Use it
MainPagelblEndDT.Text = edt;
mainPage.previouspagevalue = MainPagelblEndDT.Text;
//change the source of imageBtn
myImageBtn.Source = "Images1";
myImageBtn.IsEnabled = true;
Navigation.PopAsync();
}
}
A sample project is available here.
Bind the button Opacity to something like 0.5 and also disable the button.
<ImageButton Command="{Binding EditActivityCommand}"
BackgroundColor="Transparent"
Opacity="{Binding Opacity}"
IsEnabled="{Binding IsAdmin}">

Xamarin Forms: Stacklayout Visible with System.Timers

I am on the most current version of Xamarin Forms. I have a Content Page. The Content Page has a grid that has a StackLayout and ScrollView. StackLayout Visible is false at the start point. When I click my Login Button, which has a Login method (see below) I set the StackLayout visible true. I use System.Timers too which start when login button clicked. If this timer reach 10 sec and the login isn't succesful the timer elapsed method activate. This method you can see below. At this point this work great, but I want to Login again and the StackLayout content doesn't show up. Can Anybody help me?
LoginPage.xml code:
<?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="Spirocco.LoginPage"
xmlns:renderer="clr-namespace:Spirocco.Droid.Renderers">
<Grid>
<StackLayout x:Name="stackView" IsVisible="False" HorizontalOptions="Center" VerticalOptions="Center" WidthRequest="300" HeightRequest="50" BackgroundColor="LightGray" Orientation="Horizontal">
<ActivityIndicator IsRunning="True" Color="Black" HorizontalOptions="Center" Margin="20" HeightRequest="30" WidthRequest="30"/>
<Label Text="Bejelentkezés..." TextColor="Black" VerticalOptions="Center" FontSize="16"/>
</StackLayout>
<ScrollView Orientation="Both" x:Name="scrollView">
<ScrollView.Content>
<StackLayout BackgroundColor="#302138">
<Image Source="login_logo" Margin="0,0,0,0"></Image>
<StackLayout BackgroundColor="White" Margin="20,0,20,30">
<Label Text="ÜDVÖZÖLJÜK!" FontSize="30" FontFamily="Comic Sans MS" Margin="0,15,0,0" TextColor="#302138" HorizontalTextAlignment="Center"></Label>
<renderer:BaseEntry x:Name="entryEmail" Text="{Binding Email}" Placeholder="E-mail" Margin="40,0,40,0" Keyboard="Email" ReturnType="Next"/>
<renderer:BaseEntry x:Name="entryPassword" Text="{Binding Password}" Placeholder="Jelszó" IsPassword="True" Margin="40,0,40,0" ReturnType="Send"/>
<Button Text="BEJELENTKEZÉS" Clicked="Login" TextColor="White" BackgroundColor="#302138" Margin="40,10,40,0"/>
<Button Text="REGISZTRÁCIÓ" Clicked="Register" TextColor="White" BackgroundColor="#302138" Margin="40,0,40,25"/>
</StackLayout>
<Label BackgroundColor="#302138" HeightRequest="160"/>
</StackLayout>
</ScrollView.Content>
</ScrollView>
</Grid>
My login method:
private async void Login(object sender, EventArgs e)
{
if (entryEmail.Text != null && entryPassword.Text != null)
{
try
{
stackView.IsVisible = true;
scrollView.Opacity = 0.5;
timer = new Timer(10000);
timer.Start();
timer.Elapsed += SetContentViewVisible;
}
catch (Exception)
{
stackView.IsVisible = false;
scrollView.Opacity = 1;
await DisplayAlert("Hiba történt", "Sikertelen bejelentkezés", "Vissza");
}
}
else
{
await DisplayAlert("Bejelentkezés", "Sikertelen bejelentkezés, kérem a sikeres bejelentkezéshez töltse ki az e-mail cím és jelszó mezőt!", "Vissza");
}
}
SetContentViewVisible method:
private void SetContentViewVisible(object sender, ElapsedEventArgs e)
{
timer.Dispose();
scrollView.Opacity = 1;
stackView.IsVisible = false;
timer.Stop();
}
I want to refresh UI from different thread. This was problem.
private void SetContentViewVisible(object sender, ElapsedEventArgs e)
{
Device.BeginInvokeOnMainThread(
() =>
{
timer.Dispose();
scrollView.Opacity = 1;
stackView.IsVisible = false;
timer.Stop();
DisplayAlert(SaySomething);
});
}

Categories