Why the Completed event occur on Focus()? - xamarin

I have some problem with Xamarin forms on Completed event which has been triggered when the control have focus (Entry)
Below is the View :
<Entry
x:Name="EntryOrderNumber"
Placeholder="MFGO Number"
Text="{Binding TextOrderEntry}" />
<Entry
x:Name="EntryMachineNumber"
Placeholder="Machine Number"
Text="{Binding TextMachineEntry}" />
And this is where I control the even which is placed at view.cs
public ProductionOrderPage()
{
InitializeComponent();
BindingContext = App.Locator.ProductionOrderPageVM;
EntryOrderNumber.Completed += EntryOrderNumber_Completed;
EntryMachineNumber.Completed += EntryMachineNumber_Completed;
EntryTotalPosition.Completed += EntryTotalPosition_Completed;
}
private void EntryMachineNumber_Completed(object sender, EventArgs e)
{
EntryMachineNumber.Unfocus();
EntryTotalPosition.Focus();
}
private void EntryOrderNumber_Completed(object sender, EventArgs e)
{
EntryOrderNumber.Unfocus();
EntryMachineNumber.Focus();
}
My problem is : While the Entry (Text field) has focused , the Completed event has been triggered which resulting in the focus will go to another field continuously as per set in the Completed event.
the apps being debugged and deployed onto emulator
using MVVMlight
Thanks a lot

using soft keyboard instead of hardware keyboard solve this problems. Weird.
Enabling soft keyboard : Visual Studio Android Emulator Display Keyboard

Related

Complte event for Editor behaving like Unfocussed event

I am trying for saving some value in the editor after clicking on done on the keyboard for that I used the Completed event for the editor but this is calling while tapping on everywhere in the view like the Unfocused event. How to avoid that this?
<controls:CustomEditor Keyboard="Default"
ReturnKeyType="Next"
TextChanged="Comment_Changed"
Completed="OnDoneClicked"
VerticalOptions="StartAndExpand"
HorizontalOptions="FillAndExpand"
Text="{Binding QuestionComment}">
<controls:CustomEditor.FontSize>
<OnIdiom x:TypeArguments="x:Double">
<OnIdiom.Phone>
<OnPlatform x:TypeArguments="x:Double"
iOS="12"
Android="12"
WinPhone="30" />
</OnIdiom.Phone>
<OnIdiom.Tablet>
<OnPlatform x:TypeArguments="x:Double"
iOS="13"
Android="13"
WinPhone="40" />
</OnIdiom.Tablet>
</OnIdiom>
</controls:CustomEditor.FontSize>
</controls:CustomEditor>
CustomEditorClass is as following:
public const string ReturnKeyPropertyName = "ReturnKeyType";
public CustomEditor() { }
public static readonly BindableProperty ReturnKeyTypeProperty = BindableProperty.Create(
propertyName: ReturnKeyPropertyName,
returnType: typeof(ReturnKeyTypes),
declaringType: typeof(CustomEditor),
defaultValue: ReturnKeyTypes.Done );
public ReturnKeyTypes ReturnKeyType
{
get { return (ReturnKeyTypes)GetValue(ReturnKeyTypeProperty); }
set { SetValue(ReturnKeyTypeProperty, value); }
}
public enum ReturnKeyTypes : int
{
Default,
Go,
Google,
Join,
Next,
Route,
Search,
Send,
Yahoo,
Done,
EmergencyCall,
Continue
}
And the event is as following:
private async void OnDoneClicked(object sender, EventArgs e)
{
//some logic
}
Please see the documentation, this is the intended behavior
iOS (Unfocusing the editor or pressing "Done" triggers the event). Android / Windows Phone (Unfocusing the Editor triggers the event)
I've checked some messenger apps (iOS) and they don't have a done button on the keyboard at all, they provide a send button besides the entry control.
Depending on what you try to achieve, I'd ditch the done button on the keyboard altogether and rely on some sort of view on the UI, that is used for that purpose. This is platform dependent behavior anyway, Android does not support it at all, AFAIK. In order to get rid of the done button, you'll have to implement a custom renderer and set the InputAccessoryView of the native control to null (see here)
[assembly: ExportRenderer(typeof(CustomEditor), typeof(CustomEditorRenderer))]
namespace ProjectName.iOS
{
public class CustomEditorRenderer : EditorRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs e)
{
base.OnElementChanged(e);
this.Control.InputAccessoryView = null;
}
}
}
If you really want a button to send, you could create a derivative of Editor with a custom renderer, that creates a InputAccessoryView and raises an SendPressed event on your custom Editor when the InputAccessoryView is pressed. But remember, this is not possible for Android.

Xamarin.Forms 2.4 is loosing hidden entry background on iOS

