Windows phone - how to make a button that has two text elements - windows-phone-7

I am wondering what would be the easiest/best way to make a button that has two text elements. One on the left side and another on the right.
First I was thinking to place a TextBlock on top of a Button, but the TextBlock should change color like the Button's text does when the Button is pressed etc. and I haven't found a way to do this perfectly. Also I would like a bit smoother solution.
Should I inherit the Button and make my own version? And is there some example or tutorial somewhere out there that would help on this?
I haven't done much with Windows Phone before as you can probably guess.

there is several variants in your case
Put Container with two TextBlocks inside Button
<Button>
<Grid>
<Grid.ColumnDefenitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefenitions>
<TextBlock Text="TIME"/>
<TextBlock Text="2:20" Grid.Column="1" HorizontalAlignment="Right"/>
</Grid>
</Button>
Another option is to use StringFormat, if you use Binding
<Button Content="{Binding time, StringFormat={}TIME {0:t}}"" />

Related

Position of buttons and designs in Xamarin

Scenario 1:
I'm trying to create a mobile application using Xamarin. I want the position of my buttons and design to be in its place. I tried to run it on my smartphone, and the buttons and designs' position looks good, but when I tried to run it on another smartphone, they all got messed up. It changes its position. I used stacklayout here.
Scenario 2:
Then some people say they used grid. So, I tried using a grid in creating my login page. The problem is when I click on the entry box to type a username/password, of course the keyboard will pop up, but the whole UI will go higher/above its original position. When I tried to not type username/password or click the back button from the keyboard, the UI will go back to its original position.
How to fix any of these?
I tried to run it on my smartphone, and the buttons and designs'
position looks good, but when I tried to run it on another smartphone,
they all got messed up. It changes its position.
Xamarin.Forms XAML Support :
Xamarin.Forms use the platform-specific mechanisms to calculate the absolute pixel dimensions. Xamarin.Forms uses xaml as it's base markup language for renderng displays, and converts this into the native counterparts at runtime. Usually, you don't have to care about the resolution, it will adjust the views based on your layout and constraints.
Some useful link about Xamarin.Forms multi-device support :
Bringing Xamarin.Forms Apps to Tablets
Provides a few nice helper methods to extend the app for a better tablet experience
Xamarin.Forms Device Class
The Device class contains a number of properties and methods to help developers customize layout and functionality on a per-platform basis.
Layout for Tablet and Desktop apps
Discuss about the supported device types, and how to optimize layouts for tablets versus phones.
For Scenario 2
The problem is when I click on the entry box to type a
username/password, of course the keyboard will pop up, but the whole
UI will go higher/above its original position. When I tried to not
type username/password or click the back button from the keyboard, the
UI will go back to its original position.
I don't know what's the detail of your app, but you can refer to the following code,which does not present the problem you mentioned.
<Grid Margin="20" >
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Frame HeightRequest="30" BackgroundColor="White" CornerRadius="10" Grid.Row="0" Grid.ColumnSpan="2" />
<Entry Grid.Row="0" Grid.Column="0" MaxLength="30" Placeholder="User Name:" ClearButtonVisibility="WhileEditing" />
<Image Source="head.png" Scale="0.6" Grid.Row="0" Grid.Column="1"/>
<Frame HeightRequest="30" BackgroundColor="White" CornerRadius="10" Grid.Row="1" Grid.ColumnSpan="2"/>
<Entry Grid.Row="1" Grid.Column="0" MaxLength="30" Placeholder="Password:" ClearButtonVisibility="WhileEditing" IsPassword="True"/>
<Image Source="Lock.png" Scale="0.6" Grid.Row="1" Grid.Column="1"/>
</Grid>
<Button Text="Login"/>
The result is:
Update
Since your controls(e.g. the two Entry ) are at the bottom of the page,when you want to enter something, the keyboard will pop up. The keyboard will surely take up some space on the screen. The system makes a decision as to how it should adjust the visible portion of your UI, but it might not get it right. To ensure the best behavior for your app, you should specify how you'd like the system to display your UI in the remaining space.
From the Android Developer Site link, we know
"adjustResize"
The activity's main window is always resized to make room for the soft
keyboard on screen.
"adjustPan"
The activity's main window is not resized to make room for the soft
keyboard. Rather, the contents of the window are automatically panned
so that the current focus is never obscured by the keyboard and users
can always see what they are typing. This is generally less desirable
than resizing, because the user may need to close the soft keyboard to
get at and interact with obscured parts of the window.
In xamarin, you can change the value of WindowSoftInputModeAdjust in class Application
<Application ...
xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core"
android:Application.WindowSoftInputModeAdjust="Resize">
...
</Application>
For more details, you can check:
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/platform/android/soft-keyboard-input-mode .
https://developer.android.com/training/keyboard-input/visibility

Using AdControl in windows phone 7

How to make Adcontrol always visible at bottom of the screen. Right now it is going at the end of the screen.
I have pivot control, in it I have Listbox which is dynamically binding with data. After pivot control I have adcontrol. So when the data grows in Listbox, adcontrol goes down. How to make it fixed at the bottom.
Your root layout control should be a grid which wraps the pivot and ad control:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<toolkit:Pivot Grid.Row="0">
<ListBox>
...
</ListBox>
</toolkit:Pivot>
<AdControl Grid.Row="1"/>
</Grid>
You can most certainly use the popup call and contain the ad in it. My solution below is using a canvas to hold the ad in which would net the same result, but may be more familiar with what you've used before.
Also, you don't have to set the height and width of your listbox/pivot container if you didn't want to. What the canvas/popup would do is be outside of the pivot control, so it is always on top of the stack.
For instance (approximating your code)
<Grid x:Name="LayoutRoot">
<controls:Pivot>
<!--PivotItemOne-->
<controls:PivotItem>
<Grid>
<!-- code -->
</Grid>
</controls:PivotItem>
....
</controls:Pivot>
<Canvas x:Name="Ad" VerticalAlignment="Bottom" Width="Auto" Height="80"/>
<!-- or whatever height you want depending on ad size -->
</Grid>
This way, the canvas is always at the bottom of the page and hovering over your pivot control. It will not matter if the pivot changes as it is outside of the Pivot control.
If you ever needed to get rid of the Ad visibility or destroying the container itself, it would require less work overall than changing your listbox/pivot size.

Weird behaivior of ProgressBar in WP7

So, I'm tryging to use a progress bar in my WP7 Panorama aplication.
There is one page, that is downloadidng data from the web, so I want to indicate that my App is doing something with ProgressBar
<controls:PanoramaItem Header="news">
<Popup Name="myWeatherPopup" IsOpen="False" Width="404" Height="Auto" Margin="0,136,4,293">
<StackPanel>
<ProgressBar Height="Auto" IsIndeterminate="True" Width="400" />
<TextBlock Text="Loading" HorizontalAlignment="Center" Foreground="Gray" />
</StackPanel>
</Popup>
</controls:PanoramaItem>
It basically works as it should. However, there is one tiny (and anoying bug).
The first... run (?) of the dots appears on every panorama item, just under header. After that, it returns on its normal position under news item.
The same thing happens on emulator and Lumia 800.
Image
Have you tried without the Popup, by just setting the Visibility property on the StackPanel?

Scrollviewer & SIP Issue (WP7.5 Mango)

