Open DatePicker from a Popup window - windows-phone-7

I want to open a DatePicker from a popup. When I click the datepicker, the date input page (the one with spinners) opens behind the Popup.
How can the DatePicker go over the popup window and return to it after I select the date?
This is how I open the popup:
private void btnShowBuyNow_Click(object sender, RoutedEventArgs e)
{
Popup buyNowScreen;
buyNowScreen = new Popup();
buyNowScreen.Child =
new BuyNowScreen
("Buy this application and get rid of the ads!");
buyNowScreen.IsOpen = true;
buyNowScreen.VerticalOffset = 100;
buyNowScreen.HorizontalOffset = 25;
buyNowScreen.Closed += (s1, e1) =>
{
// Add you code here to do something
// when the Popup is closed
};
}
DatePicker in the popup's xaml file:
<toolbox:DatePicker x:Name="DatePick" Height="Auto" HorizontalAlignment="Center" Margin="0,0,0,0" VerticalAlignment="Top" Width="400" IsEnabled="True"/>

With Events:
Add a button in your PopUp with the DatePicker. When the button is clicked have an event to close the PopUp
XAML
<toolbox:DatePicker x:Name="DatePick" Height="Auto" HorizontalAlignment="Center" Margin="0,0,0,0" VerticalAlignment="Top" Width="400" IsEnabled="True"/>
<Button Click="DateAcceptedClick"/>
CodeBehind for Popup
public event DateAcceptedButtonClick DateAcceptedButtonEvent;
public delegate void DateAcceptedButtonClick (object sender, RoutedEventArgs e);
private void DateAcceptedClick(object sender, RoutedEventArgs e)
{
if (DateAcceptedButtonEvent!= null)
DateAcceptedButtonEvent(sender, e);
}
CodeBehind for Popup Parent
Popup BuyNowScreen;
private void btnShowBuyNow_Click(object sender, RoutedEventArgs e)
{
BuyNowScreen = new Popup();
BuyNowScreen.Child = new BuyNowScreen("Buy this application and get rid of the ads!");
BuyNowScreen.IsOpen = true;
BuyNowScreen.VerticalOffset = 100;
BuyNowScreen.HorizontalOffset = 25;
BuyNowScreen.DateAcceptedButtonEvent += new DateAcceptedButtonClick(PopupDateAcceptedButtonClick)
}
private void PopupDateAcceptedButtonClick(object sender, RoutedEventArgs e)
{
BuyNowScreen.IsOpen = false;
//Closed logic
}

Related

How to Trigger event Cancel Button on SearchBar Xamarin Form

Xamarin Form View Model can trigger the onTextChange Event for Searchbar but there is no Event handler for OnCancelButtonClicked.
What I want:
An Event should be Triggered whenever Cancel/Close Button is clicked as below.
You can get Searchbar CloseButton event in SearchBar custom render, but I think it is not useful for your goal.
I suggest you can click search icon to refresh data source. I don one sample using Searchbar and ListView, you can take a look:
[assembly: ExportRenderer(typeof(SearchBar), typeof(CustomSearchBarRenderer))]
namespace FormsSample.Droid
{
public class CustomSearchBarRenderer: SearchBarRenderer
{
public CustomSearchBarRenderer(Context context):base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<SearchBar> e)
{
base.OnElementChanged(e);
if (Control != null)
{
var searchView = Control;
searchView.Iconified = true;
searchView.SetIconifiedByDefault(false);
int searchCloseButtonId = Context.Resources.GetIdentifier("android:id/search_close_btn", null, null);
// search close button icon, you can add event for closeIcon.click.
var closeIcon = searchView.FindViewById(searchCloseButtonId);
int searchViewSearchButtonId = Control.Resources.GetIdentifier("android:id/search_mag_icon", null, null);
var searchIcon = searchView.FindViewById(searchViewSearchButtonId);
searchIcon.Click += SearchIcon_Click;
}
}
private void SearchIcon_Click(object sender, EventArgs e)
{
Element.OnSearchButtonPressed();
}
}
}
<StackLayout>
<SearchBar
x:Name="searchBar"
HorizontalOptions="Fill"
Placeholder="Search fruits..."
SearchButtonPressed="OnSearchButtonPressed"
VerticalOptions="CenterAndExpand" />
<Label
HorizontalOptions="Fill"
Text="Enter a search term and press enter or click the magnifying glass to perform a search."
VerticalOptions="CenterAndExpand" />
<ListView
x:Name="searchResults"
HorizontalOptions="Fill"
VerticalOptions="CenterAndExpand" />
</StackLayout>
public partial class Page23 : ContentPage
{
public Page23()
{
InitializeComponent();
searchResults.ItemsSource = DataService.Fruits;
}
private void OnSearchButtonPressed(object sender, EventArgs e)
{
if(string.IsNullOrEmpty(searchBar.Text))
{
searchResults.ItemsSource = DataService.Fruits;
}
else
{
searchResults.ItemsSource = DataService.GetSearchResults(searchBar.Text);
}
}
}
You can click search closebutton to move text in SearchBar firstly, then click SearchButton to refresh data for listView.