Recently I have switched to Xamarin.Forms 2.4.0.18342 and now on iOS (10 and 11) the default white background for Entry controls is lost if the control was hidden and then shown.
A simple layout:
<StackLayout Orientation="Vertical" Padding="50">
<Button Text="Hide" Clicked="OnHideClicked"/>
<Button Text="Show" Clicked="OnShowClicked"/>
<Entry x:Name="PasswordEntry" Placeholder="Password" HeightRequest="44" AutomationId="PasswordStyleId" IsVisible="true"/>
</StackLayout>
looks like this:
But if I tap the Hide and then Show buttons with the following code behind:
void OnHideClicked(object sender, System.EventArgs e)
{
PasswordEntry.IsVisible = false;
}
void OnShowClicked(object sender, System.EventArgs e)
{
PasswordEntry.IsVisible = true;
}
the white background is no longer there:
Update 1
It works fine on Xamarin.Forms 2.3.4.270, but breaks on 2.4.0.280.
Update 2
Xamarin Bug: 60261

Why am I getting a Null reference exception for a Slider value changed event that happens each time the page loads

I am new to creating Windows Phone 7 Apps, I have added a slider to my page to which i have customized the look but when i load the page in the emulator before the page even loads I get a "NullReferenceException" I thought this was because I had not initialized the slider so i changed the settings method to
public settings()
{
InitializeComponent();
sldPassLegnth.Value = (double)3;
}
The value changed event simply looks like this:
private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
double d;
d = sldPassLegnth.Value;
}
The xaml for the slider is:
<Slider Style="{StaticResource SliderStyle1}" Margin="24,75,22,352" Name="sldPassLegnth" ValueChanged="Slider_ValueChanged" Background="Black" Foreground="#FF3399FF" Maximum="15" Minimum="3" />
Any insight into this would be great! Thanks in advance.
Try this instead:
private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
double d;
d = e.NewValue;
}

using TabControl in windows phone

I read the article about using a TabControl on Windows Phone application. I can avoid it to fire when it is first load. However, the selectionChanged fired twice when user click the tab. Would someone can help me how to fix it. Thanks in advance.
There is my TabControl:
<cc:TabControl Grid.Row="1" SelectionChanged="tabList_SelectionChanged" x:Name="tabList">
<cc:TabItem Height="80" Header="Events" Foreground="Black"/>
<cc:TabItem Height="80" Header="Details" Foreground="Black"/>
<cc:TabItem Height="80" Header="Notes" Foreground="Black" />
</cc:TabControl>
There is cobe behind:
public partial class Tab : PhoneApplicationPage
{
private bool blnFristLoad=true;
public Tab()
{
InitializeComponent();
tabList.SelectionChanged += new SelectionChangedEventHandler(tabList_SelectionChanged);
}
private void tabList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (blnFristLoad == false)
{
TabItem t = (sender as TabControl).SelectedItem as TabItem;
t.Content = "202020";
}
else blnFristLoad = false;
}
It's very obvious in your code. You are adding SelectionChanged event handler twice. One from your XAML code and the other from the code behind. As you are using += symbol, the eventhandler is added as a seperate instance.
Remove one of those statements.
Please use the Pivot control instead of a TabControl for the WindowsPhone. the Pivot control follows the design guidelines for the phone and looks and feels much better.

How to create a small Windows Phone 7 app with click event

I am a newbie in windows phone app and need to create a small Windows Phone 7 app. The app will perform the following task
App Screen has an image "image1' ,when i press on 'image1' it will shows a second image 'image2'
When I press on image2 it will shows image1 and so on
My XAML code
<Button Click="Button_Click">
<Image Source="resourse/image1.jpg"/>
</Button>
C# code
namespace Test
{
public partial class MainPage : PhoneApplicationPage
{
public MainPage()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
// here will shows the 'image2' and also give click event to turn 'image1'
}
}
}
Please help
I would tackle this by having a couple of images in your XAML:
<Button Click="Button_Click">
<Grid>
<Image x:Name="imageOne" Source="resourse/image1.jpg"/>
<Image x:Name="imageTwo" Source="resourse/image2.jpg"
Visibility="Collapsed"/>
</Grid>
</Button>
The use of x:Name causes visual studio to generate a field for each image. The second image is 'collapsed', i.e. hidden.
You click handler then does the following:
private void Button_Click(object sender, RoutedEventArgs e)
{
if (imageOne.Visisbility == Visibility.Visible)
{
imageOne.Visisbility = Visibility.Collapsed
imageTwo.Visisbility = Visibility.Visible
}
else
{
imageOne.Visisbility = Visibility.Visible
imageTwo.Visisbility = Visibility.Collapsed
}
}
On each click it toggled the visibility of each image.
This is easier than changing the Source of the image, which involves URIs etc...

Resources