This question already has answers here:
Is there an easy way to detect shake motions on Windows Phone 8?
(3 answers)
Closed 8 years ago.
I have created a windows phone app that locates a users location when a button is pressed but I want to do away with the button and make this function occur when the phone is shaken! Below is the code that I have created so far, when the application loads it will call a function called Locate_Me which initializes the Accelerometer.
private async void Locate_Me()
{
if (accelerometer == null)
{
// Instantiate the Accelerometer.
accelerometer = new Accelerometer();
accelerometer.TimeBetweenUpdates = TimeSpan.FromMilliseconds(20);
accelerometer.CurrentValueChanged +=
new EventHandler<SensorReadingEventArgs<AccelerometerReading>>(accelerometer_CurrentValueChanged);
}
try
{
statusTextBlock.Text = "starting accelerometer.";
accelerometer.Start();
}
catch (InvalidOperationException ex)
{
statusTextBlock.Text = "unable to start accelerometer.";
}
}
So how would I go about making the onShaken function?
First step: Download ShakeGestures library from microsoft site here. Add ShakeGetures.dll to your project.
Now it's a piece of cake for you to detect shake gestures. Below is the code you can use:
//constructor of page register event handler for shake
public Page1()
{
InitializeComponent();
// register shake event
ShakeGesturesHelper.Instance.ShakeGesture +=new
EventHandler<ShakeGestureEventArgs>(Instance_ShakeGesture);
// optional, set parameters
ShakeGesturesHelper.Instance.MinimumRequiredMovesForShake = 2;
// start shake detection
ShakeGesturesHelper.Instance.Active = true;
}
private void Instance_ShakeGesture(object sender, ShakeGestureEventArgs e)
{
//call your method
}
This is the minimal code you would require. Worked for me.
Related
This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 3 years ago.
The instruction this.Frame.Navigate(typeof(RegionPage)); on my mainpage doesn't work. It generates an exception:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
So I tried putting it in some function after the mainpage and all goes fine.
My objective: I want to make a control such that, if is it the first time that the user opens the app, it will display a new page with tutorial.
Question: how can I work around that problem?
public MainPage()
{
this.InitializeComponent();
Windows.UI.Core.SystemNavigationManager.GetForCurrentView().BackRequested += App_BackRequested;
this.NavigationCacheMode = Windows.UI.Xaml.Navigation.NavigationCacheMode.Enabled;
TextBoxRicerca.Visibility = Visibility.Collapsed;
Mappe.Loaded += Mappe_Loaded;
Regione.RegNome = "";
this.Frame.Navigate(typeof(RegionPage));
}
Due to your app is preparing some components for launching the so you need to give some time to your app to load components.
So you need to give some delay like this--
using System.Threading.Tasks;
public MainPage()
{
this.InitializeComponent();
Loaded += async (s, e) =>
{
await Task.Delay(100);
Frame.Navigate(typeof(RegionPage));
};
}
you can adjust delay accordingly.
Demo-
Alternate Way-
And Full Solution for first time launch of your so it should show some specific page or tutorial page, you can edit your App.xaml.cs in OnLaunched event
using Windows.Storage;
if (e.PrelaunchActivated == false)
{
if (rootFrame.Content == null)
{
IPropertySet roamingProperties = ApplicationData.Current.RoamingSettings.Values;
if (roamingProperties.ContainsKey("FirstTimePage"))
{
// regular visit
rootFrame.Navigate(typeof(MainPage), e.Arguments);
}
else
{
// first time visit
rootFrame.Navigate(typeof(RegionPage), e.Arguments);
roamingProperties["FirstTimePage"] = bool.TrueString;
}
}
// Ensure the current window is active
Window.Current.Activate();
}
I don't like to use the delay, and editing App.xaml.cs in OnLaunched event was too difficult.
So I made a mix and put a "Loaded += Loading;" on the main and created.
public MainPage()
{
this.InitializeComponent();
Windows.UI.Core.SystemNavigationManager.GetForCurrentView().BackRequested += App_BackRequested;
this.NavigationCacheMode = Windows.UI.Xaml.Navigation.NavigationCacheMode.Enabled;
TextBoxRicerca.Visibility = Visibility.Collapsed;
Mappe.Loaded += Mappe_Loaded;
Regione.RegNome = "";
**Loaded += Loading;**
//this.Frame.Navigate(typeof(RegionPage));
}
I created the function:
private void Loading(object sender, RoutedEventArgs e)
{
this.Frame.Navigate(typeof(RegionPage));
}
It's only giving me a message that I should add a "new" somewhere don't know where and don't know why, but works.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am developing a project in C#, for which I want to login/authenticate a user using their fingerprint.
I bought a ZK4500 Fingerprint scanner and got its SDK from http://www.zkteco.com/product/ZK4500_238.html. The SDK is in C++.
So How can I integrate this SDK with my C# project to perform the desired functionality?
You need to add reference to ZKFPEngXControl that will appear under COM Type Libraries. After that you can use the ZKFPEngX Class to do whatever you require.
using ZKFPEngXControl;
and then
ZKFPEngX fp = new ZKFPEngX();
fp.SensorIndex = 0;
fp.InitEngine(); // Do validation as well as it returns an integer (0 for success, else error code 1-3)
//subscribe to event for getting when user places his/her finger
fp.OnImageReceived += new IZKFPEngXEvents_OnImageReceivedEventHandler(fp_OnImageReceived);
You can write your own method fp_OnImageReceived to handle the event. for example you can write this in that method;
object imgdata = new object();
bool b = fp.GetFingerImage(ref imgdata);
Where imgdata is an array of bytes.You can also use other methods in ZKFPEngX, to achieve your goals. Remember to close the engine when form closes.
fp.EndEngine();
You can store a fingerprint under OnEnroll(bool ActionResult, object ATemplate) Event.This event will be called when BeginEnroll() has been executed.
//Add an event handler on OnEnroll Event
ZKFPEngX x = new ZKFPEngX();
x.OnEnroll += X_OnEnroll;
private void X_OnEnroll(bool ActionResult, object ATemplate)
{
if (ActionResult)
{
if (x.LastQuality >= 80) //to ensure the fingerprint quality
{
string regTemplate = x.GetTemplateAsStringEx("9");
File.WriteAllText(Application.StartupPath + "\\fingerprint.txt", regTemplate);
}
else
{
//Quality is too low
}
}
else
{
//Register Failed
}
}
You can try to verify the fingerprints under OnCapture(bool ActionResult, object ATemplate)event. This event will be called when a finger is put on the scanner.
Add an event handler on OnCapture Event:
x.OnCapture += X_OnCapture;
Verify the fingerprints when the event has been called (a finger is put on the scanner):
private void X_OnCapture(bool ActionResult, object ATemplate)
{
if (ActionResult) //if fingerprint is captured successfully
{
bool ARegFeatureChanged = true;
string regTemplate = File.ReadAllText(Application.StartupPath + "\\fingerprint.txt");
string verTemplate = x.GetTemplateAsString();
bool result = x.VerFingerFromStr(regTemplate , verTemplate, false, ARegFeatureChanged);
if (result)
{
//matched
}
else
{
//not matched
}
}
else
{
//failed to capture a valid fingerprint
}
}
I try to detect a shake on my phone with the Shaken event from Accelerometer object. The accelerometer object is not null but when I shake the phone, it never go in the _accelerometer_Shaken event.
public int shakeCount = 0;
Accelerometer _accelerometer = Accelerometer.GetDefault();
public MainPage()
{
this.InitializeComponent();
if (_accelerometer != null)
{
_accelerometer.Shaken += _accelerometer_Shaken;
}
}
async private void _accelerometer_Shaken(Accelerometer sender, AccelerometerShakenEventArgs args)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
shakeCount++;
tbInfo.Text = shakeCount.ToString();
});
}
I don't understand why
This feature is not supported yet.
This is an extract from the official sample code
https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/Accelerometer
Accelerometer Shake Events
When you choose the Enable button for the Shake Events option, the app
displays the cumulative number of shake events each time an event
occurs. (The app first increments the event count and then renders the
most recent value.)
Note that shake events are not supported in Windows 10 build 10240, so
the Shaken event will never be raised, but the sample demonstrates how
to handle the event when support for shake is added.
I made a test under Windows 10 10586 and it still does not work.
So I've built in to a solution a very basic TAPI function that makes a phone call to a given number. I'm not particularly strong with TAPI code, and was wondering if there is any way to record a phone call length using the c# functions? My initial thought was if there is a call started and call ended watch event, I could start and end a timer respectively, but documentation seems thin on the ice.
Does anybody know if this is possible, and if so, how?
Thanks,
Ryan
Current Code:
Global Variables:
TAPI3Lib.ITAddress line;
Form initialisation code:
public Outbound()
{
InitializeComponent();
#region TAPI Initialize
TAPIClass tapi = new TAPIClass();
tapi.Initialize();
foreach (TAPI3Lib.ITAddress ad in (tapi.Addresses as TAPI3Lib.ITCollection))
{
line = ad;
}
tapi.EventFilter = (int)(TAPI_EVENT.TE_CALLNOTIFICATION | //All events you could need, probably
TAPI3Lib.TAPI_EVENT.TE_CALLINFOCHANGE |
TAPI3Lib.TAPI_EVENT.TE_DIGITEVENT |
TAPI3Lib.TAPI_EVENT.TE_PHONEEVENT |
TAPI3Lib.TAPI_EVENT.TE_CALLSTATE |
TAPI3Lib.TAPI_EVENT.TE_GENERATEEVENT |
TAPI3Lib.TAPI_EVENT.TE_GATHERDIGITS |
TAPI3Lib.TAPI_EVENT.TE_REQUEST);
tapi.ITTAPIEventNotification_Event_Event += new TAPI3Lib.ITTAPIEventNotification_EventEventHandler(tapi_ITTAPIEventNotification_Event_Event);
#endregion
}
TAPI Functions:
private void btncall_Click(object sender, EventArgs e)
{
if (line == null) return;
TAPI3Lib.ITBasicCallControl bc = line.CreateCall(txttelephone.Text, TAPI3Lib.TapiConstants.LINEADDRESSTYPE_PHONENUMBER, TAPI3Lib.TapiConstants.TAPIMEDIATYPE_AUDIO);
bc.Connect(false);
}
private void tapi_ITTAPIEventNotification_Event_Event(TAPI_EVENT TapiEvent, object pEvent)
{
try
{
switch (TapiEvent)
{
case TAPI3Lib.TAPI_EVENT.TE_CALLNOTIFICATION:
TAPI3Lib.ITCallNotificationEvent cn = pEvent as TAPI3Lib.ITCallNotificationEvent;
if (cn.Call.CallState == TAPI3Lib.CALL_STATE.CS_OFFERING)
{
string c = cn.Call.get_CallInfoString(TAPI3Lib.CALLINFO_STRING.CIS_CALLERIDNUMBER);
}
break;
}
}
catch (Exception ex)
{
}
}
SOLVED: So I did some digging on the documentation I found a way. There is an event called TAPI3Lib.TAPI_EVENT.TE_CALLSTATE that, if you attach to a watcher, can track if the call state changes. So, for me, this activated when a phone call started and a phone call ended. All I have to do is control a timer with a flag in here and ill get a phone call length.
I am currently developing a windows 8 metro app in that I want to pause my app (Basically game) when charms bar opens up that I am doing :
CustomSettingFlyout.Show();
PauseGame();
But after user closes the Charm bar I want to resume my game.I thought there would be an event that says that the charms has been closed, but that's not the case. What's the correct way of to get charms bar got closed?
I also tried this :-
Window.Current.Activated += Current_Activated;
Added this in main class's Constructor
void Current_Activated(object sender, WindowActivatedEventArgs e)
{
if (e.WindowActivationState == CoreWindowActivationState.CodeActivated)
{
if (CharmsActivated)
{
CharmBar.IsOpen = true;
CharmsActivated = false;
ResumeGame();
}
}
if (e.WindowActivationState == CoreWindowActivationState.Deactivated)
{
if (CharmBar.IsOpen == true)
{
CharmsActivated = true;
CharmBar.IsOpen = false;
PauseGame();
}
}
}
But that is not working.Please tell me if there is another way to do this.
(C#, XAML, Windows 8.1 app)