Xamarin Forms: How to add single and double tap for an image?

I am trying to implement a keypad UI. In the keypad, the +(plus) and 0(zero) options are placed on a single image icon. So I need to show 0 on UI for single tap and for double tap I need to show the + on UI.
My Code
<Image
Grid.Column="1"
Source="ic_zero_xx.png"
Style="{StaticResource KeypadImageStyle}">
<Image.GestureRecognizers>
<TapGestureRecognizer
Tapped="ZeroTapped"
NumberOfTapsRequired="1">
</TapGestureRecognizer>
</Image.GestureRecognizers>
</Image>
private void ZeroTapped(object sender, EventArgs e)
{
phonenumber_label.Text = "0";
}
How I can mention 2 different taps at the same time on an image?
We can add two TapGestureRecognizer on that image, one for single tap and the other for double tap .
xaml
<Image >
<Image.GestureRecognizers>
<TapGestureRecognizer Tapped="TapGestureRecognizer_Single" NumberOfTapsRequired="1"/>
<TapGestureRecognizer Tapped="TapGestureRecognizer_Double" NumberOfTapsRequired="2"/>
</Image.GestureRecognizers>
</Image>
Code behind
private void TapGestureRecognizer_Single(object sender, EventArgs e)
{
label.Text = "0";
}
private void TapGestureRecognizer_Double(object sender, EventArgs e)
{
label.Text = "+";
}
I think you can use a timer If user tap twice within 150ms then it is a double tap.
int Tapped=0;
private void ZeroTapped(object sender,EventArgs e)
{
if(Tapped==0)
{
Device.StartTime(TimeSpan.FromMillSeconds(150),()=>
{
if(Tapped=1)
{
//1 tap.
}
else
{
//double tap.
}
Tapped=0;
return false;
}
}
else
{
Tapped++;
}
}

Do A on tabbedpage children button click, do B when the same button is pressed for x seconds

I have spent my recent hours on researching on how to make this possible
I have a bottom tabbed page with a home button. What I want to achieve is, when button is pressed for x seconds, a new page shall be opened. How should the custom renderer for the tabbed page look like?
I already have one custom renderer for the tabbed page that makes sure that the home page button is a floating action button. The custom renderer for the home page button:
[assembly: ExportRenderer(typeof(NavigationBar), typeof(CustomTabbedRenderer))]
namespace MysteryLocation.Droid
{
public class CustomTabbedRenderer : TabbedPageRenderer
{
Context context;
public CustomTabbedRenderer(Context context) : base(context)
{
this.context = context;
}
protected override void OnElementChanged(ElementChangedEventArgs<TabbedPage> e)
{
base.OnElementChanged(e);
if (e.OldElement == null && e.NewElement != null)
{
for (int i = 0; i <= this.ViewGroup.ChildCount - 1; i++)
{
var childView = this.ViewGroup.GetChildAt(i);
if (childView is ViewGroup viewGroup)
{
((ViewGroup)childView).SetClipChildren(false);
for (int j = 0; j <= viewGroup.ChildCount - 1; j++)
{
var childRelativeLayoutView = viewGroup.GetChildAt(j);
if (childRelativeLayoutView is BottomNavigationView bottomView)
{
FloatingActionButton button = new FloatingActionButton(context);
BottomNavigationView.LayoutParams parameters = new BottomNavigationView.LayoutParams(150, 150);
parameters.Gravity = GravityFlags.CenterHorizontal;
Drawable d = Resources.GetDrawable(Resource.Drawable.image);
button.SetScaleType(Android.Widget.ImageView.ScaleType.Center);
button.SetImageDrawable(d);
button.LayoutParameters = parameters;
bottomView.AddView(button);
}
}
}
}
}
}
}
}
==========
EDIT
==========
In my customtabbedrenderer I've added button.LongClick += Button_LongClick and button.Click += Button_Click with the methods
private void Button_LongClick(object sender, EventArgs e)
{
//open a page through pushasync
}
private void Button_Click(object sender, EventArgs e)
{
//navigate to the page as usual, as it does when there's no
"button.Click" specified in the custom renderer
}
Any ideas on how the code should look like in the given methods?
=========
EDIT 2
=========
I have solved on long click, a new page is being opened through
button.LongClick += (object sender, LongClickEventArgs args) =>
{
Application.Current.MainPage.Navigation.PushModalAsync(new NavigationPage(new Pass()));
};
The app works both on click and on long click now. The remaining problem is, how can I allow the same button to navigate to the page that it is supposed to when only doing a simple click? It navigates to that page when there's no "button.Click += Button_Click" and "button.LongClick += Button_Click1". But as soon as I add a longclick event, the button stops working on click.
==========================
SOLVED
==========================
Added Element.CurrentPage = Element.Children[2] in the method Button_Click method, simple as that.
Longpress may be problematic in XF. For one of my applications I used a double tap:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core"
xmlns:CustomCtrls ="clr-namespace:D4503.CustomControls"
xmlns:Views="clr-namespace:D4503.Views"
x:Class="D4503.Pages.MainPage"
BackgroundColor="{StaticResource ScreenBackColor}"
ios:Page.UseSafeArea="true">
<ContentPage.Content>
<StackLayout>
<Image Grid.Row="1" Grid.Column="0" x:Name="btnRed" Style="{StaticResource ColorButtonImage}" Source="stoplight_red.png"
Aspect="AspectFit">
<Image.GestureRecognizers>
<TapGestureRecognizer Tapped="Red_Button_Tapped"/>
<TapGestureRecognizer Tapped="Red_Button_Tapped_Double" NumberOfTapsRequired="2" />
</Image.GestureRecognizers>
</Image>
</StackLayout>
</ContentPage.Content>
</ContentPage>

how to use listpicker selectionChanged event in wp7

Am new to Wp7, developing the app with ListPicker and used SelectionChanged event to get selected data form listPicker but am having the problem with SelectionChanged event when am using this and getting NullReferenceException but when i used same code in button_Click that works perfectly and am getting the selected text
my c# code is :
private void listPicker1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListPickerItem lpi = (ListPickerItem)listPicker1.SelectedItem;//this code is working in click event
MessageBox.Show(lpi.Content.ToString());
}
my Xaml code is
<toolkit:ListPicker x:Name="listPicker1" Grid.Row="0" ExpansionMode="ExpansionAllowed" SelectionChanged="listPickerCountryLogin_SelectionChanged" HorizontalAlignment="Left" Margin="14,43,0,0" VerticalAlignment="Top" Width="436" FullModeHeader="Select Country" Background="White" BorderBrush="White">
<toolkit:ListPicker.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Country}" Width="250" />
</StackPanel>
</DataTemplate>
</toolkit:ListPicker.ItemTemplate>
<toolkit:ListPicker.FullModeItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Country}" Width="300" Margin="0,0,0,20" FontSize="44"/>
</StackPanel>
</DataTemplate>
</toolkit:ListPicker.FullModeItemTemplate>
</toolkit:ListPicker>
but i want to get the text form SelectionChanged event?
how to achive this :)
thanks in advance
am also ran into same problem am also getting NullReferenceException
Try this works fine for me
1) If you are using static ListPickerItems means without DataBinding use this
private void listPicker1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListPickerItem lpi = (sender as ListPicker).SelectedItem as ListPickerItem;
MessageBox.Show("selected item is : " + lpi.Content);
}
2) Try this if You are using DataBinding to dispaly the listPickerItems
private void listPicker1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Countries item = (sender as ListPicker).SelectedItem as Countries;
MessageBox.Show("Selected Item is : " + item.Country);
}
here am assuming that you prepared a class Countries with country property for taht you need to typeCast to selected item to Countries class then only you get the result
I had this same problem when using SelectionChanged event in my listpicker, here is what I had:
// My listpicker is LpBluetoothPaired
private void LpBluetoothPaired_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
rcvData.Text = LpBluetoothPaired.SelectedItem.ToString();
}
But when opening the app it had an exception, so I fixed it:
private void LpBluetoothPaired_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (LpBluetoothPaired.SelectedItem != null) {
rcvData.Text = LpBluetoothPaired.SelectedItem.ToString();
}
}
it looks like the event is called when the application is opening but at that time there is still no selectedItem, so in order to avoid the exception and only fulfill the rcvData textBox I check if it isn''t null
Sometimes SelectionChanged event fires, when setting the ItemsSource from code behind. So, in such case the selectedItem may be null.
Hence, add this line in your SelectionChanged code and try.
private void listPicker1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if(listPicker1.SelectedIndex == -1) //otherwise (listPicker1.SelectedItem == null) also works
return;
ListPickerItem lpi = (ListPickerItem)listPicker1.SelectedItem;//this code is working in click event
MessageBox.Show(lpi.Content.ToString());
}
If still problem persists, place a break point in the SelectionChanged handler, and observe the values
When data is loading to the listpicker it fires selectionchanged event.So for this put your code in Try-catch and For selectionchanged write this :
private void listPicker1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
try
{
Countries SelectedCountries =e.AddedItems[0] as Countries;
String SelectedCountry = SelectedCountries.Country;
}
catch
{
}
}

