Navigate to page with parameter without OnNavigateTo method in codebehind - c#

I'm developing an Windows 8 Store App.
I want pass a parameter when I navigate to another page in my app.
I'm using the MVVMLight toolkit and I use the .Navigate(string Type, object param) method from the NavigationService which implements INavigationService.
I can navigate to the page and use the parameter, but just when I use the OnNavigateTo(NavigationArgs e) method.
In fact I just want the parameter in the ViewModel of the second page without using any code in the codebehind of the second page.

You can use GalaSoft.MvvmLight.Messaging.Messenger to communicate within your application.
Please refer http://blog.galasoft.ch/archive/2009/09/27/mvvm-light-toolkit-messenger-v2-beta.aspx
for more details.

Take a look at my implementation of MVVM Navigation via an Interface and it's implementation
It's as simple as doing _navigationService.Navigate<Map>(false);
(I'm navigating to the ViewModel Map, and my NavigationService just knows that X ViewModel is mapped to X.xaml page!)
More at https://github.com/cmorgado/MultiPlatform

Related

Navigation between pages in Xamarin Forms

I have the following command inside a VM:
ICommand LaunchGameCommand => new Command(() =>
{
//Navigation.PushAsync(...
});
According to the answers here I should be able to use something akin to the navigation in the commented code; however, the Navigation object seems to reside in Android.Content.Res... which seems to be something else entirely.
Is this still the correct method of navigating between views / viewmodels in Xamarin Forms, or has this now been superseded with an alternate method?
Navigation is part of a page, you can’t find navigation property if you don’t have the reference to a some page, you need to have access to your current page in your view model to see this property, you can have access to your current page using
Application.Current.MainPage.Navigation.Push...
Are you using any particular MVVM framework? Most of these include a way of navigating from VM to VM.
I use FreshMvvm. It allows you to perform the following to navigate between VMs and also pass data:
CoreMethods.PushPageModel<MyNextPageModel>(DataToPass);
More details here

Using single View for multiple ViewModels with Caliburn.Micro on Windows Phone 8.1

I have a Windows Phone 8.1 app using Caliburn.Micro. In the app I have a few ViewModels that fetch data in different way and with different logic but show them in the same way. So I want to make all those ViewModel use the same one View.
I found out that ViewLocator.LocateTypeForModelType is a method that gets executed for mapping ViewModels to Views. So I override it to use my custom attribute when present:
var original = ViewLocator.LocateTypeForModelType;
ViewLocator.LocateTypeForModelType = (modelType, displayLocation, context) =>
{
var useViewAttributes = modelType.GetTypeInfo().GetCustomAttributes<UseViewAttribute>(true);
if (useViewAttributes.Count() == 1)
{
var viewTypeName = string.Concat(modelType.Namespace.Replace("Model", string.Empty), ".", useViewAttributes.First().ViewName);
var type = AssemblySource.FindTypeByNames(new List<string>() { viewTypeName });
return type;
}
return original(modelType, displayLocation, context);
};
Stepping through the it seems to work fine. If I navigate to a ViewModel and that ViewModel has a UseView, my method returs the correct View.
The app navigates to the correct View but the ViewModel is never created. Kind of like Caliburn.Micro forgot about the ViewModel, or was looking for one using a different convention, or something.
I found out that ViewModelLocator.LocateTypeForViewType is called after navigation to a View to resolve the ViewModel. The ViewModel type from the previous step seems to be forgotten completely.
In ViewModelLocator.LocateTypeForViewType I only have access to the View type and I do not know, how to make it resolve the correct ViewModel from the previous step. I could scan all the ViewModel and find the ones with the correct attribute, but I would not know which one to choose.
Any ideas on how to approach this?
Here is a minimal project showing my setup: https://dl.dropboxusercontent.com/u/73642/CMVMTest.zip
This sort of solution would work everywhere else except for the top level navigation. The reason for this is there is sort of a "double dispatch: going on when you navigate.
As you know the Frame or PhoneNavigationFrame control (depending on WinRT or Silverlight) is view based in it's navigation. So the steps look a little like this.
Your code tells the navigation servie=ce to navigate to ProductViewModel.
It uses ViewLocator (where you've injected your code) to locate ProductView and tells the Frame to navigate to that.
The navigation service then responds to the navigating event to ProductView and locates the correct view model using ViewModelLocator.
It then instantiates and binds this view model.
This sort of view model to view to view model step in navigation service causes the hiccup in your code.
You should be able to create dummy views that simply inherit the base view and add nothing. So if you have MySharedView.xaml then declaring what's below should be enough.
public class SecondView : MySharedView { }
It's not ideal I know, but does get you the reuse you're after. Having the navigation service remember the view model between the navigating and navigated events becomes complicated with all the external factors that can cause navigation as well.

Frame GoBack Event with parameter

I'm building an XAML Metro app using C# and having some thoughts.
I'm using two pages, MainPage and OtherPage.
When i click a button in MainPage i go to the other one with
this.Frame.Navigate(typeof(OtherPage), AndAnObjectIsSentTo);
To go back from OtherPage to MainPage, i have a (cancel) button with the code Frame.GoBack();
What i want to achieve when i go back is to send a parameter and (EDIT: not "reload" but just run a function) the MainPage (the page i get back to). How should I do this?
GoBack doesn't pass a parameter so you'll need to pass the data through an outside channel.
One possibility would be to include it as a property in the object passed to Frame.Navigate. The original page can track what it sent and then look up what it had sent when OnNavigatedTo is called with NavigationMode.Back.
Also consider that complex objects aren't recommended for the parameter in Frame.Navigate since only simple types support navigation state serialization. A typical alternative is to pass a string or GUID key into a lookup table. This table could also be used to store return values back.
Lastly, if the pages share a data model you could store the data there, though you may not want to muddle your data with command parameters.
You can always go back with the Navigate method to the previous page the same way you got there. And then you can pass a parameter, too.
Will that not work for you?

Error using 'this' in a cs page trying to access control

I have a page called test.aspx with test.cs.
However, i want to access my control called mbResult
Which is my custom messagebox control, from a sepearate CS page.
I know many people have asked this question and i have found that this is a method to access my controls.
MessageBoxControl mbox1 = this.FindControl("mbResult") as MessageBoxControl;
But I keep getting this error
Error 5 Keyword 'this' is not valid in a static property, static method, or static field initializer
Any ideas on how to access this control all i am trying to do is make it visible.
Thanks
You need to move the code into a non-shared method. You need to be operating in an instance of the page.
Update for clarification in comments
Unfortunately, your application is going to need some restructuring.
If the messageboxcontrol is shown in a new window, then you will need to pass the value from your source page to the new window in the query string.
However, if you want the messagebox control to be displayed on the source page, then you will need to convert it from a page to a UserControl, add a reference to the user control to your source page, and then add an instance of the usercontrol directly to the source page.
Statics don't have instance-based contexts, so using this is not applicable. You'll need a reference to the control for which you want to use .FindControl (possibly by passing it as a parameter).

How to send data to a page navigated to through the Frame.Navigate in Silverlight 4

Platform:
Silverlight 4 / .NET 4
Background:
I have a page that consists of two parts. The left part is a tree view, the right one is the content area. When I select a tree item, an appropriate UserControl page should be loaded in the right part of the page, depending on the type of the tree item.
I am using a Frame object, defined in XAML.
When a user selects a tree view item, I resolve the item's type and then I navigate to the page defined for that type.
However, Frame.Navigate is an asynchronous method so if I try to get the frame's content after Navigate, the frame has not navigated yet, so I either get nothing or the last loaded page.
contentFrame.Navigate(new Uri("/PageA.xaml", UriKind.Relative));
PageA page = contentFrame.Content as PageA;
// page here is either null or a previously opened page
Problem:
I need to send some data (stored in treeview item's Tag) to the page being navigated to and the only Frame.Navigate overload is asynchronous (without callback). How can I send some data to the navigated page? Is there any other technique for accomplishing what I need?
You can use the NavigationService provided by Silverlight. It has query string support using which you can build RESTful URI. I am assuming that you need to pass some context based data to the landing page. Following links might help you
http://www.silverlightshow.net/items/The-Silverlight-3-Navigation-Framework.aspx
http://blogs.microsoft.co.il/blogs/alex_golesh/archive/2009/04/02/silverlight-3-quick-tip-6-navigation-framework-and-uri-routing.aspx
http://www.wintellect.com/CS/blogs/jprosise/archive/2009/04/07/silverlight-3-s-new-navigation-framework.aspx
Although the links says Silverlight 3, these features are supported in Silverlight 4 as well.
You definitely want to use some flavor of the M-V-VM pattern that Nilesh suggested.
I guess you could also have a static class that holds static references of objects.
You can simply refer to static objects here when your frames have completed their event (e.g. NavigatedTo).

Categories

Resources