Xamarin.Forms.Switch sends Toggled event when value is updated - c#

So I'm still debuting with Xamarin.Forms. So far so good, if I put aside a few pesky bugs I encountered. Here's the new guy. Maybe one of you could tell me if I'm doing something wrong.
Basically, I have a Xamarin.Forms.Switch on my interface, and I'm listening for changes in its state with the Toggled event. The doc says about this event: "Event that is raised when this Switch is toggled by the user."
Unfortunately, when I update the value of the switch programmatically, the event fires.
var mySwitch = new Switch ();
mySwitch.Toggled += (object sender, ToggledEventArgs e) => {
Console.WriteLine ("Switch.Toggled event sent");
};
mySwitch.IsToggled = true;
Any way to prevent the event from firing / to know that it's not the user who triggered it?

The behavior you're experiencing is correct: every time the IsToggled property changes, the switch will fire the Toggled event.
I'm not sure if the Xamarin.Forms documentation has been updated recently, but as of today it says this about the Toggled event:
Event that is raised when this Switch is toggled
Sample Code
Here is sample code that prevents the Toggled event from being handled when the Toggled event is not fired by the user
using System;
using Xamarin.Forms;
namespace SwitchToggle
{
public class SwitchPage : ContentPage
{
public SwitchPage()
{
var mySwitch = new Switch
{
IsToggled = true
};
mySwitch.Toggled += HandleSwitchToggledByUser;
var toggleButton = new Button
{
Text = "Toggle The Switch"
};
toggleButton.Clicked += (sender, e) =>
{
mySwitch.Toggled -= HandleSwitchToggledByUser;
mySwitch.IsToggled = !mySwitch.IsToggled;
mySwitch.Toggled += HandleSwitchToggledByUser;
};
var mainLayout = new RelativeLayout();
Func<RelativeLayout, double> getSwitchWidth = (parent) => parent.Measure(mainLayout.Width, mainLayout.Height).Request.Width;
Func<RelativeLayout, double> getToggleButtonWidth = (parent) => parent.Measure(mainLayout.Width, mainLayout.Height).Request.Width;
mainLayout.Children.Add(mySwitch,
Constraint.RelativeToParent((parent) => parent.Width / 2 - getSwitchWidth(parent) / 2),
Constraint.RelativeToParent((parent) => parent.Height / 2 - mySwitch.Height / 2)
);
mainLayout.Children.Add(toggleButton,
Constraint.RelativeToParent((parent) => parent.Width / 2 - getToggleButtonWidth(parent) / 2),
Constraint.RelativeToView(mySwitch, (parent, view) => view.Y + view.Height + 10)
);
Content = mainLayout;
}
async void HandleSwitchToggledByUser(object sender, ToggledEventArgs e)
{
await DisplayAlert(
"Switch Toggled By User",
"",
"OK"
);
}
}
public class App : Application
{
public App()
{
MainPage = new NavigationPage(new SwitchPage());
}
}
}

Toggled event will every-time called, when you manually change the toggle that time also Toggled event will fire.
The solution is just set
mySwitch.Toggled -= HandleSwitchToggledByUser;
before you manually change toggle value, and write
mySwitch.Toggled += HandleSwitchToggledByUser;
after you manual change of toggle,
Hope, this will help you

Related

BarcodeDetector should only scan when a Button is clicked

