Tip:
Highlight text to annotate it
X
I also want some padding around my name. Padding is the space inside the control between the Text and each edge.
If I set Padding to 10
what shows of my name gets very small … that won't work. With the Height at 30 and all that padding
there is not enough space for my letters.
Rather than trying to figure out the best height
I could delete the Height setting so the control can be whatever size it wants to be.
If I need to control the range that my height will shrink or grow to
I can set MinHeight and MaxHeight.
Right now, however, space is not an issue
What happens if I change my Vertical Alignment?
My control is inside my stack panel
which is inside my ContentPanel grid.
A grid can have rows and columns but non are defined so my grid
currently, is just a single cell with a stack panel inside.
For what I have done so far
I wouldn't need the grid
– but it is probably a good idea to keep it since I don't know if anything bad would happen if my phone app was missing it.
I position my cursor on the value of " Top" and double-click my mouse to select the word.
Typing replaces section and what I want to be deleted is my selection
so I start typing …c…. Intellisense pops up. I press TAB to choose "Center".
Now that I see what happens
I don't think I will do that.
The control moved as per my instructions
– to the center of the total area
since I have not defined the grid row that my stack panel is in to have a specific height.
I'll undo the change to the VerticalAlignment so my control goes back to the top.
I wonder what other properties might be interesting… I resize my windows so I can see the Properties better
(David M. Bailey: "Changes come and changes go")
("but really we're the same")
("and if we don't see eye-to-eye, no one is to blame"
("Somewhere there's a rainbow that never sings the blues")
("When I find it, I'll send it straight to you")
Everything looks good so far, so I will Save All my files ("Somewhere there's a song that tells the truth about our hearts")
Now I click the Run button, which both Builds,
and Runs if everything is ok.
Normally I prefer to do this in 2 steps
especially if I think anything might have a problem.
There is the the phone emulator.
There is the Start screen
… and there is my application.
I see the 2 controls I just added. I click in the textbox and now I can type my name using the on-screen keyboard.
The on-screen keyboard is a SIP, or software input panel.
The on-screen keyboard is the default InputScope when running the emulator.
My name is Crystal, which starts with capital "C"
The keyboard is showing little letters.
I click on the SHIFT key to toggle the display on the keyboard to uppercase. I see capital C
so I click on it. Capital C goes into my textbox.
If I was using a phone, I would be touching instead of clicking.
Now I need a little "r". I click on the SHIFT key to toggle back to lowercase letters. I pick "r".
This is so slow! If I press PageUp, I can use my computer keyboard since I am running the emulator.
Using my keyboard is much faster .
I have a place for my name, and that's it.
I click BACK, which terminates my application in the emulator.
Before that happens, code in the Page.OnNavigatingFrom method, if it exists, would be called.
Code in the Application_Closing event is also executed.
I don't have code there now, but I might in the future.
It is good to know what the possibilities are.
In my solution window, I click on the green triangle icon to run my app again.
I see my name is … not there anymore.
I've done nothing to save it, or display it.
When I leave my main page, anything in the controls is lost.
I would like to save my name.
I go to the markup and click on the textbox containing my name.
In the Properties Window I click on the Events tab.
There are all kinds of events I can use.
The default event is TextChanged, which happens every time any character is typed or deleted.
I only need to store my name when the typing is all done.
The event I want is SelectionChanged.
I double-click in the property setting for the SelectionChanged event.
This creates a method declaration and curly braces for my code block.
This is also called my event handler.
Any code I write between the start and end braces will be executed when my name is changed.
Before I create any code, in case I need to look up Help
is is good to remind myself what I am using.
My .NET development language is C#
which is part of Visual Studio .NET,
which is built on top of Microsoft .NET Framework, which has common components to simplify development and deployment.
C# is also case-sensitive.
Where do I save my name?
The Windows Phone has a storage area that is isolated and protected from other applications
It is called Isolated Storage.
The easiest way to save my name in isolated storage is to use a Key/Value pair.
My app only needs to save one setting in isolated storage, but I could have more.
The settings in isolated storage can be defined for each user on an application level or a site level.
I will use isolated storage APPLICATION settings since I do not plan to share my name setting with another application on my site, at least not right now.
First I will create a reference to my application settings. I start typing.
IsolatedStorageSettings
IsolatedStorageSettings, however has a red squiggly line underneath indicating it is not understood.
The little blue rectangle under the first character of the word means this keyword is contained in one or more namespaces.
I press Ctrl-Period and am prompted to choose a namespace. The one I want is the first one on the list
Before I double-click to choose it, look at the Using statements listed at the top of my code.
The last one, currently, is Microsoft.Phone.Controls
I double-click on System.IO.IsolatedStorage
A new Using statement appears in the directives at the top of my code.
Namespaces contain code that can be used by my code.
Since not every program class needs to use isolated storage, the namespace for it is optional.
Now that the namespace is referenced, the color of my IsolatedStorageSettings keyword is aqua.
I need to specify the name I will use to refer to my settings …. hmm
application… settings… appSettings is a good name….
I can name the reference whatever I want.
I could have picked something else like mySettings, or isolatedStorageNamePairs, or something else.
I try to follow the guidelines for naming on MSDN:
Here is a good article on Guidelines for Names in .NET Framework 4
The case I am using for appSettings is called Camel Case
because it begins with a little letter and has a big letter somewhere in the middle of the word.
Here is a good page on MSDN for Capitalization Conventions
One of the many articles I read on isolated storage was "How to: Create a Settings Page for Windows Phone" on msdn
Links are in the video description so
you can look while I do if you want to, or look later
Another good article on msdn is
"Isolated Storage Overview for Windows Phone"
Bob Tabor's videos on isolated storage are really good.
I have a playlist for his entire Windows Phone for Absolute Beginners series
on my LearnByCrystal channel.
The meat of isolated storage starts with Day 3, Video 6
"Understanding Isolated Storage" video by Bob Tabor
Now that I have declared appSettings, I can assign a value.
Equal sign
As I start typing Isolated
I see there are 4 classes starting with IsolatedStorage that I can pick from:
* IsolatedStorageException
* IsolatedStorageFile
* IsolatedStorageFileStream
* IsolatedStorageSettings
I choose IsolatedStorageSettings
then I type a dot to see what choices intellisense will give me.
I type "a" and press TAB to select ApplicationSettings. Then I type semi-colon to end the line. This is my complete statement
IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;
I have a way to reference the isolated storage area.
First I want to see if isolated storage already contains a key called MyName.
If it is already there, I will change the saved value.
If not, I will add a new key. Here is my code:
if (appSettings.Contains("MyName"))
{appSettings["MyName"] = this.textBox_MyName.Text;}
else {appSettings.Add("MyName", this.textBox_MyName.Text);}
WHERE this. Refers to the form my code is behind
textBox_MyName is the Name property of the control that has my name
.Text is the text property of the control with my name
MyName is what I am calling the key in isolated storage
Now I will Save All the files and Build my solution.
Build succeeded is displayed in the lower left corner of the screen.
In case you weren't so lucky, I will show you how to turn on the Error List.
Ctrl-W, E or you can choose from the menu
I will go back to my XAML page. My name should get saved.
I will put the SelectionChanged event on its own line as I have done with the other properties.
When my page loads, I would like it to read the MyName key
in isolated storage and display its value in my textbox control.
I move to the root element at the top of my XAML page.
I create a new line and type … lo… I see "Loaded" on the intellisense list.
I press TAB to choose it.
Now I press ENTER to choose
The name for my event handler is created for me and is called PhoneApplicationPage_Loaded.
I right-click on the event handler name
and choose Navigate to Event Handler from the shortcut menu.
The code to load my name will also need a reference to the settings in isolated storage.
I select the declaration statement I typed in the SelectionChanged event. I click on my selection and drag it to my Loaded event.
I press Ctrl before I let go of the mouse
so that when my mouse is released, what is selected is is copied instead of moved.
I remember this because Ctrl and Copy both start with a "C".
Note the PLUS sign next to the mouse cursor indicating that the operation is Copy.
Once again, I want to see if the settings contain my key.
If the key is there, I am going to reverse the assignment.
I am getting an error message on this line. The error says
The text in my textbox will be given the value of
the MyName key in isolated storage.
"Cannot implicitly convert type 'object' to string. An explicit conversion exists (are you missing a cast?)"
I will try casting it to a string
Aah, seems to like that. Here is the statement that works:
this.textBox_MyName.Text = (string)appSettings["MyName"];
If isolated storage does not contain my key
an empty string will be assigned to the text in my textbox.
this.textBox_MyName.Text = string.Empty;
I Save All and Build the project. No errors – good – yay! Save all again,
click on the green triangle to test my app.
I click in my textbox to enter my name. The emulator still remembers that I am using my computer keyboard so I type my name.
Now I click the BACK button to close my app on the emulator
and leave the emulator running.
Note that when the emulator program is terminated,
whatever is in isolated storage is lost.
This is different behavor than a real phone, which remembers what is in isolated storage.
I click on the green triangle to run my app again.
Aha! I am remembered
("Then take the time to sit outside and watch the sunset")
("as it slowly melts the corners of your soul")
("Put a candle in the window",)
(" say a prayer for those you don't know,")
("and ask the God of Peace to make… to make us whole")
David M. Bailey was amazing.
He performed a couple songs for me.
One of them was "Miracle Change".
He touched me to the core and left me speechless.
I didn't even have the breath to say "Thank You" ... he said it for me.
Sadly, David passed on a year ago.
He was a poet, philosopher, prolific songwriter,
and so much more -- and he loved coffee.
Where he went, he made a difference.
His music lives on to inspire, give hope,
and lead us to our own revelations.
I consider myself extremely fortunate to have known him.
"Put a candle in the window and say a prayer for those you don't know"
while you're watching the sunset. Thank you, David �