Button command in windows phone 7 - windows-phone-7

I am trying to use code the button click command programmatically but i am not sure how to do it.
I want something like when click on the button it will do something.
Below is my code for the button:
HyperlinkButton viewButton = new HyperlinkButton();
viewButton.Margin = new Thickness(-150, 20, 0, 0);
viewButton.Width = 100;
viewButton.Height = 50;
viewButton.Name = songTitle;
viewButton.Background = new ImageBrush { ImageSource = new BitmapImage(new Uri("/AlarmClock;component/Images/music_note.png", UriKind.Relative)) };
viewButton.FontSize = 30;

It is going to be a standard event handler:
viewButton.Click += new RoutedEventHandler(viewButton_Click);
private void viewButton_Click(object sender, RoutedEventArgs e)
{
// Action here.
}

Related

windows phone 7.. play or pause the music

i am developing game in windows phone 7. i want to add a button on homepage which allow to toggle between images.
i wrote following code but doesnt work
int key = 1;
switch (key)
{
case 1:
var brush = new ImageBrush();
BitmapImage image = new BitmapImage(new Uri(#"Assets/small/misc/music pause button.png", UriKind.Relative));
brush.ImageSource = image;
music.Background = brush;
key=0;
break;
case 0:
var brush2 = new ImageBrush();
BitmapImage image2 = new BitmapImage(new Uri(#"Assets/small/misc/music pause button.png", UriKind.Relative));
brush2.ImageSource = image2;
music.Background = brush2;
key = 1;
break;
}
solved this by using togglebutton
in xaml there is toggle button control
<ToggleButton Name="tog" Margin="555,358,0,7" IsChecked="{x:Null}" Checked="tog_Checked" Unchecked="tog_Unchecked" Background="{x:Null}" BorderBrush="{x:Null}" BorderThickness="0" IsThreeState="False" HorizontalAlignment="Left" Width="123"></ToggleButton>
now add code on event handlers :
private void tog_Checked(object sender, RoutedEventArgs e)
{
tog.Background = brush;
togkey = 1;
System.Diagnostics.Debug.WriteLine("1");
}
private void tog_Unchecked(object sender, RoutedEventArgs e)
{
tog.Background = null;
togkey = 0;
System.Diagnostics.Debug.WriteLine("0");
}

SL5 - OOB - animating the main window

I need to grow the size of my Application.Current.MainWindow (when running OOB, of course).
Is there any way to do it in a Storyboard?
I can do it programatically:
private long timerCntr = 0;
private void OpenBtn_OnClick(object sender, RoutedEventArgs e)
{
timerCntr = 0;
DispatcherTimer t = new DispatcherTimer();
t.Interval = new TimeSpan(0, 0, 0, 0, 10);
t.Tick += (o, args) =>
{
if (timerCntr < 100)
{
Application.Current.MainWindow.Height += 1;
userControl.Height += 1;
LayoutRoot.Height += 1;
++timerCntr;
}
else
{
t.Stop();
}
};
t.Start();
OpenStory.Begin(); // Controls opacity of object becoming visible
}
Thanks for any insights...
The link in comments provided the answer.
Basically, add a hidden slider control; change the slider's value in a storyboard; in the slider's value_changed handler change the window size.
Works great.

How to handle list picker in Wp7

I have a list picker which is displayed in my phone application page.I have created list picker in starting of class,and i am adding the list picker in the phoneApplicationPage_loaded() method.When the page is launched the first time, ,the scenario works perfectly and its navigates further to second page.When i navigate back to previous page(containing list picker),it shows Invalid Operation Exception occured stating "Element is already the child of another element."
I want to know how to handle these scenarios?
Code is below
namespace My.Design
{
public partial class myclass : PhoneApplicationPage
{
String[] values = null;
ListPicker picker = new ListPicker();
StackPanel sp;
StackPanel mainFrame;
String statementInfo = "";
public myclass()
{
InitializeComponent();
}
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
Debug.WriteLine("Phone Application Page Loaded_>>>>>>");
List<String> source = new List<String>();
displayUI();
}
public void displayUI()
{
Debug.WriteLine("About to display UI in miniStatement");
Debug.WriteLine("<-------------Data--------->");
Debug.WriteLine(statementInfo);
Debug.WriteLine("<-------------Data--------->");
int count = VisualTreeHelper.GetChildrenCount(this);
if (count > 0)
{
for (int i = 0; i < count; i++)
{
UIElement child = (UIElement)VisualTreeHelper.GetChild(this, i);
string childTypeName = child.GetType().ToString();
Debug.WriteLine("Elements in this Child" + childTypeName);
}
}
List<String> source = new List<String>();
String[] allParams = ItemString.Split('#');
source.Add("PleaseSelect");
for (int i = 0; i < allParams.Length; i++)
{
Debug.WriteLine("All Params Length" + allParams[i]);
if (!(allParams[i].Equals("") && (!allParams[i].Equals(null))))
{
if (values != null)
{
Debug.WriteLine("Values length" + values.Length);
values[values.Length] = allParams[i];
}
else
{
Debug.WriteLine("Allparams Length" + allParams[i]);
source.Add(allParams[i]);
}
}
}
//picker = new ListPicker();
this.picker.ItemsSource = source;
mainFrame = new StackPanel();
TextBlock box = new TextBlock();
box.Text = "> DEmoClass";
box.FontSize = 40;
mainFrame.Children.Add(box);
Canvas canvas = new Canvas();
StackPanel sp = new StackPanel();
TextBlock box1 = new TextBlock();
box1.Text = "Number";
box1.HorizontalAlignment = HorizontalAlignment.Center;
box1.FontSize = 40;
SolidColorBrush scb1 = new SolidColorBrush(Colors.Black);
box1.Foreground = scb1;
sp.Children.Add(box1);
picker.Width = 400;
picker.Height = 150;
sp.Children.Add(picker);
Canvas.SetTop(sp, 150);
canvas.Children.Add(sp);
mainFrame.Children.Add(canvas);
this.ContentPanel1.Children.Add(mainFrame);
}
protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
{
/*
Debug.WriteLine("OnNavigatingFrom>>>.>>MainPage");
if (sp != null)
{
sp.Children.Remove(picker);
}*/
base.OnNavigatingFrom(e);
}
}
}
If you are not intending to update the listpicker after navigating back from the second page add the following line in your Loaded event handler
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
this.Loaded -= PhoneApplicationPage_Loaded;
Debug.WriteLine("Phone Application Page Loaded_>>>>>>");
List<String> source = new List<String>();
displayUI();
}
i don't know why you can not use that case when app resume from tombstoned.
error happened because when you back to your page , loaded event runs again.
by the way,
Application_Activated 's argument can tell you app resumes from tombstoned or not--.
if (e.IsApplicationInstancePreserved)
{
IsTombstoning = false;
}
else
{
IsTombstoning = true;
}
I'm curious why you're creating it in code and not leaving it in XAML? Also the error is coming from the fact that you're attempting to add it twice into a location that can probably only have a single content element. What's the higher level problem you're trying to solve?

Pause WP7 HyperlinkButton?

I'm trying to 'pause' a HyperlinkButton in a WP7 app so that the user can confirm whether or not to leave the app and follow the link. The series of the events would look like:
The user clicks the HyperlinkButton
MessageBox pops up to confirm they want to leave the app and visit this external site
If the user agrees, the webpage loads; if not, the user is returned to the app
My question is: can I get HyperlinkButton to wait for the user's response?
At the moment, I've hacked a solution as below:
<HyperlinkButton Tag="http://www.google.com/" Tap="ConfirmBeforeLoading()"/>
ConfirmBeforeLoading then prompts the user and, if they agree, it creates a new WebBrowserTask using the address in the Tag property.
This works, but it seems 'hackish'. Is there any way I can use a normal HyperlinkButton with NavigateUri and just have it wait for the user's response?
Many thanks in advance!
try this one,maybe helpfull to you,
Popup mypopup; //golbal variable
private void hyperlinkButton1_Click(object sender, RoutedEventArgs e)
{
LayoutRoot.Opacity = 0.6;
mypopup = new Popup();
Border border = new Border();
StackPanel st = new StackPanel();
TextBlock tb = new TextBlock();
tb.Text = "Visit website";
tb.FontSize = 24;
Button btnok = new Button();
btnok.Content = "Ok";
btnok.Click += new RoutedEventHandler(btnok_Click);
Button btncancel = new Button();
btncancel.Content = "Cancel";
btncancel.Click += new RoutedEventHandler(btncancel_Click);
st1.Orientation = System.Windows.Controls.Orientation.Horizontal;
st1.Children.Add(btnok);
st1.Children.Add(btncancel);
st.Children.Add(tb);
st.Children.Add(st1);
border.Child = st;
mypopup.VerticalOffset = 25;
mypopup.HorizontalOffset = 25;
mypopup.Margin = new Thickness(LayoutRoot.ActualWidth / 4, LayoutRoot.ActualHeight / 3, 0, 0);
mypopup.Child = border;
mypopup.IsOpen = true;
}
void btncancel_Click(object sender, RoutedEventArgs e)
{
LayoutRoot.Opacity = 1;
mypopup.IsOpen = false;
}
void btnok_Click(object sender, RoutedEventArgs e)
{
//here what do you want....
}
its work for me.

Camera Capture in WP7 Mango

I've recently upgraded my WP7 app to Mango and am having some problems with the camera. The code below used to work on 7.0, but on 7.1 the completed handler fires before the dialog is even shown, so I can't capture the result. After taking the photo, the phone displays "Resuming..." which it never used to do.
var dlg = new CameraCaptureTask();
dlg.Completed += (s, e) =>
{
if (e.TaskResult == TaskResult.OK) {
BitmapImage bmp = new BitmapImage();
bmp.SetSource(e.ChosenPhoto);
//var img = new Image();
//img.Source = bmp;
string caption = string.Empty;
var inputDialog = new InputPrompt()
{
Title = "Caption",
Message = "Enter caption/description for snapshot",
};
inputDialog.Completed += (ss, ee) =>
{
if (ee.PopUpResult == PopUpResult.Ok)
{
caption = ee.Result;
var snap = SnapshotBLL.AddSnapshot(recipeId, bmp, caption);
onComplete(null, new SnapshotEventArgs(snap));
}
};
inputDialog.Show();
}
};
dlg.Show();
The MSDN docs appear to show a variation of my code but I can no longer get the result of the camera capture task.
Assuming that your sample comes from a single method I wouldn't expect it to ahve worked pre Mango.
The CameraCaptureTask should be created and the callback assigned in the constructor of the page for it to work properly.
Something like:
public partial class MainPage : PhoneApplicationPage
{
private CameraCaptureTask cct = new CameraCaptureTask();
public MainPage()
{
InitializeComponent();
cct.Completed += new EventHandler<PhotoResult>(cct_Completed);
}
private void cct_Completed(object sender, PhotoResult e)
{
// Do whatever here
}
private void SomeEventHandler(object sender, RoutedEventArgs e)
{
cct.Show();
}
}
This works in both 7.0 & 7.1

Resources