Essentially, what I'm trying to do is take a selected item from a combobox on Page 1 and set the image source on Page 2 based on that selection, but I'm not exactly sure how to make this happen.
On Page 1 I have:
private void pOneColorChoice(object sender, SelectionChangedEventArgs e)
{
setPlayerOneColor = PlayerOneColor.SelectedItem;
}
On Page 2 I'm not sure how to call that selection and then fit it into this to set the image:
BitmapImage ImageOne;
if (PlayerOneColor == Black)
{
ImageOne = new BitmapImage(new Uri("Assets/Black.jpg"));
PlayerOneImage.Source = ImageOne;
}
I would add other statements for the other color choices. I'm probably way off but any suggestions would be greatly appreciated!
When you are navigating to the Page2 you can send it as a parameter
private void pOneColorChoice(object sender, SelectionChangedEventArgs e)
{
setPlayerOneColor = PlayerOneColor.SelectedItem;
Frame.Navigate(typeof(Page2), setPlayerOneColor);
}
Here in Page 2 Navigation Method you can get your parameter like this
var name = e.NavigationParameter as yourType;
As has been answered here, you can pass your option as a parameter when you navigate to the page.
I'd recommend taking a look at following an MVVM design pattern for your application though, this way you can access your other page's view model from other pages. MVVM Light is a great starting point.
Doing it with this method will let your have one view model for your settings page where you choose your option, and then from the other, you'd access that same property which you've changed. I can go into more detail if required :)
Related
This may be a very simple question, so my apologies.
My problem here is that I want to build a Multilingual WPF App using C#, but I don't know how to make my different Page elements inherit the same method which makes my MainWindow translate to different languages. The app is done, I'm just translating it to English (My native language is Spanish).
I'm using Resource files to translate it.
Code for the language translation:
private void Languages_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//This is the combobox in which you select the language to display the app in
Set_Language();
}
//This is the method to invoke the Resource Manager and all the stuff from the resource file.
private void Set_Language()
{
if (!boolInit)
{
strLanguage = "LeitProjekteV2._0.Languages." + ((ComboBoxItem)LanguageSel.SelectedItem).Name.ToString();
ResourceManager LocRm = new ResourceManager(strLanguage, typeof(MainWindow).Assembly);
//Menu buttons
lblMenu.Content = LocRm.GetString("strMainMenu"); //The names inside the "" are the names of the resource in the Resource file which, depending on the language selected(Spanish, English and German)
//Change the text of whatever I choose; in this case, a Label named 'lblMenu'
MapButt.Content = LocRm.GetString("strMapButt");
BuscButt.Content = LocRm.GetString("strBusButt");
AgeButt.Content = LocRm.GetString("strAgeButt");
ComButt.Content = LocRm.GetString("strComButt");
InfButt.Content = LocRm.GetString("strInfButt");
LoginButt.Header = LocRm.GetString("strLoginButt");
RegisterButt.Header = LocRm.GetString("strRegisterButt");
ContacButt.Header = LocRm.GetString("strContacButt");
MasButt.Header = LocRm.GetString("strMoreButt");
//Here go the names of everything the Pages contain that I want to translate, just like above
//Have no idea how to inherit this method to all the pages
}
}
Now, I have several pages embedded in the same MainWindow.xaml, so that you click the button "Map", a Frame changes it's content to a Page named Map.xaml, and so on for other buttons.
But how do I make those Pages also translate?
Since the Set_Language() method takes the string value of the Combobox in order to select the correct Resource File, I don't want to create one combobox for every Page that I have, albeit that would eliminate my problem.
Any help? Sorry for the horrible way of asking, I'm still getting the hint here.
Thanks.
Use the below:
var wnd = Window.GetWindow(this); //get current window
Cast it to your window class and expose your language as a public property
Use your page to get the property by finding the current window
You may create a parent page class that do the above, and inheriting it for all your pages so you dont repeat code
I can see the difficulty you are facing is that you can't find a way to share the combobox across the main window and the pages hosted in the frame.
You can set a global variable that is accessible from the whole application, a good place is in application settings. Then when you make a selection with the Combobox, you just update the selected value to that variable.
Then call each page's Set_Language() method when they are being loaded into the Frame. In the Set_Language() method of each page, you can query what is been set to the variable stored in the application settings.
If you want to quick solution, create a static class to hold the selected language is also OK.
static class UserSelections
{
public static string Language { get; set; }
}
I have two Pages ProductSearch,ProductDetail and Im changing the Content property to Navigate between Pages. I want to know if any events are fired so I can write some code in it?
On ProductDetail Page I have UIElement property
public UIElement MainContent { get; set; }
On ProductSearch Page I Navigate to ProductDetail By setting the Content property like this:
private void OnGetDetailsClick(object sender, RoutedEventArgs e)
{
ProductDetail productDetail = new ProductDetail();
productDetail.MainContent = this.Content;
this.Content = productDetail;
}
On ProductDetail Page's Back Button I navigate back to ProductSearch:
private void OnBackButtonClick(object sender, RoutedEventArgs e)
{
this.Content = MainContent;
}
Now I want to know how can I call a method when I navigate Back to ProductSearch Page i.e how would I know that I have Navigated from ProductDetail Page? I tried to check if it loads the page but found out that When you change content of control it doesn't fire the load event of the page. Any solution?
Yea this will not execute the load event since your are only changing the content obviously.
if you want to take advantage of navigation you should check out this video. Even if its done with blend the concepts apply also with Visual STudio: https://vimeo.com/6599527 (Simple Silverlight Master/Detail Navigation App with Blend 3)
You should check out articles on master detail binding like this one: https://msdn.microsoft.com/en-us/library/cc645060(v=vs.95).aspx
The key is to take advantage of the powerful binding concepts which come with silverlight. And if you are not using deep linking you might want to consider using a user control to hide/show details instead of a extra page.
HTH
I'm currently building a universal app but I'm concentrating on the WP8.1 part of it right now. I'm using MVVMLight with this project.
For simplicity sake, we'll just assume that I only have 2 pages in the project.
Page1.xaml contains a list which has various items. The Page1.xaml is binded to its own ViewModel i.e. Page1ViewModel. Each item in the list represents a viewModel i.e. ItemViewModel.
When I tap on an item, I call the following code:
public RelayCommand<ItemViewModel> ItemTapCommand
{
get
{
return this._itemTapCommand ?? (this._itemTapCommand =
new RelayCommand<ItemViewModel>((msg) =>
ExecuteItempTapCommand(msg)));
}
}
When an item in the list is tapped, I call the following code:
private object ExecuteItempTapCommand(ItemViewModel selectedItemViewModel)
{
Page2ViewModel page2ViewModel =
SimpleIoc.Default.GetInstance<ItemViewModel>();
page2ViewModel.SelectedItem = selectedItemViewModel;
_navigationService.Navigate(typeof(Page2),
selectedItemViewModel);
return null;
}
As you can see I'm using my Ioc to create get an instance of my Page2ViewModel and I then set the SelectedItem to the selectedItemViewModel.
Once it is set, I navigate to Page2 which is binded to my Page2ViewModel.
What I want to know is, is the above is ok to do? I've seen plenty of examples when dealing with passing object from one page to another is done by passing an Id for example and then I request the information from Page2, but why request it again when most of the information I need is already in my SelectedItemViewModel since it represents the tapped item in my list in Page1.
If it's not correct, what is the best way to go about this using MVVMLight?
Can you provide a sample? I've seen something about Messaging but I'm not sure how this would work as if I navigate to my page2, the Page2ViewModel will only be initiated when the page is created, so how can it receive a message? The way I have it above seems to initiate the Page2ViewModel and my Pag2 loads, it's re-using it and everything bind correctly but I'm not sure this is the correct way to go about it.
Any help would be appreciated.
Thanks.
In your Page2ViewModel, why not use
protected override void OnNavigatedTo(NavigationEventArgs e)
{
Page2SelectedItem = e.Parameter as ItemViewModel;
base.OnNavigatedTo(e);
}
It looks like you are packing that data in with your _navigationService.Navigate call already.
With that set up, what happens if you just change to:
private object ExecuteItempTapCommand(ItemViewModel selectedItemViewModel)
{
_navigationService.Navigate(typeof(Page2), selectedItemViewModel);
return null;
}
You can use the ViewModel to get it if you do some work before that.
Read this blog post by Marco Minerva called Calling ViewModel methods in response to Page navigation events using MVVM Light in WinRT
which explains how to react to OnNavigatedTo and OnNavigatedFrom in the ViewModel.
It's a very cool solution.
I'm writing a music player app for WP8 in XAML and C#, but coming from a winforms background I don't know how to do the following.
My main page opens a separate page to display the tracks of a music album. The user can select some of these tracks which then get added to a central playlist which is working fine, but I want the main page to call its playlist refresh function when the song selection page is closed.
If I was doing this as winforms I would do something like:
private void ShowAlbumPage(Int16 albumId)
{
albumPage.albumId = albumId;
albumPage.ShowDialog();
RefreshPlaylist();
}
But this will not work for XAML
I currently have this:
private void ShowAlbumPage(Int16 albumId)
{
NavigationService.Navigate(new Uri("/AlbumPage.xaml?albumId=" + albumId.ToString(), UriKind.Relative));
}
Any suggestions on how and when to call RefreshPlaylist?
Its a vague idea ,but i think if i share i can also improve. You could pass the List to the AlbumPage (Current collection) , if user add a song to playlist , add this to the collection , when returning just send back the collection and update the Home Page.
// MainPage.Xaml
private void list_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
SongsList selectedItemData = SelectedItem as SongsList ;
if(selectedItemData != null)
{
NavigationService.Navigate(new Uri(string.Format("/AlbumPage.xaml?parameter={0}",selectedItemData.ID ), UriKind.Relative));
}
}
//AlbumPAge.Xaml
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
string parameter = this.NavigationContext.QueryString["parameter"];
SongsList country = null;
// GETS THE SONG COLLECTION HERE , UPDATE WHEN USER ADD TO PLAYLIST , AND RETURN BACK.
}
**If your doing on MVVM Way .This is just an Idea not tested.**
There is view-model it contains Collection of Songs, Maintain the same view-model over the two pages , Update the Song collection so Mainpage UI will automatically gets updated.
ViewModel Maintain the same over the Album Page.
ObservableCollection<Songs> _songs=new ObservableCollection<Songs>();
_songs.Add(new songs{Artist="ArtistName",Album="AlbumName"});
// AlbumPage CodeBehind.
private AddSongtoPlaylist(Song currentSong)
{
_songs.Add(currentSong);
}
Reference
Some of the ways you could accomplish this:
Add the newly selected songs on the Application level on the PhoneApplicationService.State Dictionary. Whenever you return to the main page, load the transient state that will include the added songs.
Save them on a variable on the Application level and whenever navigating to the main page load the playlist.
Store them in the IsolatedStorage and load them whenever you navigate to the main page.
Pass them back to your main page through the navigation QueryString.
The method you choose is just a matter of personal preference.
I think I have found a reasonably simple workaround - I have just put a call to RefreshPlayQueue() in the OnNavigatedTo method of MainPage.xaml. This seems to work
I can't seem to find an answer to this, maybe I'm not using the correct terminology.
I have a ListView that is editable, I want it so that if a user clicks on Edit and then Update, that the field is updated with the value from another textbox, not the field they are editing.
The reason for this is that I have a Colour Picker that alters the value of a textbox, when they click Update I want this value to be the updated value.
I guess I utilise the ItemUpdating event, but I don't have much in the way of code because I'm pretty lost. I have this so far:
protected void ListView2ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
var selectedItem = ListView2.Items[ListView2.EditIndex];
// I have no idea what to put here
something = ColourChosen.Value;
}
Here is an image that I hope will make what I'm trying to do a little more understandable:
If any one could point me in the right direction of any examples, that would be much appreciated.
Although this doesn't answer my initial question this does what I want to happen.
What I should be doing is altering the database that ListView is attached to.
I use this code:
protected void ListView2ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
using (var myEntities = new i96X_utilEntities())
{
var myPlotColour = (from plotC in myEntities.PlotColours
where plotC.ID == selectedID
select plotC).Single();
myPlotColour.PlotColour1 = ColourChosen.Value;
myEntities.SaveChanges();
}
}
So, even though I have no idea how to intercept a field being updated in a ListView, in this example I don't need to.