I'm trying to write an app with an integrated barcode scanner.
I followed this tutorial:
https://www.c-sharpcorner.com/article/xamarin-android-qr-code-reader-by-mobile-camera/
The scan works fine and very fast (before I used ZXing.Net.Mobile and it is horrible slow).
Now I need some help to integrate that the app only detects one barcode when the user presses a button and not the whole time. Maybe a delay would solve the problem too.
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.ScannerTest);
surfaceView = FindViewById<SurfaceView>(Resource.Id.cameraView);
txtResult = FindViewById<TextView>(Resource.Id.txtResult);
barcodeDetector = new BarcodeDetector.Builder(this)
.SetBarcodeFormats(BarcodeFormat.Code128 | BarcodeFormat.Ean13 | BarcodeFormat.QrCode)
.Build();
cameraSource = new CameraSource
.Builder(this, barcodeDetector)
.SetRequestedPreviewSize(320, 480)
.SetAutoFocusEnabled(true)
.Build();
surfaceView.Click += StartScanning;
surfaceView.Holder.AddCallback(this);
//barcodeDetector.SetProcessor(this);
}
private void StartScanning(object sender, EventArgs e)
{
barcodeDetector.SetProcessor(this);
}
public void ReceiveDetections(Detections detections)
{
SparseArray qrcodes = detections.DetectedItems;
if (qrcodes.Size() != 0)
{
txtResult.Post(() => {
//Vibrator vibrator = (Vibrator)GetSystemService(Context.VibratorService);
//vibrator.Vibrate(1000);
txtResult.Text = ((Barcode)qrcodes.ValueAt(0)).RawValue;
});
}
}
At the moment the user presses the SurfaceView and the scanner starts and never stops.
Is it possible, that it just scans one after pressing the "button"?
r3d007
1 Uncomment this line in OnCreate method
barcodeDetector.SetProcessor(this);
2 Remove or comment this line from SurfaceCreated and OnRequestPermissionsResult methods
cameraSource.Start(surfaceView.Holder);
3 Your StartScanning method should call the Start
private void StartScanning(object sender, EventArgs e)
{
cameraSource.Start(surfaceView.Holder);
}
4 Once you read and validate a code, stop the scanner
public void ReceiveDetections(Detections detections)
{
SparseArray qrcodes = detections.DetectedItems;
if (qrcodes.Size() != 0)
{
txtResult.Post(() => {
//Vibrator vibrator = (Vibrator)GetSystemService(Context.VibratorService);
//vibrator.Vibrate(1000);
txtResult.Text = ((Barcode)qrcodes.ValueAt(0)).RawValue;
});
using (var h = new Handler (Looper.MainLooper))
h.Post (() => {
cameraSource.Stop();
});
}
}
To prevent crashes, consider also to hide or disable the button until you get the camera permissions, and when the scanner is already started.
You need add this after scan process triggered. "-" operator must be added to prevent non-stop working.You plugged-in event in this line
//adds the handler
surfaceView.Click += StartScanning;
after that you need this.
// removes the handler
surfaceView.Click -= StartScanning;
Also look here

How to programmatically raise SizeChanged event

I'm trying to execute code in a SizeChangedEventHandler but the following is not working:
[TestMethod]
public void TestSizeChanged()
{
var panel = new System.Windows.Controls.StackPanel();
bool handled = false;
panel.SizeChanged += (o, e) =>
{
handled = true; // how to get this to be executed
};
panel.Width = 100; // naive attempt to change size!
Assert.IsTrue(handled);
}
I originally tried to use the RaiseEvent method but I was not been able to supply it with the correct xxxEventArgs type, due to not knowing the constructor arguments and the object browser is not helping:
panel.RaiseEvent(new System.Windows.SizeChangedEventArgs()) // does not compile
Obviously, the above test serves no purpose but I'm after correct way of getting the event to fire in a unit-tested environment.
It's very strange that the SizeChanged event doesn't fire with your code, it appears to be correct. Maybe the StackPanel doesn't exists in the visual tree because it's not really shown on the screen, so the event is never fired.
Try to show a real window with a StackPanel on the screen, and programmatically change his width or height.
[TestMethod]
public void TestSizeChanged()
{
Window wnd = new Window();
wnd.Content = new System.Windows.Controls.StackPanel();
bool handled = false;
wnd.SizeChanged += (o, e) =>
{
handled = true; // how to get this to be executed
};
wnd.Show();
wnd.Width = 100; // naive attempt to change size!
Assert.IsTrue(handled);
}
You can't use the RaiseEvent method, because SizeChanged is not a RoutedEvent.
Using the below reflection you can succeed:
//panel =>System.Windows.Controls.Panel instance..
SizeChangedInfo sifo = new SizeChangedInfo(panel, new Size(0, 0), true, true);
SizeChangedEventArgs ea = typeof(System.Windows.SizeChangedEventArgs).GetConstructors(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).FirstOrDefault().Invoke(new object[] {(panel as FrameworkElement),sifo }) as SizeChangedEventArgs;
ea.RoutedEvent = Panel.SizeChangedEvent;
panel.RaiseEvent(ea);

How to initialise the UIElements before click to checkButton (by just launching program)?

I have situation where i disable or enable UI elements on checking or unchecking by calling a function ManageDisablity(ParentClass,value,cbox) like this:
cbox.Checked += (o, e) =>
{
manageDisable(parentClass,0, cbox);
};
chb.Unchecked += (o, e) =>
{
manageDisable(parentClass,1, cbox);
};
It works fine.It disables and enables well the UIElements to be disabled or enabled.
Why it do not work and how to make it work?
Try The Following :
When Creating :
childGrid.Loaded += DoYourThing;
in DoYourThing :
DoYourThing()
{
foreach(var chb in comboboxs)
{
if (param.Component.Attributes[0].Value == 1)
chb.IsChecked = true;
}
childGrid.Loaded -= DoYourThing;
}

CustomMessageBox from Windows Phone 8 Toolkit throw NullPointerException

Code for creating the CustomMessageBox:
CustomMessageBox is a property, and not a reference to the C# Class in the Toolkit.
CustomMessageBox.Dismissed += (dismissSender, dismissedEvent) =>
{
switch (dismissedEvent.Result)
{
case CustomMessageBoxResult.LeftButton:
PlaceCall(clickedFavorite.Name, clickedFavorite.PhoneNo);
break;
case CustomMessageBoxResult.RightButton:
HERE ---> SendText(clickedFavorite.PhoneNo);
break;
}
};
Code for SendText() method:
private void SendText(String phoneNo)
{
var smsTask = new SmsComposeTask
{
To = phoneNo
};
smsTask.Show();
}
Thing is when the SmsComposeTask has started, the Phone navigates to the SMS application, which is correct.
If the user then decides to go back, with the Hardware Back Button, the SMS application closes and the phone shows my app again - but immediately closes, caused by a NullPointerException:
at Microsoft.Phone.Controls.CustomMessageBox.ClosePopup(Boolean restoreOriginalValues)
at Microsoft.Phone.Controls.CustomMessageBox.<>c__DisplayClass4.<Dismiss>b__1(Object s, EventArgs e)
at Microsoft.Phone.Controls.Transition.OnCompleted(Object sender, EventArgs e)
at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)
I have also tried to override the OnBackKeyPress event, like this:
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
if (CustomMessageBox != null && CustomMessageBox.IsEnabled)
{
e.Cancel = true;
}
else
{
base.OnBackKeyPress(e);
}
}
Does anyone know what to do?
I have found a solution to my own problem. Instead of using the faulty CustomMessageBox, I found Coding4Fun Windows Phone Toolkit which provides a by far, more stable message box called MessagePrompt - here's how to use it.
Create buttons
var smsButton = new Button { Content = "SMS" };
smsButton.Click += (o, args) =>
{
// do something
};
var buttonList = new List<Button>
{
smsButton
};
Create the actual message prompt
var msgPrompt = new MessagePrompt
{
Title = "Message Prompt Title",
Body = new TextBlock { Text = "Text for the Body", FontSize = 25, TextWrapping = TextWrapping.Wrap },
ActionPopUpButtons = buttonList
};
Show it
msgPrompt.Show()
No bullocks
The good thing, which I have experienced with this MessagePrompt is that you are not bound to two static Left and Right buttons like with CustomMessageBox.
And if you want, you can set the Body property to a whole new XAML page, which makes this control flexible.
Reference: Coding4Fun WP7 Message Prompt in depth
Doesn't this problem has something to do with Windows Phone Application lifecycle. As can be found here, figure 6. When activiting another program when your program is active you should save all application data so when a reactivating event ,such as navigating with your back button back to your application, starts your program again you can load the user's data again.
I'm not sure what's happening, but you can just delay the SMS task to avoid the issue:
CustomMessageBox.Dismissed += (dismissSender, dismissedEvent) =>
{
switch (dismissedEvent.Result)
{
case CustomMessageBoxResult.LeftButton:
PlaceCall(clickedFavorite.Name, clickedFavorite.PhoneNo);
break;
case CustomMessageBoxResult.RightButton:
Dispatcher.BeginInvoke(new Action(() => SendText(clickedFavorite.PhoneNo)));
break;
}
};
My 0.02$: this is a bug in the CustomMessageBox. They're keeping lots of singletons alive there and a good timing bug doesn't do that a world of good. Agreed with KooKiz that you can't work around with that without either fixing CustomMessageBox or waiting until the CustomMessageBox finishes its thing. From my ad-hoc testing it requires anywhere between 2-6 Dispatcher.BeginInvoke() until those actions finish. Instead, maybe consider using DispatcherTimer and wait 256MS which should be enough time.
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
var msgBox = new CustomMessageBox()
{
Caption = "foo",
Message = "bar",
LeftButtonContent = "baz",
RightButtonContent = "goo",
IsFullScreen = false,
};
msgBox.Dismissed += (s, args) =>
{
DispatcherTimerHelper.InvokeReallySoon(() =>
{
new SmsComposeTask()
{
Body = "foo",
To = "bar"
}.Show();
});
};
msgBox.Show();
}
public static class DispatcherTimerHelper
{
public static void InvokeReallySoon(Action action)
{
var t = new DispatcherTimer() {Interval = TimeSpan.FromMilliseconds(256)};
t.Tick += (s, args) => action();
t.Start();
}
}
The problem just happen in wp8.
I use the same code in wp7, nothing wrong happens.
Use Code4fun messagebox is a good choice,but is Button Click handler you need to call
MessagePrompt.Hide();
to close the MessagePrompt.
used a boolean on the dismissed event to define which button had been pressed. I then implemented the code I would of implemented in the dismissed event in the Unloaded event instead. This seemed to solve the issue.
i.e
messageBox.Dismissed += (s1, e1) =>
{
switch (e1.Result)
{
case CustomMessageBoxResult.LeftButton:
{
delete = true ;
}
break;
case CustomMessageBoxResult.RightButton:
break;
case CustomMessageBoxResult.None:
break;
default:
break;
}
};
messageBox.Unloaded += (s1, e1) =>
{
if (delete)
DeleteWorkout();
};
This is a known bug.
It was fixed in the latest version.
Remove the reference and install the toolkit again.

