I am using xamarin community toolkit for LongPress and GestureRecognizers for PinchGestureRecognizer. After the completion of PinchGestureRecognizer, xct:TouchEffect.LongPressCommand is also fired. Is there any way to trigger these events once at a time?
Here is my code sample
<StackLayout
xct:TouchEffect.LongPressCommand="{Binding LongPressCommand}"
xct:TouchEffect.LongPressCommandParameter="LongPress"
BackgroundColor="Red">
<Frame
Padding="24"
BackgroundColor="#2196F3"
CornerRadius="0">
<Label
FontSize="36"
HorizontalTextAlignment="Center"
Text="Welcome to Xamarin.Forms!"
TextColor="White" />
</Frame>
<Label
Padding="30,10,30,10"
FontSize="Title"
Text="Start developing now" />
<Label
Padding="30,0,30,0"
FontSize="16"
Text="Make changes to your XAML file and save to see your UI update in the running app with XAML Hot Reload. Give it a try!" />
<Label Padding="30,24,30,0" FontSize="16">
<Label.FormattedText>
<FormattedString>
<FormattedString.Spans>
<Span Text="Learn more at " />
<Span FontAttributes="Bold" Text="https://aka.ms/xamarin-quickstart" />
</FormattedString.Spans>
</FormattedString>
</Label.FormattedText>
</Label>
<StackLayout.GestureRecognizers>
<PinchGestureRecognizer PinchUpdated="OnPinchUpdated" />
</StackLayout.GestureRecognizers>
</StackLayout>
The cs file
public ICommand LongPressCommand { get; set; }
public MainPage()
{
InitializeComponent();
LongPressCommand = new Command<string>(LongPress);
BindingContext = this;
}
public void LongPress(string flag)
{
}
private void OnPinchUpdated(object sender, PinchGestureUpdatedEventArgs e)
{
}
}
You can set a duration of the LongPress command to trigger the command. The default is 500ms, you can set it via LongPressDuration property. In the example below, I set it to 3s which means the LongPress will be triggered after 3s . You can set the proper duration to avoid the conflict with PinchGestureRecognizer.
xct:TouchEffect.LongPressDuration="3000"
Related
I'm currently trying to create a Button with custom color text when disabled. For this point i successfully did the job with a custom renderer.
But an unexpected side effect as came. Some button style are disapplying when passing on this specific state.
As you can see on the image above the button icon position is different and the cornerRadius property isn't applied anymore.
In the enable state with the custom button renderer i recover the cornerRadius property (actually i can resolve this problem setting each time the radius in the renderer, so isn't really a problem). The icon remain at the top and doesn't respect the ContentLayout margin property. The renderer i expect is the left button who is did with normal button component.
Here is the declaration of my control (i put the left button too to demonstrate the settings are the same:
<StackLayout Orientation="Horizontal" Margin="0,50,0,0">
<Button Text="Disponibilités de l'élève" BackgroundColor="{StaticResource Primary}" BorderColor="LightGray" BorderWidth="1" TextColor="White" FontAttributes="Bold" CornerRadius="10" ContentLayout="Top,25" WidthRequest="{Binding ButtonWidth}" HorizontalOptions="CenterAndExpand" Command="{Binding OpenMapCommand}">
<Button.ImageSource>
<FontImageSource FontFamily="FASolid" Color="White" Glyph="{x:Static fa:FontAwesomeIcons.CalendarDays}"/>
</Button.ImageSource>
</Button>
<!--<Button Text="Poser une option" BackgroundColor="{StaticResource Primary}" TextColor="White" BorderColor="LightGray" BorderWidth="1" FontAttributes="Bold" CornerRadius="10" ContentLayout="Top,25" WidthRequest="{Binding ButtonWidth}" HorizontalOptions="CenterAndExpand" Command="{Binding SubmitOptionCommand}">
<Button.ImageSource>
<FontImageSource FontFamily="FARegular" Color="White" Glyph="{x:Static fa:FontAwesomeIcons.CalendarPlus}"/>
</Button.ImageSource>
</Button>-->
<controls:DisableExtentButton Text="Poser une option" BackgroundColor="{StaticResource Primary}" DisabledBackgroundColor="LightGreen" TextColor="White" DisabledTextColor="Green" BorderColor="LightGray" BorderWidth="1" FontAttributes="Bold" CornerRadius="10" ContentLayout="Top,25" WidthRequest="{Binding ButtonWidth}" HorizontalOptions="CenterAndExpand" Command="{Binding SubmitOptionCommand}">
<Button.Triggers>
<DataTrigger TargetType="Button" Binding="{Binding OptionTaken}" Value="True">
<Setter Property="IsEnabled" Value="False"/>
<Setter Property="ImageSource">
<Setter.Value>
<FontImageSource FontFamily="FARegular" Color="Green" Glyph="{x:Static fa:FontAwesomeIcons.CalendarCheck}"/>
</Setter.Value>
</Setter>
</DataTrigger>
</Button.Triggers>
<Button.ImageSource>
<FontImageSource FontFamily="FARegular" Color="White" Glyph="{x:Static fa:FontAwesomeIcons.CalendarPlus}"/>
</Button.ImageSource>
</controls:DisableExtentButton>
</StackLayout>
Here is the declaration of my custom Control:
public class DisableExtentButton : Button
{
public Color DisabledTextColor
{
get { return (Color)GetValue(DisabledTextColorProperty); }
set { SetValue(DisabledTextColorProperty, value); }
}
public Color DisabledBackgroundColor
{
get { return (Color)GetValue(DisabledBackgroundColorProperty); }
set { SetValue(DisabledBackgroundColorProperty, value); }
}
public static readonly BindableProperty DisabledTextColorProperty = BindableProperty.Create(nameof(DisabledTextColor), typeof(Color), typeof(DisableExtentButton), Color.Gray);
public static readonly BindableProperty DisabledBackgroundColorProperty = BindableProperty.Create(nameof(DisabledBackgroundColor), typeof(Color), typeof(DisableExtentButton), Color.DarkGray);
}
The android renderer:
public class DisableExtentButtonRenderer : ButtonRenderer
{
public DisableExtentButtonRenderer(Context context) : base(context) { }
protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
{
if (Control != null)
SetColors();
base.OnElementChanged(e);
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(Button.IsEnabled))
SetColors();
base.OnElementPropertyChanged(sender, e);
}
private void SetColors()
{
var element = (DisableExtentButton)this.Element;
Control.SetTextColor(Element.IsEnabled ? Element.TextColor.ToAndroid() : element.DisabledTextColor.ToAndroid());
Control.SetBackgroundColor(Element.IsEnabled ? Element.BackgroundColor.ToAndroid() : element.DisabledBackgroundColor.ToAndroid());
}
}
I didn't successfully found why the ContentLayout property isn't applied correctly anymore and more for informational purpose why the corner radius is reseted when passing into OnElementPropertyChanged. If i redo the button without custom renderer it work as expected like the left button.
I am quite new in xamarin and I am trying to add a click events to my buttons. So here are my 4 buttons:
<StackLayout HorizontalOptions="Center" VerticalOptions="Center">
<Button Text="Page 1" TextColor="#EEF2FF" FontSize="20" HorizontalOptions="Center" BackgroundColor="#FF5959" />
</StackLayout>
<StackLayout Grid.Column="1" HorizontalOptions="Center" VerticalOptions="Center">
<Button Text="Page 2" Clicked="Handle_Page" TextColor="#676FA3" FontSize="20" HorizontalOptions="Center" BackgroundColor="#EEF2FF"/>
</StackLayout>
<StackLayout Grid.Column="2" HorizontalOptions="Center" VerticalOptions="Center">
<Button Text="Page 3" TextColor="#676FA3" FontSize="20" HorizontalOptions="Center" BackgroundColor="#EEF2FF"/>
</StackLayout>
<StackLayout Grid.Column="3" HorizontalOptions="Center" VerticalOptions="Center">
<Button Text="Page 4" TextColor="#676FA3" FontSize="20" HorizontalOptions="Center" BackgroundColor="#EEF2FF"/>
</StackLayout>
So the colour of the buttons are #EEF2FF but when I click on the button I want it to be changed to #FF5959. But if there is another button clicked already, then it should also change to the main colour, #EEF2FF. So we can cay synchronized click event.
I am quite new, so any help would be appreciated.
add a clicked handler
<Button Clicked="ButtonClicked" ... />
then in the handler
protected void ButtonClicked(object sender, EventArgs args)
{
// set the other buttons back to the base color
// you will need to assign each button an x:Name in xaml
btn1.BackgroundColor = Color.FromHex("EEF2FF");
...
btn10.BackgroundColor = Color.FromHex("EEF2FF");
// then change the color of only the selected button
var btn = (Button)sender;
btn.BackgroundColor = Color.FromHex("FF5959");
}
protected override void OnCreate(Bundle savedInstanceState)
{
ScreenClickButton.Click += ChangeColorToRed;
}
private void ChangeColorToRed(object sender, EventArgs e)
{
ScreenClickButton.SetBackgroundColor(color: Color.Red);
}
I am using Rg.Plugins.Popup library to create a Popup alert. I don't know how to get a return value (boolean value). Can someone please help with this?
<pages:PopupPage.Animation>
<animations:ScaleAnimation
HasBackgroundAnimation="True"
PositionIn="Top"
PositionOut="Center"
/>
</pages:PopupPage.Animation>
<StackLayout Margin="12"
Padding="24"
WidthRequest="307"
HeightRequest="264"
HorizontalOptions="Center"
VerticalOptions="Center">
<StackLayout Orientation="Horizontal"
HorizontalOptions="Center">
<Button BackgroundColor="#61BC52"
Margin="0,0,18,0"
FontSize="18"
Text="Cancel"
CornerRadius="20"
WidthRequest="108"
HeightRequest="41"
TextColor="White" />
<Button BackgroundColor="#61BC52"
Text="Restart"
FontFamily="PatrickFont"
CornerRadius="20"
WidthRequest="108"
HeightRequest="41"
TextColor="White" />
</StackLayout>
</StackLayout>
</pages:PopupPage>
And this is how it's called from the ViewModel of my page.
private void RefreshIconCommandExecute(object obj)
{
PopupNavigation.Instance.PushAsync(new RestartPopup());
}
Essentially, I want to be able to get the response as like the following but with the Rg Popup instead:
var response = App.Current.MainPage.DisplayAlert("Title", "Message", "Accept", "Cancel");
I have a ListView and I want to delete some items, I have not found a useful answer yet.
this is a XMAL :
<ListView.ItemTemplate >
<DataTemplate>
<ViewCell>
<StackLayout>
<Label Text="{Binding Name}"
Style="{DynamicResource ListItemTextStyle}" />
<Label Text="{Binding PhoneNo}"
Style="{DynamicResource ListItemDetailTextStyle}"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
and listview :
public ObservableCollection<Contact> ContactList2 { get; set; }
I can easily add it, but I do not know how to delete it.
Use a context menu
XAML
<ViewCell.ContextActions>
<MenuItem Clicked="OnDelete" CommandParameter="{Binding .}"
Text="Delete" IsDestructive="True" />
</ViewCell.ContextActions>
code behind
public void OnDelete (object sender, EventArgs e) {
var mi = ((MenuItem)sender);
Contact contact = (Contact)mi.CommandParameter;
ContactList2.Remove(contact);
}
I am trying to register a user on my application using web API on Xamarin.
Here is registration form in RegisterPage.xaml
<Entry Text="{Binding FirstName}" Placeholder="First Name"/>
<Entry Text="{Binding LastName}" Placeholder="Last Name"/>
<Entry Text="{Binding UserName}" Placeholder="Username"/>
<Entry Text="{Binding Email}" Placeholder="Email" />
<Entry Text="{Binding Password}" Placeholder="Password" IsPassword="True"/>
<Entry Text="{Binding ConfirmPassword}" Placeholder="Confirm Password" IsPassword="True"/>
<DatePicker x:Name="BirthDay" MinimumDate="1/1/1948" MaximumDate="12/31/2007"/>
<Label Text="Gender"/>
<Picker x:Name="GenderPicker" SelectedIndexChanged="GenderPicker_OnSelectedIndexChanged"/>
<Label Text="User Role"/>
<Picker x:Name="RolePicker" SelectedIndexChanged="RolePicker_OnSelectedIndexChanged"/>
<Button Command="{Binding RegisterCommand}" Text="Register"/>
<Label Text="{Binding Message}" />
Here is my RegisterPage.xaml.cs file
public partial class RegisterPage : ContentPage
{
public RegisterPage()
{
InitializeComponent();
GenderPicker.Items.Add("Male");
GenderPicker.Items.Add("Female");
RolePicker.Items.Add("Admin");
RolePicker.Items.Add("Participant");
}
private void GenderPicker_OnSelectedIndexChanged(object sender, EventArgs e)
{
var gender = GenderPicker.Items[GenderPicker.SelectedIndex];
}
private void RolePicker_OnSelectedIndexChanged(object sender, EventArgs e)
{
var role = RolePicker.Items[RolePicker.SelectedIndex];
}
}
On submitting the registration form I am not receiving my values against DatePiker and Picker into the RegisterCommand
Here is my RegisterCommand , I am receiving other attributes but not from DatePiker and Picker
public ICommand RegisterCommand
{
get
{
return new Command(async () =>
{
var isSuccess = await _apiServices.RegisterAsync(FirstName,LastName,UserName,Email,Password,ConfirmPassword,BirthDate,Gender,UserRole);
Message = isSuccess ? "Registered Successfully" : "Retry Later";
});
}
}
Starting in Xamarin Forms 2.3.4 thy finally introduced Binding to the Picker. You only need to bind to the SelectedItem as shown below.
<?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:PickerDemo"
x:Class="PickerDemo.PickerDemoPage">
<ContentPage.BindingContext>
<local:PickerDemoPageViewModel />
</ContentPage.BindingContext>
<StackLayout Padding="10,20">
<!-- Picker with explicitly set values in XAML -->
<Picker SelectedItem="{Binding SelectedItem}">
<Picker.Items>
<x:String>Item 1</x:String>
<x:String>Item 2</x:String>
</Picker.Items>
</Picker>
<Label Text="{Binding SelectedItem,StringFormat='You Selected: {0}'}" />
<!-- Picker with Dynamically set values -->
<Picker ItemsSource="{Binding SomeCollection}" SelectedItem="{Binding AnotherItem}" />
<Label Text="{Binding AnotherItem,StringFormat='You picked: {0}'}" />
<!-- Date Picker using a custom display format -->
<DatePicker Date="{Binding SomeDate}" Format="MMMM dd, yyyy" />
<Label Text="{Binding SomeDate,StringFormat='{0:dd MMMM, yyyy}'}" />
</StackLayout>
</ContentPage>