DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Keeping Input Fields Above the Keyboard in UWP Apps

Sometimes the UWP renderer in Windows Mobile doesn't push the UI up far enough. Check out how to address the issue in this post.

Joost van Schaik user avatar by
Joost van Schaik
·
May. 19, 16 · Opinion
Like (1)
Save
Tweet
Share
4.96K Views

Join the DZone community and get the full member experience.

Join For Free

Before you all think I am stark raving mad, it appears that it is actually possible to create XAML constructions that confuse the UWP renderer to such an extent that, although it moves the user interface upwards as it should, it does not always move it up far enough. This can be observed in the video below, as well as the fact that it is fixable.


A user observed this on my app Map Mania (it has been fixed since). I have only been able to repro this on Windows 10 mobile. Apparently it has something to do with going rampant on adaptive triggers, and another key part is the use of a bottom app bar.

The XAML is a simplified version of what I used for the post about a CompositeTrigger-enabled AdaptiveTrigger. Basically, I use a simple ViewModel and VisualStateGroup with some Triggers to change what I see on the screen. The XAML is not too complicated:

<Grid >
 <Grid.RowDefinitions>
 <RowDefinition Height="Auto"></RowDefinition>
 <RowDefinition Height="*"></RowDefinition>
 </Grid.RowDefinitions>
 <controls:PageHeader Text="Fix" FontSize="30" VisualStateNarrowMinWidth="0" 
 VisualStateNormalMinWidth="700"></controls:PageHeader >
 <Grid Grid.Row="1" Margin="12" x:Name="TopGrid">
 <Grid.ColumnDefinitions>
 <ColumnDefinition Width="*"></ColumnDefinition>
 </Grid.ColumnDefinitions>
 <Grid>
 <Grid.RowDefinitions>
 <RowDefinition Height="60"></RowDefinition>
 <RowDefinition Height="*"></RowDefinition>
 <RowDefinition Height="Auto"></RowDefinition>
 </Grid.RowDefinitions>

 <Grid Margin="0,6,0,6" x:Name="NarrowMenu" >
 <Grid.RowDefinitions>
 <RowDefinition Height="Auto"></RowDefinition>
 <RowDefinition Height="Auto"></RowDefinition>
 </Grid.RowDefinitions>
 <Grid.ColumnDefinitions>
 <ColumnDefinition Width="Auto"></ColumnDefinition>
 <ColumnDefinition Width="Auto"></ColumnDefinition>
 </Grid.ColumnDefinitions>
 <TextBlock Text="Green" Grid.Row="0" FontSize="20"
 Margin="0,0,6,0" Tapped="{x:Bind ViewModel.ToggleDisplay}" >
 </TextBlock>

 <TextBlock Text="Red" Grid.Row="0" Grid.Column="1" FontSize="20"
 Margin="6,0,0,0" Tapped="{x:Bind ViewModel.ToggleDisplay}">
 </TextBlock>
 <Grid Height="2" Background="White" Grid.Row="1" Grid.Column="0" Margin="0,0,6,0" 
 x:Name="GreenUnderline"/>
 <Grid Height="2" Background="White" Grid.Row="1" Grid.Column="1" Margin="6,0,0,0" 
 x:Name="RedUnderline"/>
 </Grid>

 <Grid Background="Green" Grid.Row="1" x:Name="GreenArea"></Grid>
 <Grid Background="Red" Grid.Row="1" x:Name="WideRedArea"></Grid>

      <StackPanel Grid.Row="2" Orientation="Vertical" HorizontalAlignment="Stretch" 
         VerticalAlignment="Bottom" >
        <TextBlock  Text="Some label" x:Uid="MapName"  Margin="0,0,0,6"/>
        <TextBox TextWrapping="NoWrap"/>
      </StackPanel>
 </Grid>
 </Grid>
</Grid>


This stuff is based on Template10, but the actual usage is very limited. First, we have some heading, then the menu, then the two areas (green and red) that are used to fill the middle of the screen—it stands in for actual content. Then, all the way below in red and bold, the StackPanel that has some problems as displayed in the video. When you click on the menu text (“Red” and “Green”) a command in the view model is called that flips a property “TabDisplay”. This triggers the VisualStateManager.

