Week3 Examples: Sensors, Asynchronous Callbacks, and Touch Manipulation
- Vibration, Accelerometer, GPS:
- Vibration: follow the two button service functions. Boring. We can
stop the vibration before the end of the programmed time. Big Deal!
- Accelerometer: Not that interesting either, ... except
- Once again, see the powerful object-abstraction
interface. As far as we are concerned, Accelerometer is but
another object
- Follow the checkbox service functions: WhenChecked/Un-Checked:
register/un-register the service function and attempt to
start/stop the sensor. OK.
- Accelerometer readings when resting: -1 is gravitational
pull!
- ReadingChange service function: WHAT in the name are we
doing here?
- Try: un-comment out the commended out code and
change the TextLabel directly. Let's hope it will fail.
- Try: un-comment out the conditional check before
string update, and check Accelerometer on/off, and see
after the accelerometer is off, there are still string
updates coming and will erase the "Accelerometer is off"
string! What is going on?
- What is this "BeginInvoke()" thing
- What is "() => {}" syntax? (next example)
- GPS: exactly parallel Accelerometer! Conceptually, identical
interface!
- PositionChange: service routine, also observe the "BeginInvoke()"
thing.
- Interesting example, CivicAddress Resolver, does not work but we are
making a sync query to avoid async call back registration. How can we
avoid the tedious event function registration?
- Delegates, Actions, and "shorthands"
for event functions
- delegate: lets you defined function pointer data type
- Example: GPSResponseFunctionType(GeoCoordinate, DateTimeOffset)
<-- new data type
- Must define variable for data type: Example: GPSResponseFunction,
or AccResponseFunction. These are variables!!
- Action: function pointer data type that:
- Accepts 1 to 3 arguments
- Always return void
- Note, Action is a data type!! We can declare variables of Actino<Type>
data type. Example: AccResponseAction is a variable!
- Event service short hand:
- MainPage::constructor: mGetAddress.ResolveAddressCompleted <--
what is this line of code doing?
- Notice: the explicit declaration of data types for arguments are
optional!! This is precisely the lambda expression (in-line function
definition) we saw in the previous example!!
- Here we register for event service to async civic address
resolving request
- Using delegate:
- Assign initial values to the variables: in MainPage()
constructor
- Go to the Accelerometer service routine (AccelerometerReadingChange),
to see we pass the delegate AccResponseFunction as a parameter to be
invoked.
- Go to the GPS enabling button to see: short hand of invocation:
(mGPSOn_Checked): no explicit function definition for event service
for PositionChanged event. Implicitly defined the function!
- Notice, with event service for async CivicAddressResolver
defined, in GPS PositionChange, we make async query to civic address
resolving. (not implemented in the API, so we are still not going to
get a response).
- Watch out: short hand register for an event service function, I am
not sure how you can un-register for this function (without name!):
Example: GPS's PositionChange service function!
- Touch Manipulation (of image):
- Example of using Manipulation to move an image
- Notice: the difference between: DeltaManipulate and
CumulativeManipulation
- In ManipulationDelta: use DeltaManipulate because we accumulates
the results
- Make sure you understand the difference between delta and
cumulative!
- Important to note: once the image is not visible in the UIElement,
manipulation event does not trigger anymore!! Events are triggered only
when "touch" occurs on visible portion of the image!
- Pinch Manipulation (of image):
- Work with Silverlight.Toolkit (External library from:
http://silverlight.codeplex.com/releases/view/75888).
- First thing firsts: declare we are going to work with the library in
MainPage.xaml
- xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
- Remember: to include the library DLL's (Microsoft.Phone.Controls,
and Microsoft.Phone.Controls.Toolkit)
- UIElement layout: pay attention to the <Rectangle> object
- Define: toolkit:GestureService.GestureListner (listen to
gestures on this UIElement)
- PinchStarted/Delta/Complete
- MainPage.xaml.cs: constructor
- mImageToShow: is an ImageBrush (for filling the content of the
Rectangle)
- Must allocate a "CompositeTransform" for the brush
- Going to scale/translate the "brush", not the Rectangle! Think
about the difference!! We are viewing the image through the opening
of the Rectangle object!
- Gesture event services: a little math, but basically:
- Notice: reference to initial values (not current values). This
means, if input returns to 1, we can return the current values!!
- Scaling factor: CurrentFingerDistance/InitFinderDistance
- Translation: step-1. undo previous translation with scaling
consideration (mDeltaVec * s)
- Translation: step-2: redo translation based on current finger
position
- Work with Silverlight.Toolkit: I downloaded their source code: (http://silverlight.codeplex.com/releases/view/75888#DownloadId=304099)
compiled their samples to check out what the library can do. Try it,
pretty cool stuff.
- Misc: Sharing of data across pages, Using Event Parameters, A little
more about the toolkit ...
- Behavior:
- Two pages: navigate to the second page by clicking on the button
- On second Page: see two lists, click on any item (see the tilt
effect) to return to the main page and see selected item name show
in the status echo.
- Sharing of text across the two pages:
- Use the App class
- Open App.xaml.cs file: instance variable:
public
TextBlock
SystemEcho { get;
set;
}
- Open MainPage.xaml.cs: in constructor, sets the
App:SystemEcho TextBlock
- Notice how we access the App object: (Application.Current
as
App).SystemEcho
= mEchoString;
- All pages can access to App object this way. Problem solved?
- Using the parameter of event parameters to share service routine:
- SecondPage: the two lists, notice they have the exact same
service routine for SelectionChange event
- SecondPage.xaml.cs: implementation of the ListSelectionChange
function: uses the parameters.
- The tilt effect of when we click to select object?
- SecondPage.xaml file, in the :
toolkit:TiltEffect.IsTiltEnabled="True"
- Notice, this is exactly identical to put this line of code in
the constructor in SecondPage.xaml.cs:
Microsoft.Phone.Controls.TiltEffect.SetIsTiltEnabled(this,
true);
- Either case, the code says, all children of "this" will support
tilt. So, in this case, all children of "SecondPage" supports Tilt,
but not MainPage!
- PhotoCamera via API (instead of via Task):
- Code sample:
http://msdn.microsoft.com/en-us/library/ff431744%28v=vs.92%29.aspx.
Take a look, once we have a foundation concept, it should be easy to
look at the code and generalize.
- Camera:
http://msdn.microsoft.com/en-us/library/hh202956%28v=vs.92%29.aspx
- Idea of a "Brush" ImageBrush, VideoBrush: as "Background"
for any UIElement
- VideoBrush: can accept and process continuous streaming of photos.
- UI setup: collection of "Canvas" for hosting appropriat "Brush"
- VideoBrush: for hosting camera view finder
- ImageBrushx2: for the photo and the thumbnail
- Remember the model: C# Class representing physical object:
PhotoCamera
- NavigatedTo: camera initialization when first open the page
- Register for event services, REMEMBER: events will come
asynchronously from the hardware device!
- Notice: mCameraViewFinder.RelativeTransform =
new
CompositeTransform()
{ CenterX = 0.5, CenterY = 0.5, Rotation = 90};
- By default: camera supports LandScape
- Camera event service:
- All code within "Dispatcher.BeginInvoke()" block! (asyn!! try
removing BeginInvoke(), illegal thread access will crash the program!)
- Notice the taken pictures, they are "rotated"!
- In camera photo taking, we rotated the image in view finder, but did
not rotate the actual photo we display! Verification of the mental
model!!
- Problems:
- aspect ratio,
- ThumbNail and Photo not rotated (camera
default is landscape mode)
- See how easy it is now for us to take a photo every s