MY web Broswer doesn't support silver light toolkit tap event and flick event

<controls:PivotItem Header="item1">
<Grid>
<phone:WebBrowser IsHitTestVisible="False" Source="www.google.com">
<toolkit:GestureService.GestureListener>
<toolkit:GestureListener Tap="onTap" Flick="GestureListener_Flick" />
</toolkit:GestureService.GestureListener>
</phone:WebBrowser>
</Grid>
</controls:PivotItem>
the code behind this
private void onTap(object sender, GestureEventArgs e)
{
MessageBox.Show("tap");
// Listbox.index = m.SelectedIndex;
// m.SelectedIndex = -1;
// App.Navigate(new Uri("/Home.xaml", UriKind.Relative));
}
private void GestureListener_Flick(object sender, FlickGestureEventArgs e)
{
if (e.Direction == System.Windows.Controls.Orientation.Horizontal)
{
if (e.HorizontalVelocity < 0)
{
MessageBox.Show("right");
}
else
{
MessageBox.Show("left");
}
}
else
{
if (e.VerticalVelocity < 0)
{
MessageBox.Show("up");
}
else
{
MessageBox.Show("down");
}
}
}
but it doesn't work ...
thanx in advance..
These events are not supported on the WebBrowser control. The main reason for this is probably that it's hard to distinguish whether the user is interacting with a webpage (scrolling or clicking a link) or whether you would like to capture the events on the control.

Resources