I am working on an application which includes a registration form. The form contains multiple text entry boxes, and so a ScrollViewer is used to allow them all to be displayed on one page.
The following is a stripped down example of the XAML code I am using:
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="SCROLLVIEWER TEST" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="registration" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<ScrollViewer Grid.Row="1">
<StackPanel>
<TextBlock Text="Hello" Margin="12,0,0,0"/>
<TextBox />
<TextBlock Text="Hello1" Margin="12,0,0,0"/>
<TextBox />
<TextBlock Text="Hello2" Margin="12,0,0,0"/>
<TextBox />
<TextBlock Text="Hello3" Margin="12,0,0,0"/>
<TextBox />
<TextBlock Text="Hello4" Margin="12,0,0,0"/>
<TextBox />
<TextBlock Text="Hello5" Margin="12,0,0,0"/>
<TextBox />
<TextBlock Text="Hello6" Margin="12,0,0,0"/>
<TextBox />
<TextBlock Text="Hello7" Margin="12,0,0,0"/>
<TextBox />
<TextBlock Text="Hello8" Margin="12,0,0,0"/>
<TextBox />
<TextBlock Text="END" Margin="12,0,0,0"/>
<TextBox />
</StackPanel>
</ScrollViewer>
</Grid>
(Note that the ScrollViewer is inside a grid cell, which means that the title panel should stay OnScreen at all times)
The scrolling works perfectly fine, so that is not an issue. However, when the user selects a TextBox to enter data (i.e. the soft keyboard opens), the system pushes the content of the entire page around (including the registration title panel), which is not expected behaviour.
[See the People app on Windows Phone and try adding a new contact. This contains a similar structure, but the ScrollViewer behaves correctly by only pushing content in the scrollviewer up]
Test Cases
Select a TextBox that is visible near the top of the screen, to open the keyboard.
Attempt to scroll to the bottom of the page with keyboard open.
Items at the bottom of the page are unreachable.
or
Select a TextBox that is visible near the bottom of the screen.
Content of entire page is pushed up.
Attempt to scroll to the top of the page with keyboard open.
Items at the top of the page are unreachable, and title panel never comes back into view until keyboard is closed.
Any help on resolving this issue would be appreciated. Thanks.
The problem is that the ScrollViwer height is not modified after the keyboard appears so it becomes clipped. One solution would be to modify the height of the scrollviwer (according to the keyboard height) and then reposition it (this might give you some headaches).
Another problem is knowing when the keyboard appears - you could register for the GotFocus/LostFocus events on all your TextBoxes but it's not a great solution. This might help you: http://blogs.msdn.com/b/jaimer/archive/2010/11/05/guessing-if-the-sip-is-visible-in-a-windows-phone-application.aspx
Hope this helps a little :)
I think you can solve this by coming at the problem from another angle. The phone will scroll up the page so that the SIP (software keyboard) never covers up the TextBox which has focus.
However you can force the SIP to hide by detecting touch events on the top element contained in your ScrollViewer, e.g.:
<ScrollViewer Grid.Row="1">
<StackPanel ManipulationDelta="OnScrollViewerGridManipulationDelta">`
Then, by giving the focus to a hidden button (0x0 pixels in size) this will force the SIP to close. Then it will be possible for your users to scroll up and down the scrollviewer as expected...
private void OnScrollViewerGridManipulationDelta(object sender, System.Windows.Input.ManipulationDeltaEventArgs e)
{
// This will hide the SIP if it is currently showing.
// We can't do this directly, but we can force this by taking focus away from any of the TextBoxes that may have it.
this.hiddenButton.Focus();
}
I've had the same issue with an app I've developed and the way I dealt with it was to find out the auto height of the panel containing the input textboxs and then manually set the height and add approximately 400 - 500 px to the bottom to make it scroll nicely. The effect is quite smooth and will not make your UI look "hackish" IMHO.
In your case you will have to find out the automatic height of the LayoutRoot Grid and then on RowDefinitionof Row 1 set the height manually - remembering to add an extra 400px (or whatever looks appropriate in your situation).
For ease of input I then handled each OnKeyDown event of each TextBox to change the focus to the next TextBox upon hitting Enter. On the last TextBox I set the focus to this.focus() which sets focus to the Page and hides the SIP.
Have a look at my small library please - https://siphelper.codeplex.com/
It modifies height of scrollviewer and content can be scrolled to the topmost/bottommost point.
If you have any suggestions - feel free to contact me.

Windows Phone 7 dev: How to autoscroll textblock's text Horizontally

I have this textblock <TextBlock Text="{Binding Name}" TextWrapping="NoWrap" Margin="12,-3,12,0" FontSize="27"/> , and I want to tell me a way in order to text, scrolled automatically when it is longer than screen.
Wrap it in a ScrollViewer
<ScrollViewer>
<TextBlock Text="{Binding Name}" TextWrapping="NoWrap" Margin="12,-3,12,0" FontSize="27"/>
</ScrollViewer>
For those who still interest and wants to do one for yourself, please take a look at my article on How to create marquee TextBlock on Windows Phone
It uses Storyboard to perform animation so that all are handled by GPU
I agree that it isn't a good practice, but look at my answer if you really need it.

Resources