Why is my button's 'Validating' event handler never called?

In the following mini-app, I am wondering why the BtnOk_Validating event handler is never called. I expected that clicking the Ok button would call the event handler.
The real dialog has many more controls, each that have a validating event handler. My plan was to use the Ok button validating event handler to call each of the other event handlers before allowing the dialog to close.
If it's not obvious, I'm quite the novice when it comes to Forms development.
using System.ComponentModel;
using System.Windows.Forms;
namespace ConsoleApp
{
class Program
{
static void Main( string[] args )
{
Dialog dialog = new Dialog();
dialog.ShowDialog();
}
}
public class Dialog : Form
{
Button m_BtnOk;
Button m_BtnCancel;
public Dialog()
{
m_BtnOk = new System.Windows.Forms.Button();
m_BtnCancel = new System.Windows.Forms.Button();
m_BtnOk.CausesValidation = true;
m_BtnOk.DialogResult = DialogResult.OK;
m_BtnOk.Text = "Ok";
m_BtnOk.Location = new System.Drawing.Point( 0, 0 );
m_BtnOk.Size = new System.Drawing.Size( 70, 23 );
m_BtnOk.Validating += new CancelEventHandler( BtnOk_Validating );
m_BtnCancel.CausesValidation = false;
m_BtnCancel.DialogResult = DialogResult.Cancel;
m_BtnCancel.Text = "Cancel";
m_BtnCancel.Location = new System.Drawing.Point( 0, 30 );
m_BtnCancel.Size = new System.Drawing.Size( 70, 23 );
Controls.Add( this.m_BtnOk );
Controls.Add( this.m_BtnCancel );
}
private void BtnOk_Validating( object sender, CancelEventArgs e )
{
System.Diagnostics.Debug.Assert( false ); // we never get here
}
}
}
Edit: Please see my follow-up question for a more complete example that works (well mostly).
Its because the button will never loose focus with it being the only control. If you add a TextBox or something that can take the focus of the button, then you will see it fire.
From MSDN
When you change the focus by using the keyboard (TAB, SHIFT+TAB, and so on), by calling the Select or SelectNextControl methods, or by setting the ContainerControl.ActiveControl property to the current form, focus events occur in the following order:
Enter
GotFocus
Leave
Validating
Validated
LostFocus
When you change the focus by using the mouse or by calling the Focus method, focus events occur in the following order:
Enter
GotFocus
LostFocus
Leave
Validating
Validated
If the CausesValidation property is set to false, the Validating and Validated events are suppressed.
Update: Like Hans mentions, you'll need to extract the validating you do in each of the Validating events for all the other controls into separate functions. Then you can create a ValidateAll function to check all values. If the function returns false, then you dont close the Form. If it returns true, you call this.Close(). So it might look like this:
// pseudo code
textbox1.Validating += ValidateTx1();
textbox2.Validating += ValidateTx2();
btnOk.Click += OkBtnClicked();
private void OkBtnClicked(...)
{
if(ValidateAll())
{
this.Close();
}
}
private bool ValidateTx1(...)
{
DoTx1Validation();
}
private bool ValidateTx2(...)
{
DoTx2Validation();
}
private bool ValidateAll()
{
bool is_valid = DoTx1Validation();
return (is_valid && DoTx2Validation());
}

Categories

Resources