i have a page where user can swap labels. So I have a ExtendedWebView CZ and ExtendedWebView EN once user presses button i need them to swap. I have decided to swap these ExtendedWebView in the bac of my page as that seemed much easier however both these commes with button that each fires different pop up. So if i swap ExtendedWebView CZ i need the button that fires CZ pop up to be swapped also.
This is how i am changing my ExtendedWebViews
bool isSwap = false;
private void ChangePosition(object sender, EventArgs e)
{
isSwap = !isSwap;
czView.RemoveBinding(ExtendedWebView.SourceProperty);
enView.RemoveBinding(ExtendedWebView.SourceProperty);
if (isSwap)
{
btnChangePositions.Text = "EN/CZ";
czView.SetBinding(ExtendedWebView.SourceProperty,("EN"));
enView.SetBinding(ExtendedWebView.SourceProperty, ("CZ"));
}
else
{
btnChangePositions.Text = "CZ/EN";
czView.SetBinding(ExtendedWebView.SourceProperty, ("CZ"));
enView.SetBinding(ExtendedWebView.SourceProperty, ("EN"));
}
}
Xaml
<Label
x:Name="makeLargerEN"
FontFamily="{StaticResource IconsFontFamily}"
HorizontalTextAlignment="End"
Text="{Binding MakeLargerEN}"
TextColor="{DynamicResource AccentColor}"
VerticalTextAlignment="End">
<Label.GestureRecognizers>
<TapGestureRecognizer Command="{Binding MakeWebViewLargeEnCommand}" />
</Label>
And this is my pop up in the vIew Model
private async void MakeWebViewLargeEN()
{
var popup = new WordAndPhrasePopup(htmlSourceName.Html);
await PopupNavigation.Instance.PushAsync(popup);
}
I have tried in back of the content page
makeLargerEN.SetBinding(Label.TextProperty, "MakeLargerEN");
Just to swap labels but then the right command doesnt get fired
I have tried this, but gesture doesnt have Command or CommandProperty
makeLargerCZ.SetBinding(GestureRecognizer.Command, new Binding("MakeLargerEN"));
I am doing it in the back of COntent page because i am setting up there another swap that goes along with this one.
You could remove and add GestureRecognize in code behind .
Remove
makeLargerEN.GestureRecognizers.Clear();
Add
makeLargerEN.GestureRecognizers.Add(new TapGestureRecognizer
{
NumberOfTapsRequired = 1,
Command = new Command(() =>
{
//...
}),
});
Related
I really don't know how to get control dimensions in Xamarin Forms. I thought it would be as easy as:
View control = GetControlSomehow();
var bounds = control.Bounds;
But View.Bounds returns different things depending on wether the control is inside a grid or not; or wether the main layout is AbsoluteLayout or some other one.
For example if I do something like:
<AbsoluteLayout>
<Button AbsoluteLayout.LayoutBounds="200,200" x:Name="btn" Text="Btn"/>
</AbsoluteLayout>
then I am not able to read actual height or width of that button. Although MSDN says that Height & Width properties should return these values.
I tried even:
var width = ctrl.Width;
or
var width = ctrl.GetValue(WidthProperty);
But really non of these worked. I always get -1
So how can I get the control dimensions that could be reliable?
Complete example showing how to pass button itself, using data binding.
AbsoluteLayoutPage.xaml:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="TestBugs.AbsoluteLayoutPage">
<ContentPage.Content>
<AbsoluteLayout>
<Button AbsoluteLayout.LayoutBounds="200,200" x:Name="btn" Text="Btn"
Command="{Binding ButtonCommand}" CommandParameter="{x:Reference btn}"/>
</AbsoluteLayout>
</ContentPage.Content>
</ContentPage>
AbsoluteLayoutPage.xaml.cs:
using System.Diagnostics;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace TestBugs
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class AbsoluteLayoutPage : ContentPage
{
public AbsoluteLayoutPage()
{
ButtonCommand = new Command(ButtonClick);
InitializeComponent();
BindingContext = this;
}
private void ButtonClick(object ob)
{
var button = ob as View;
var bounds = btn.Bounds;
var width = btn.Width;
var height = btn.Height;
Debug.WriteLine($"--- ButtonClick {bounds}, ({width},{height})---");
}
public Command ButtonCommand { get; set; }
}
}
When click button, in VS Output pane:
[0:] --- ButtonClick {X=200 Y=200 Width=88 Height=48}, (88,48)---
this works for me
<Button Text="Click" Clicked="Clicked" />
public void Clicked(object sender, EventArgs a)
{
var btn = (Button)sender;
var w = btn.Width;
var h = btn.Height;
DisplayAlert("Dimesions", $"{w}x{h}","OK");
}
Sorry guys, my bad.
I was testing it on two pages - the real one and the test one. The test page is built in a different way and I confirm that the problem was in time when I retrieved these values. Control wasn't painted yet.
So I am really sorry to bother you and thank you all for investing your time into this.
I am trying to create a Trivia App. I have some very basic Xamarin Form code implementing a Radio Button. The buttons work fine (ie a selection is registered and processed properly). The issue is that after the first question is answered, the radio button selection persists when the second question is displayed. Is there a way to reset the radio buttons between questions so that they are all not selected?
Here is the basic xaml code and the code behind:
Xaml:
<StackLayout Margin="20">
<RadioButton x:Name="Option0" CheckedChanged="RadioButton_CheckedChanged" IsTabStop="False" IsChecked="False"></RadioButton>
<RadioButton x:Name="Option1" CheckedChanged="RadioButton_CheckedChanged" IsTabStop="False" IsChecked="False"></RadioButton>
<RadioButton x:Name="Option2" CheckedChanged="RadioButton_CheckedChanged" IsTabStop="False" IsChecked="False"></RadioButton>
<RadioButton x:Name="Option3" CheckedChanged="RadioButton_CheckedChanged" IsTabStop="False" IsChecked="False"></RadioButton>
</StackLayout>
Code behind: (Note: getQuestion() has the logic to display the next question so I have not included it here and I suspect something needs to be done at // do something to reset the radio button but I am not sure what to do)
void RadioButton_CheckedChanged(System.Object sender, Xamarin.Forms.CheckedChangedEventArgs e) // sender is of class System.Object and must be converted in the proper type
{
Debug.WriteLine("\nRadio Button Clicked");
Debug.WriteLine("sender: " + sender);
Debug.WriteLine("e: " + e);
var s = sender;
var radioEvent = e;
// converts sender to Radio Button type
var radioButton = (RadioButton)sender;
// Once in Radio Button type now you can see and use content
string contentString = radioButton.Content.ToString();
Debug.WriteLine("contentString: " + contentString);
if (radioButton.IsChecked == true)
{
if (contentString == Movie.Text)
{
DisplayAlert("Correct", "You're Good!", "OK");
Debug.WriteLine("Correct Answer");
ans_correct = ans_correct + 1;
// do something to reset the radio button
getQuestion();
}
else
{
DisplayAlert("Whoops", "Wrong Agian!", "OK");
Debug.WriteLine("Wrong Answer");
ans_wrong = ans_wrong + 1;
}
}
}
Never mind - I guess I just had to post the question to find the answer.
It turns out all I needed to do was put
radioButton.IsChecked = false;
where I had my comment
// do something to reset the radio button
How to create a calendar type date picker in Xamarin forms UWP app.
So far I have tried this:
<Image Grid.Row="3" Grid.Column="1" Source="Images/datePicker.png" WidthRequest="20" HeightRequest="20" Margin="-40,0,40,0" HorizontalOptions="End">
<Image.GestureRecognizers>
<TapGestureRecognizer Command="{Binding OnSelectDOBCommand}"/>
</Image.GestureRecognizers>
</Image>
public void OnSelectDOB(object obj)
{
dateOfBuildDatePicker = UserDialogs.Instance.DatePrompt(new DatePromptConfig { MaximumDate = DateTime.Today, OnAction = (result) => SetDateOfBirth(result), IsCancellable = true });
}
But this displays the date picker control on top left corner of the screen.
Is there a way to customize it so that it should display the date picker right next to the field where I click or is there any other control which can help me achieve this functionality.(Below image is the behavior that I am expecting)
Any help is appreciated]1
If you want to consume a special control on UWP platform, you can try custom renderer.
Firstly, create a custom control:
public class CustomDatePicker : DatePicker
{
}
Then make a custom renderer on UWP:
[assembly: ExportRenderer(typeof(CustomDatePicker), typeof(CustomDatePickerRenderer))]
namespace Demo.UWP
{
public class CustomDatePickerRenderer : ViewRenderer<DatePicker, Windows.UI.Xaml.Controls.CalendarDatePicker>
{
protected override void OnElementChanged(ElementChangedEventArgs<DatePicker> e)
{
base.OnElementChanged(e);
if (Control == null)
{
Windows.UI.Xaml.Controls.CalendarDatePicker datePicker = new Windows.UI.Xaml.Controls.CalendarDatePicker();
SetNativeControl(datePicker);
}
}
}
}
At last, use it on your forms project:
<StackLayout>
<local:CustomDatePicker HorizontalOptions="Start"/>
</StackLayout>
Alternatively, you can try native view: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/platform/native-views/
date value is always set to the current date even you selected a data. how to resolve this?
Changing the date value requires working with the date change events. See my working solution here:
https://stackoverflow.com/a/60449912/9523062
Iam using syncfusion SFListview in my xamarin forms app. I implemented multiselect of listview cell from https://help.syncfusion.com/xamarin/sflistview/selection?cs-save-lang=1&cs-lang=xaml.It works fine.But the problem iam facing is everytime we need to hold the itemcell for selection. Is it possible for multiselect that hold only for first cell and tap for all other cell?
Is it possible for multiselect that hold only for first cell and tap for all other cell?
Sure can do that.If you want multiselect items,I guess that next steps will do some tasks about multiselect items.The picture below may look like the one you want.
You can look at the content of this chapter in the share link, and the sample code it provides.
Solution One:(Generally acceptable)
If the project doesn't mind adding a control button outside, then this will be the quickest and easiest way.That is to add a ToolbarItems in the NavigationPage, use it to control whether you can click multiple selections without jumping to the next page.
Add ToolbarItems :
<ContentPage.ToolbarItems>
<ToolbarItem x:Name="ToolbarItemsButton" Text="MultipleSelect" Clicked="Edit_Clicked"/>
</ContentPage.ToolbarItems>
<sync:SfListView x:Name="listView"
SelectionGesture="Hold"
SelectionMode="Multiple"
ItemTapped="ListView_ItemTapped"
SelectionBackgroundColor="Transparent"
IsStickyHeader="True" ItemSize="70">
...
In ContentPage ,add Flag to judge SelectionMode of ListView.
int flag = 0;
private void Edit_Clicked(object sender, EventArgs e)
{
if(0 == flag)
{
listView.SelectionGesture = TouchGesture.Tap;
ToolbarItemsButton.Text = "Done";
flag = 1;
}
else
{
ToolbarItemsButton.Text = "MultipleSelect";
listView.SelectionGesture = TouchGesture.Hold;
flag = 0;
}
}
Can judge when you can switch to the next page.
private void ListView_ItemTapped(object sender, Syncfusion.ListView.XForms.ItemTappedEventArgs e)
{
if(0 == flag)
{
Navigation.PushAsync(new ContentPage());
}
}
Solution Two:(Recommended)
SfListView has a method is ItemHolding.Not using another button also can exchange the SelectionMode.
Xaml code different is adding this method of SfListView.
<sync:SfListView x:Name="listView"
SelectionGesture="Hold"
SelectionMode="Multiple"
ItemTapped="ListView_ItemTapped"
SelectionBackgroundColor="Transparent"
ItemHolding="ListView_ItemHolding" // ItemHolding
IsStickyHeader="True" ItemSize="70">
When OnHolding can do something here:
private void ListView_ItemHolding(object sender, ItemHoldingEventArgs e)
{
if (0 == flag)
{
listView.SelectionGesture = TouchGesture.Tap;
ToolbarItemsButton.Text = "Done";
flag = 1;
}
else
{
listView.SelectionGesture = TouchGesture.Hold;
ToolbarItemsButton.Text = "MultipleSelect";
flag = 0;
}
}
Judge when you can switch to the next page.
private void ListView_ItemTapped(object sender, Syncfusion.ListView.XForms.ItemTappedEventArgs e)
{
if(0 == flag)
{
Navigation.PushAsync(new ContentPage());
}
}
Solution Three:(Not recommended here)
Generally, for the multiple selection of the cell of the listview, we will process the template of the custom cell, such as adding a button in the template. When clicking, the item can be marked as selected, and the UI of the item can also be customized as The style when selected.
Truncate the label text makes it one line. App shows description, it needs to be displayed in 2-3 lines but Xamarin "LineBreakMode=TailTruncation" truncates it and restricts it to one line.
Is there any way to truncate label text and show in multi-lines. If text doesn't fit into n number of lines, then it should be truncated.
<Label LineBreakMode="TailTruncation" FontSize = "20" Text="Multi line Text" />
Thanks.
Since Xamarin.Forms 3.3 a new property was introduced for this feature. It's called MaxLines.
Here is an example in C#:
var yourLabel = new Label
{
LineBreakMode = LineBreakMode.TailTruncation,
Text = "Your text",
MaxLines = 2
};
Here is an example in XAML:
<Label
LineBreakMode="TailTruncation"
Text="Your text"
MaxLines="2" />
See https://learn.microsoft.com/de-de/dotnet/api/xamarin.forms.label.maxlines?view=xamarin-forms for more information.
I have implemented custom renderer to handle this.
http://depblog.weblogs.us/2016/06/27/xamarin-forms-multi-line-label-custom-renderer-gotcha/
//Droid
public class MultiLineLabelRenderer : LabelRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs e)
{
base.OnElementChanged(e);
if (Control != null)
{
Control.LayoutChange += (s, args) =>
{
Control.Ellipsize = TextUtils.TruncateAt.End;
Control.SetMaxLines(2);
}
};
}
}