I am creating a Windows Phone 8.1 App.
There are some screens on my app. On the first screen, I am navigate my screen to second screen with a parameter data. now i want to navigate to first screen from second screen with different parameter data.
Using Frame.Navigate(typeof(frame1),parameter); I can pass data from first screen to second screen (forward navigation)
Frame.Navigate(typeof(frame1), "data");
but how can I pass data from second screen to first screen (backward navigation) ?
The only solution I can think of is to declare a global variable in App.xaml.cs so that you can access it from Frame2 and set a value, then you get the value from Frame1.
public static Object navigationData
{
get;
set;
}
And from everywhere you access it using
var data = App.navigationData;
There is no easy solution. Using the Frame.GoBack method you cannot pass a parameter. Furthermore if you use NavigationHelper.GoBack you cannot pass a parameter neither. This is by desing. If you go back, you will open the view that was previous open. That is the definition of going back. You want to "restore" a previous state.
But there would be a possible solution, when you store your new data into a variable that you can access from frame1.
Can you state why you would use a different data for backwards navigation?
Another, better possibility is to use a ContentDialog. There you can add your desired ViewModel to the DataContext prior opening. After that the ContentDialog is closed you can validate the data passed to it.
ContentDialog cd = new MyContentDialog();
if (parameter != null)
{
cd.DataContext = // set context here;
}
await cd.ShowAsync();
//now lets validate or process the necessary data.
Related
In my program I have two windows, the first one being my main window with a text box and the second one having an entry field with a button to update the text box in the first window. I'm a beginner in terms of using WPF and coding in C# in general, but is there a way to pass a pointer or reference of my main window to the second window so the second window can edit the text box of my first window? Is that even the right way to think about solving this issue?
WPF assumes you are binding your forms to a ViewModel object. This object can be bound to more than one form to give you different views and capabilities, so in this case you'd bind the same ViewModel to both forms, and what is changed in your edit form will appear automatically in your main form.
Your question is a bit vague and there are many approaches to accomplishing this. MVVM as Steve Todd mentions, is one.
However, it sounds like you simply want to open the window as a dialog. In your second window's code behind, be sure your textbox has a name in XAML and then access it create and easily accessible property that gets and sets your textbox value.
public MyTextContent
{
get => this.MyTextBox.Text;
set => this.MyTextBox.Text = value;
}
You can control the return value based on conditions (such as OK or Cancel buttons) if you like by using click events. The window contains a DialogResult property. The default is false, so you will need to set this somewhere.
this.DialogResult = true; // OK
Then in your main window's code behind, create a new instance of the window, assign it's property and show it. This will need to be done during a click event of a button or some similar trigger
var myDialog = new MyDialogWindow()
{
MyTextContent = "Textbox Starting Value";
}
bool? result = myDialog.ShowDialog(); // Returns when the dialog window is closed.
if(result != null && result)
{
this.LocalTextBox.Text = myDialog.MyTextContent; // Copy the text to the main textbox.
}
Typically you do this in data context of your main window. You use IoC to pass an instance of popup notification service in the constructor and create a private reference. You call that service method that displays the popup notification where user can enter async (and await) for its response or use reactive extensions to subscribe to submit action of that button. A thing to look out for is that you can update ui only in dispatcher thread and do not forget to dispose the subscription after you have finished using the window.
I'm almost done doing a Connect4 game with VS2012 using WinForms. Everything is working well but I wanted to bring the options for the user on a dedicated Start Menu window. On that menu I have two comboBoxes I need to take the text from to use them as a value for two variables in my other form (the game window). I also have one New Game button that should call a method from my other form if that's possible (basically, I made an "Initialization()" method in my game form and I'd like it to be launched when I click the "New Game" button on the other form).
I only found tutorials that show how to do very basic things from one form to an other (such as labeling texts) but I I didn't find an answer to my specific problem.
I used this in my main form to instantiate the menu form
public FormMenu myMenu;
myMenu = new FormMenu();
What I want to do is that I could do something like this in the other form :
amountOfRows = Int32.Parse(myMenu.comboBoxRows.Text);
amountOfColumns = Int32.Parse(myMenu.comboBoxColumns.Text);
Any idea how I could do that?
I would love to see some example code so I can help see where your confusion lies. WinForms requires the other form to be instantiated.
OtherForm form = new OtherForm();
Once the form is instantiated you should be able to run code from it.
EDIT:
Based on your implementation I would suggest making public methods within FormMenu that return these int values.
public int ReturnRows()
{
return Int32.Parse(myMenu.comboBoxRows.Text);
}
public int ReturnColumns()
{
return Int32.Parse(myMenu.comboBoxColumns.Text);
}
Then from the other form in which myMenu is instantiated you can call myMenu.ReturnRows() and myMenu.ReturnColumns()
The easiest way would be to store a reference to your form in the menue as a variable. (you already named it myMenu)
Then you should create the property/properties you need in the form an add a setter for the values. (see example here)
Last you update the form fields with
myMenu.property = newvalue;
That`s all about it
I am creating a Windows Phone 8.1 App and my mainpage has a Hub control that consists of one (manually created) XAML hubsection and multiple dynamic hubsections generated from JSON. I have a button that takes the user to a new page. When the user returns to the mainpage it currently shows the user the first hubsection. I am trying to make it so the user returns and sees the hubsection that was focused on before.
I attempted:
IList<HubSection> currentSections;
currentSections = mainHub.SectionsInView;
// Do your code
mainHub.SectionsInView = currentSections;
but apparently SectionsInView is readonly. I can't find anything online... is there a way to do this?
Try using Hub.ScrollToSection method or the Hub.DefaultSectionIndex property
I'm wondering if it is possible to leave out the Uri QueryString parameter so that when a user pins a tile, it may not link to the application. This would be for design reasons when pinning several ShellTiles onto the Start Screen. The user would of course have the option to delete the tile manually from the Start Screen or from a button within the app itself. It seems when trying this I am still taken to the application but a debug error also occurs.
An example of when I create the ShellTile is as follows
ShellTile.Create(new Uri("/MainPage.xaml?" + Constants.Key + title, UriKind.Relative), LiveTile);
Can this be modified somehow?
Your secondary tile must have unique navigation URI. However you could navigate to some page that would immediately close itself. It is not the best solution but there isn't any better way.
For uniqueness just use Guid. You can close app in code with Application.Current.Terminate() for example.
I'm working in WP8 and I would like to know if it's possible to redirect the user to a custom page instead of the default MainPage when the application's tile is pressed.
For example, Toast Notifications have a NavigationUri that let's you enter a custom page to load after it's pressed. On tiles you can only get that information and not set it.
So there is any other way or I'm missing something?
This is all covered nicely in this blog post: http://blogs.msdn.com/b/ptorr/archive/2010/08/28/redirecting-an-initial-navigation.aspx?wa=wsignin1.0
Essentially the idea is to cancel the default navigation and then request a navigation to whichever Page you want.
When you are creating the tile, you are specifying the URL to which to navigate. Simply include an additional parameter, such as comingFrom and set it to tile - in the view OnNavigatedTo, check whether the parameter is present (QueryString is your friend).
If it is - you navigated from a tile. If it's not - you didn't.
Sounds like you need a SecondaryTile. SecondaryTiles allow you to navigate to a different page than your "MainPage" if you so desire.
Here is an example of how to create a secondary tile
ShellTile tileToFind = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains("Page2.xaml"));
if (tileToFind == null) // have not pinned this page yet!
{
// Create the tile if we didn't find it already exists.
var tileData = new StandardTileData
{
Title = "Navigate To Page2",
};
// Create the tile and pin it to Start. This will cause a navigation to Start and a deactivation of our application.
ShellTile.Create(new Uri("/Page2.xaml", UriKind.Relative), tileData);
}
This will place a new tile on the start screen and when you tap it, it will navigate to Page2.