Windows Phone 8 Set Navigate URI - c#

I'm setting up a series of buttons, one for each item in a table. What I need to do is set the Navigate parameter for each button, is there anyway I can set the following from the .cs code?:
<ec:NavigateToPageAction TargetPage="/MissionPage.xaml"/>
Here's the code I'm using to make the buttons:
foreach (string i in missionQ)
{
Button btn = new Button() { Content = "Run", Width=120, Height=90 };
btn.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
btn.VerticalAlignment = System.Windows.VerticalAlignment.Top;
btn.Margin = new Thickness(0, (100*x), 20, 0); }

You could try this code in your Click event of the button
using System.Windows.Navigation;
private void Button_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/MissionPage.xaml.xaml", UriKind.Relative));
}
Ref: NavigationService Class

The way I would handle this is by, creating a new eventhandler for the tap event on each button. Pass i as reference to the function.
btn.Tap += functionToHandleTap(i);
In the function it self I would create a switch or if statement and then navigate based on the i parameter passed to funtionToHandleTap.
private void functionToHandleTap(string i)
{
string naviString = string.Empty;
if (i == "something")
{
naviString = "some xaml here";
}
else
{
naviString = "another xaml here";
}
_rootFrame.Navigate(new Uri(naviString, UriKind.Relative));
}

Related

How to create click event for dynamically created user controls in C#?

I'm creating a Windows Universal App which contains a ListView filled with User-Controls. User-Controls are added to ListView dynamically during runtime, based on the elements from the database.
public void ShowFavorites()
{
using (SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), (Application.Current as App).DBPath))
{
var Favorites = conn.Table<Favorites>();
lvFavorites.Items.Clear();
foreach (var fav in Favorites)
{
FavoriteItem favItem = new FavoriteItem();
favItem.Favorite = fav;
lvFavorites.Items.Add(favItem);
}
}
}
So how can i create an event that that triggers when the user-control is pressed?
When you create the control, all you need to do is simply link the control to a new event:
// Dynamically set the properties of the control
btn.Location = new Point((lbl.Width + cmb.Width + 17), 5);
btn.Size = new System.Drawing.Size(90, 23);
btn.Text = "Add to Table";
// Create the control
this.Controls.Add(btn);
// Link it to an Event
btn.Click += new EventHandler(btn_Click);
Then, when you (in this case) click on the newly added button, it will call your btn_Click method:
private void btn_Click(object sender, EventArgs e)
{
//do stuff...
}
I got it working even for items with diffrent text etc.
I gonna explain it based on a button beeing added to a panel.
You need to have a List<> in which you store the items, in this case buttons.
List<Button> BtList = new List<Button>();
and I also have a Panel in this case.
Panel PanelForButtons = new Panel();
Here is my code I hope it helps you:
void AddItemToPanel()
{
//Creating a new temporary item.
Button TempBt = new Button();
TempBt.Text = "Hello world!";
//Adding the button to our itemlist.
BtList.Add(TempBt);
//Adding the event to our button.
//Because the added item is always the last we use:
PanelForButtons.Controls.Add(BtList.Last());
BtList.Last().Click += MyButtonClicked;
}
And here is the event:
void MyButtonClicked(object sender, EventArgs e)
{
//First we need to convert our object to a button.
Button ClickedButton = sender as Button;
//And there we have our item.
//We can change the text for example:
ClickedButton.Text = "The world says: \"Hello!\"";
}

C# Dynamic button not firing click event

It reloads the page empty when I click the button. How do I fire click event on button click? I think Page.IsPostBack is the reason it reloads the page empty instead of showing the label.
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
account account = new account();
accountManager accountManager = new accountManager();
group group = new group();
groupManager groupManager = new groupManager();
string emailAddress;
emailAddress = HttpContext.Current.User.Identity.Name;
account = accountManager.getAccInfoByEmailAddress(emailAddress);
group = groupManager.getGroupLeader(account.groupNo);
if (account.groupNo == 0)
{
divMessage.InnerHtml = "You are not in any group.";
}
else
{
try
{
Button btn = new Button();
btn.Text = "Click";
btn.Click += new EventHandler(button_Click);
form1.Controls.Add(btn);
}
catch (Exception)
{
divMessage.InnerHtml = "Unable to retrieve data. Please contact administrator if the problem persists.";
}
}
}
}
.
private void button_Click(object sender, EventArgs e)
{
Label Label1 = new Label();
Label1.Text = "rthfg";
form1.Controls.Add(Label1);
}
When you click the button, or somehow else generate a postback, ASP.NET creates the page (as it always does) and tries to find the source of the request, that is the button you clicked. In your case this button is no longer on the page, so ASP.NET cannot find anything, end does not fire the event.
Resolution seems easy enough in your case - just always create the button and put it on the page, regardless of the postback:
if (!Page.IsPostBack)
{
...
}
Button btn = new Button();
btn.Text = "Click";
btn.Click += new EventHandler(button_Click);
form1.Controls.Add(btn);
Btw, why make the button dynamic? Dynamic controls are always harder to manage.

Button with image and not visible text properties

Can I create a button with both .Image and .Text properties simultaniously, in such way, that text is not visible on form, and is created just for identifying what button should do at the moment?
Using TextAlign and TextImageRelation properties doesn't help. Text is always visible, just a position changes.
private System.Windows.Forms.Button bRenameCourse;
this.bRenameCourse.BackColor = System.Drawing.SystemColors.ButtonFace;
this.bRenameCourse.Image = ((System.Drawing.Image)(resources.GetObject("bRenameCourse.Image")));
this.bRenameCourse.Location = new System.Drawing.Point(966, 6);
this.bRenameCourse.Name = "bRenameCourse";
I want this text "Rename" to be not visible on button
this.bRenameCourse.Text = "Rename";
this.bRenameCourse.Size = new System.Drawing.Size(64, 60);
this.bRenameCourse.TabIndex = 10;
this.bRenameCourse.UseVisualStyleBackColor = false;
this.bRenameCourse.Click += new System.EventHandler(this.bRenameCourse_Click);
Here is why do I want it works :
private void bRenameCourse_Click(object sender, EventArgs e)
{
if (bRenameCourse.Text.Equals("Rename"))
{
//DO SMTHNG
bRenameCourse.Text = "OK";
}
else if (bRenameCourse.Text.Equals("OK"))
{
//DO SMTHNG
bRenameCourse.Text = "Rename";
}
}
I can avoid this using some flags, but I'd like to know if it's possible in general.
Don't use the .Text property of the button to store information.You can use the .Tag property
ie
this.bRenameCourse.Tag = "Rename";
And in the Event
private void bRenameCourse_Click(object sender, EventArgs e)
{
if (bRenameCourse.Tag.Equals("Rename"))
{
//DO SMTHNG
bRenameCourse.Tag = "OK";
}
else if (bRenameCourse.Tag.Equals("OK"))
{
//DO SMTHNG
bRenameCourse.Tag = "Rename";
}
}
Just set the .Text property to ""(blank or empty)

Dynamically added ImageButton Command event not firing until second click

I have a page where I'm adding ImageButtons dynamically. I first set the OnClientClick of the buttons to simply show a popup of the image enlarged and return false for no postback.
I have a button on the page to set the "primary image" so when this button is clicked I set a property called _IsSettingPrimaryPhotoMode = true, call the function to recreate the ImageButtons, and when creating the ImageButtons if this property is true instead of adding an OnClientClick, I hook up a CommandEventHandler so I can tell which button was clicked by reading the CommandArgument.
The problem is the event handler won't fire on the first click of the image but only on the second click and thereafter. I've also moved the code from Page_Load to OnInit and I load the ImageButtons on every postback.
I save the _IsSettingPrimaryPhotoMode to the Session.
private bool _IsSettingPrimaryPhotoMode {
get {
bool result = false;
if(Session[ConstantsWeb.Session.IS_DELETE_IMAGE_MODE] != null) {
result = Convert.ToBoolean(Session[ConstantsWeb.Session.IS_SETTING_PRIMARY_IMAGE_MODE]);
}
return result;
}
set {
Session[ConstantsWeb.Session.IS_SETTING_PRIMARY_IMAGE_MODE] = value;
}
}
The page OnInit
protected override void OnInit(EventArgs e) {
base.OnInit(e);
if(!IsPostBack) {
_IsSettingPrimaryPhotoMode = false;
}
_LoadGalleryImages();
}
}
The _LoadGalleryImages method
private void _LoadGalleryImages() {
PhotoGalleryImageCollection images = PhotoGalleryImages.GetPhotoGalleryImages();
foreach(PhotoGalleryImage image in images) {
ImageButton displayImage = new ImageButton();
Panel panel = new Panel();
panelPhotoContainer.Controls.Add(panel);
displayImage.ImageUrl = "some URL";
if(!_IsSettingPrimaryPhotoMode) {
displayImage.OnClientClick = "showPopup(); return false;";
}
else {
displayImage.Command += new CommandEventHandler(displayImage_Command);
displayImage.CommandName = "ImageButton" + image.PhotoGalleryImageId.ToString();
displayImage.CommandArgument = image.PhotoGalleryImageId.ToString();
}
panel.Controls.Add(displayImage);
}
}
btnSetPrimaryPhoto_Click
protected void btnSetPrimaryPhoto_Click(object sender, EventArgs e) {
// if I don't call this, duplicate controls will be added since they were added
// from OnInit calling _LoadGalleryImages();
panelPhotoContainer.Controls.Clear();
_IsSettingPrimaryPhotoMode = true;
// reload since _IsSettingPrimaryPhotoMode has now changed
_LoadGalleryImages();
}
What am I doing wrong?
I suppose the issue can be caused by the fact you haven't set IDs of the dynamically created controls. It's quite important, as it's used in the process of firing the post back events. Value of the ID for each of the controls should be constant and not change between postbacks.
You can try to modify your _LoadGalleryImages() method like this:
private void _LoadGalleryImages() {
PhotoGalleryImageCollection images = PhotoGalleryImages.GetPhotoGalleryImages();
int imageCtrlCounter = 0;
foreach(PhotoGalleryImage image in images) {
ImageButton displayImage = new ImageButton() { ID = String.Format("myDisplayImage{0}", imageCtrlCounter) };
Panel panel = new Panel();
panelPhotoContainer.Controls.Add(panel);
displayImage.ImageUrl = "some URL";
if(!_IsSettingPrimaryPhotoMode) {
displayImage.OnClientClick = "showPopup(); return false;";
}
else {
displayImage.Command += new CommandEventHandler(displayImage_Command);
displayImage.CommandName = "ImageButton" + image.PhotoGalleryImageId.ToString();
displayImage.CommandArgument = image.PhotoGalleryImageId.ToString();
}
panel.Controls.Add(displayImage);
imageCtrlCounter++;
}
}
It has to be something related to the initial event wireup because the problem only happens the first time. What's different about that time is that _LoadGalleryImages is called twice (once in the init and once in the button click event handler) so I think that something is not getting cleaned up there when you clear the container panel and call _LoadGalleryImages again in the button's click handler.
Why not try this alternative:
Only call LoadImageGalleries once per page cycle (in the init).
Instead of clearing the controls and calling LoadGalleryImages again in the same postback (in the button click event), on the button click call a method that iterates through the image controls you already created when LoadGalleryImages was called and adjust them:
1) Remove the onclientclick (clear it out).
2) Attach the event.
#swannee
Actually, your method did work after I thought more about it. I do now call _LoadGalleryImages on every OnInit. I realize this is a lot of duplicate code that could be consolidated.
New _LoadGalleryImages
private void _LoadGalleryImages() {
PhotoGalleryImageCollection images = PhotoGalleryImages.GetPhotoGalleryImages();
foreach(PhotoGalleryImage image in images) {
Panel panel = new Panel();
panelPhotoContainer.Controls.Add(panel);
ImageButton displayImage = new ImageButton();
panel.Controls.Add(displayImage);
displayImage.ID = string.Format("ImageButton{0}", image.PhotoGalleryImageId);
displayImage.ImageUrl = "Some URL";
displayImage.AlternateText = displayImage.ToolTip = image.ImageName;
if(!_IsSettingPrimaryPhotoMode) {
displayImage.OnClientClick = "showPopup(); return false;";
}
else {
// handles the image button command wireup
displayImage.Command += new CommandEventHandler(displayImage_Command);
displayImage.CommandArgument = image.PhotoGalleryImageId.ToString();
}
}
}
I added a new method as you suggested to find the controls since they've already been created in OnInit and I just need to find them after the button click and clear the OnClientClick.
private void _LoadSelectPrimaryImages() {
PhotoGalleryImageCollection images = PhotoGalleryImages.GetPhotoGalleryImages();
foreach(PhotoGalleryImage image in images) {
Control control = panelPhotoContainer.FindControl(string.Format("ImageButton{0}", image.PhotoGalleryImageId));
if(control != null) {
ImageButton displayImage = (ImageButton)control;
displayImage.OnClientClick = "";
}
}
}
I also have a cancel button to return the image buttons to the way they were before to show the popup.
private void _ResetGalleryImages() {
PhotoGalleryImageCollection images = PhotoGalleryImages.GetPhotoGalleryImages(_photoGalleryId, false, true);
foreach(PhotoGalleryImage image in images) {
Control control = panelPhotoContainer.FindControl(string.Format("ImageButton{0}", image.PhotoGalleryImageId));
if(control != null) {
ImageButton displayImage = (ImageButton)control;
displayImage.ImageUrl = "Original URL";
displayImage.OnClientClick = "showPopup(); return false;";
}
}
}
and two page button clicks
protected void btnSetPrimaryPhoto_Click(object sender, EventArgs e) {
_IsSettingPrimaryPhotoMode = true;
_LoadSelectPrimaryImages();
}
protected void btnCancelSetPrimaryPhoto_Click(object sender, EventArgs e) {
_IsSettingPrimaryPhotoMode = false;
_ResetGalleryImages();
}
Someone said in a response earlier...looks like the response was removed...to clear the controls in _LoadGalleryImages such as:
private void _LoadGalleryImages() {
panelPhotoContainer.Controls.Clear();
PhotoGalleryImageCollection images = PhotoGalleryImages.GetPhotoGalleryImages();
foreach(PhotoGalleryImage image in images) {
ImageButton displayImage = new ImageButton();
Panel panel = new Panel();
panelPhotoContainer.Controls.Add(panel);
displayImage.ImageUrl = "some URL";
if(!_IsSettingPrimaryPhotoMode) {
displayImage.OnClientClick = "showPopup(); return false;";
}
else {
displayImage.Command += new CommandEventHandler(displayImage_Command);
displayImage.CommandName = "ImageButton" + image.PhotoGalleryImageId.ToString();
displayImage.CommandArgument = image.PhotoGalleryImageId.ToString();
}
panel.Controls.Add(displayImage);
}
}
which also works but I think that may be more inefficient than your method, #swannee. Thanks!

WPF how do I create a textbox dynamically and find the textbox on a button click?

I am creating a TextBox and a Button dynamically using the following code:
Button btnClickMe = new Button();
btnClickMe.Content = "Click Me";
btnClickMe.Name = "btnClickMe";
btnClickMe.Click += new RoutedEventHandler(this.CallMeClick);
someStackPanel.Childern.Add(btnClickMe);
TextBox txtNumber = new TextBox();
txtNumber.Name = "txtNumber";
txtNumber.Text = "1776";
someStackPanel.Childern.Add(txtNumber);
I hook up to a click event to the Click Me button. The click me button even is fired correctly. However I cannot find the TextBox I entered dynamically.
Here is my click me event:
protected void ClickMeClick(object sender, RoutedEventArgs e)
{
// Find the phone number
TextBox txtNumber = this.someStackPanel.FindName("txtNumber") as TextBox;
if (txtNumber != null)
{
string message = string.Format("The number is {0}", txtNumber.Text);
MessageBox.Show(message);
}
else
{
MessageBox.Show("Textbox is null");
}
}
How can I find the TextBox txtNumber?
Josh G had the clue that fixed this code: use RegisterName().
Three benefits here:
Doesn't use a member variable to save the reference to the dynamically created TextBox.
Compiles.
Complete code.
using System;
using System.Windows;
using System.Windows.Controls;
namespace AddControlsDynamically
{
public partial class Window1 : Window
{
public void Window_Loaded(object sender, RoutedEventArgs e)
{
GenerateControls();
}
public void GenerateControls()
{
Button btnClickMe = new Button();
btnClickMe.Content = "Click Me";
btnClickMe.Name = "btnClickMe";
btnClickMe.Click += new RoutedEventHandler(this.CallMeClick);
someStackPanel.Children.Add(btnClickMe);
TextBox txtNumber = new TextBox();
txtNumber.Name = "txtNumber";
txtNumber.Text = "1776";
someStackPanel.Children.Add(txtNumber);
someStackPanel.RegisterName(txtNumber.Name, txtNumber);
}
protected void CallMeClick(object sender, RoutedEventArgs e)
{
TextBox txtNumber = (TextBox) this.someStackPanel.FindName("txtNumber");
string message = string.Format("The number is {0}", txtNumber.Text);
MessageBox.Show(message);
}
}
}
Another method is to set the associated TextBox as Button Tag when instanciating them.
btnClickMe.Tag = txtNumber;
This way you can retrieve it back in event handler.
protected void ClickMeClick(object sender, RoutedEventArgs e)
{
Button btnClickMe = sender as Button;
if (btnClickMe != null)
{
TextBox txtNumber = btnClickMe.Tag as TextBox;
// ...
}
}
You can get your original click handler to work by registering the name of the text box:
someStackPanel.RegisterName(txtNumber.Name, txtNumber);
This will then allow you to call FindName on the StackPanel and find the TextBox.
If you want to do a comprehensive search through the visual tree of controls, you can use the VisualTreeHelper class.
Use the following code to iterate through all of the visual children of a control:
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parentObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(parent, i);
if (child is TextBox)
// Do something
}
If you want to search down into the tree, you will want to perform this loop recursively, like so:
public delegate void TextBoxOperation(TextBox box);
public bool SearchChildren(DependencyObject parent, TextBoxOperation op)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(parent, i);
TextBox box = child as TextBox;
if (box != null)
{
op.Invoke(box);
return true;
}
bool found = SearchChildren(child, op);
if (found)
return true;
}
}
Is there any way you can make the TextBox control a field in your class instead of a variable inside your generator method
public class MyWindow : Window
{
private TextBox txtNumber;
public void Window_Loaded()
{
GenerateControls();
}
public void GenerateControls()
{
Button btnClickMe = new Button();
btnClickMe.Content = "Click Me";
btnClickMe.Name = "btnClickMe";
btnClickMe.Click += new RoutedEventHandler(this.CallMeClick);
someStackPanel.Childern.Add(btnClickMe);
txtNumber = new TextBox();
txtNumber.Name = "txtNumber";
txtNumber.Text = "1776";
someStackPanel.Childern.Add(txtNumber);
}
protected void ClickMeClick(object sender, RoutedEventArgs e)
{
// Find the phone number
string message = string.Format("The number is {0}", txtNumber.Text);
MessageBox.Show(message);
}
}

Categories

Resources