The VisualStateManager is actually pretty simple:

<VisualStateManager.VisualStateGroups>
 <VisualStateGroup x:Name="WindowStates" >
 <VisualState x:Name="NarrowState_Red">
 <VisualState.StateTriggers>
 <StateTrigger IsActive="{x:Bind ViewModel.TabDisplay, Mode=OneWay}"/>
 </VisualState.StateTriggers>
 <VisualState.Setters>
 <Setter Target="WideRedArea.Visibility" Value="Visible"></Setter>

 <Setter Target="GreenUnderline.Visibility" Value="Collapsed"></Setter>
 </VisualState.Setters>
 </VisualState>

 <VisualState x:Name="NarrowState_Green">
 <VisualState.StateTriggers>
 <StateTrigger 
 IsActive=
 "{x:Bind ViewModel.TabDisplay, Mode=OneWay,Converter={StaticResource BoolInvertConverter}}"/>
 </VisualState.StateTriggers>
 <VisualState.Setters>
 <Setter Target="WideRedArea.Visibility" Value="Collapsed"></Setter>
 <Setter Target="RedUnderline.Visibility" Value="Collapsed"></Setter>
 </VisualState.Setters>
 </VisualState>

 </VisualStateGroup>
</VisualStateManager.VisualStateGroups


So far, so good but when you use a construction like this, and you put anything below it, your might run into issues as I described.Unless you add a little something to the StackPanel:


<StackPanel Grid.Row="2" Orientation="Vertical" HorizontalAlignment="Stretch"
 VerticalAlignment="Bottom" >
  <interactivity:Interaction.Behaviors>
    <behaviors:KeepAboveInputPaneBehavior/>
  </interactivity:Interaction.Behaviors>
 <TextBlock Text="Some label" x:Uid="MapName" Margin="0,0,0,6"/>
 <TextBox TextWrapping="NoWrap"/>
</StackPanel


And people who know me won’t be surprised is it actually a behavior again.

using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Microsoft.Xaml.Interactivity;

namespace WpWinNl.Behaviors
{
 public class KeepAboveInputPaneBehavior : Behavior<FrameworkElement>
 {
 private Thickness _originalMargin;

 protected override void OnAttached()
 {
 base.OnAttached();
 AssociatedObject.Loaded += AssociatedObjectLoaded;
 _originalMargin = AssociatedObject.Margin;
 }

 private void AssociatedObjectLoaded(object sender, RoutedEventArgs e)
 {
 AssociatedObject.Loaded -= AssociatedObjectLoaded;
 InputPane.GetForCurrentView().Hiding += InputPaneHiding;
 InputPane.GetForCurrentView().Showing += InputPaneShowing;
 }

 protected override void OnDetaching()
 {
 InputPane.GetForCurrentView().Hiding -= InputPaneHiding;
 InputPane.GetForCurrentView().Showing -= InputPaneShowing;
 }

 private void InputPaneShowing(InputPane sender, InputPaneVisibilityEventArgs args)
 {
 AssociatedObject.Margin = 
 new Thickness(_originalMargin.Left, _originalMargin.Top, 
 _originalMargin.Right, _originalMargin.Bottom + args.OccludedRect.Height);
 }

 private void InputPaneHiding(InputPane sender, InputPaneVisibilityEventArgs args)
 {
 AssociatedObject.Margin = _originalMargin;
 }
 }
}


When the attached object is loaded, it’s original margins are recorded. When the input pane is showing, the height of the ‘OcculedRect’ is added to it, moving the attached object op to exactly above the input bar.

This is possibly a bug, or the SDK team just never imagined people doing odd things with the Visual State Manager – “A fool may ask more questions in an hour than a wise man can answer in seven years”, right ;). Whatever, I like I tell people you can moan about things like this or cry foul at Microsoft, but I find it much more fun to try and fix them. QED.

A sample solution, with the behavior, can be found here.

mobile app

Published at DZone with permission of Joost van Schaik, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • DevOps Roadmap for 2022
  • PostgreSQL: Bulk Loading Data With Node.js and Sequelize
  • Secrets Management
  • Using AI and Machine Learning To Create Software

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: