Skip to content

Instantly share code, notes, and snippets.

@danielrobbins
Created October 5, 2017 02:33
Show Gist options
  • Save danielrobbins/4e2e8607f46f9a602aede4a5933bf12b to your computer and use it in GitHub Desktop.
Save danielrobbins/4e2e8607f46f9a602aede4a5933bf12b to your computer and use it in GitHub Desktop.
Test Xamarin.Forms app with Picker input issues
using System;
using System.Threading.Tasks;
using Xamarin.Forms;
public class Scene : object
{
protected RelativeLayout _slidein_layout = null;
protected RelativeLayout _static_layout = null;
protected StackLayout _next_button = null;
protected Grid _grid = null;
public Scene(Grid parent_grid)
{
this._grid = parent_grid;
}
public void CleanupPreviousScene()
{
if ((_grid != null) && (_slidein_layout != null))
{
_grid.Children.Remove(_slidein_layout);
}
if ((_grid != null) && (_static_layout != null))
{
_grid.Children.Remove(_static_layout);
}
}
public void SetupScene(String mode)
{
StackLayout stack = new StackLayout
{
// inner black box that directly contains text and widgets
Margin = 15,
Orientation = StackOrientation.Vertical,
HorizontalOptions = LayoutOptions.Center
};
switch (mode)
{
case "welcome":
Label welcome_label = new Label
{
TextColor = Color.White,
Text = "Welcome to app!",
FontSize = 18
};
stack.Children.Add(welcome_label);
break;
case "picker":
Label picker_label = new Label
{
TextColor = Color.White,
Text = "Choose something",
};
stack.Children.Add(picker_label);
Picker picker = new Picker
{
BackgroundColor = Color.FromHex("111111"),
TextColor = Color.White,
WidthRequest = 400
};
picker.Items.Add("Item 1");
picker.Items.Add("Item 2");
stack.Children.Add(picker);
break;
}
StackLayout stack_outer = new StackLayout
{
/* outer black box that contains the "Welcome to app!" text */
Opacity = 0.95,
HeightRequest = 100,
BackgroundColor = Color.Black,
Children = { stack }
};
_slidein_layout = new RelativeLayout
{
VerticalOptions = LayoutOptions.FillAndExpand,
HorizontalOptions = LayoutOptions.FillAndExpand,
Opacity = 1.0
};
_slidein_layout.Children.Add(stack_outer,
/* x constraint */
Constraint.RelativeToParent((parent) =>
{
// this means it's initially off-screen
return parent.Width;
}),
/* y constraint */
Constraint.RelativeToParent((parent) =>
{
return parent.Height * 0.15;
}),
/* width constraint */
Constraint.RelativeToParent((parent) =>
{
return parent.Width;
}),
/* height constraint */
Constraint.RelativeToParent((parent) =>
{
return 100;
})
);
_static_layout = new RelativeLayout
{
VerticalOptions = LayoutOptions.FillAndExpand,
HorizontalOptions = LayoutOptions.FillAndExpand,
};
CreateNextButton();
_grid.Children.Add(_static_layout, 0, 0);
_grid.Children.Add(_slidein_layout, 0, 0);
}
public void CreateNextButton()
{
Label next_label = new Label
{
Margin = 15,
TextColor = Color.White,
Text = "Next >",
FontSize = 18,
InputTransparent = true
};
StackLayout next_button_inner = new StackLayout
{
Opacity = 100.0,
HorizontalOptions = LayoutOptions.Center,
Orientation = StackOrientation.Vertical,
Margin = 15,
Children = { new Label { TextColor = Color.White, Text = "Next >", FontSize = 18 } },
};
_next_button = new StackLayout
{
Opacity = 100.0,
HeightRequest = 100,
BackgroundColor = Color.Green,
Children = { next_button_inner }
};
var tgr = new TapGestureRecognizer();
tgr.Tapped += async (object sender, EventArgs e) =>
{
await _next_button.FadeTo(0.0, 250, Easing.CubicOut);
await ExitTransition();
};
_next_button.GestureRecognizers.Add(tgr);
_static_layout.Children.Add(_next_button,
/* x constraint */
Constraint.RelativeToParent((parent) =>
{
return parent.Width * 0.7;
}),
/* y constraint */
Constraint.RelativeToParent((parent) =>
{
return parent.Height * 0.8;
}),
/* width constraint */
Constraint.RelativeToParent((parent) =>
{
return parent.Width * 0.3;
}),
/* height constraint */
Constraint.RelativeToParent((parent) =>
{
return 100;
})
);
}
public async Task ExitTransition()
{
await _next_button.ScaleTo(0.9, 250, Easing.CubicOut);
await _next_button.FadeTo(0.0, 250, Easing.CubicOut);
await _slidein_layout.TranslateTo(-2 * Application.Current.MainPage.Width, 0, 500, Easing.CubicIn);
_slidein_layout.IsVisible = false;
CleanupPreviousScene();
SetupScene("picker");
EnterTransition();
}
public void EnterTransition()
{
Device.BeginInvokeOnMainThread(async () =>
{
await _slidein_layout.TranslateTo(-Application.Current.MainPage.Width, 0, 500, Easing.CubicIn);
await _next_button.ScaleTo(1.0, 250, Easing.CubicOut);
await _next_button.FadeTo(1.0, 250, Easing.CubicOut);
});
}
}
namespace testapp
{
public class MyPage : ContentPage
{
protected Scene _scene = null;
protected Grid _grid = null;
public MyPage()
{
NavigationPage.SetHasNavigationBar(this, false);
this._grid = new Grid
{
VerticalOptions = LayoutOptions.FillAndExpand,
HorizontalOptions = LayoutOptions.FillAndExpand,
RowDefinitions =
{
new RowDefinition { Height = new GridLength(1, GridUnitType.Star) },
new RowDefinition { Height = new GridLength(30, GridUnitType.Absolute) }
},
ColumnDefinitions =
{
new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }
},
RowSpacing = 0,
ColumnSpacing = 0
};
}
protected override void OnAppearing()
{
this.Content = this._grid;
this._scene = new Scene(_grid);
_scene.SetupScene("welcome");
_scene.EnterTransition();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment