navigate from login page to usercontrol after login using mvvm - silverlight

I have login page and I have bind login button to command in view model. I want to navigate from this page to other user control which is Home page.
It is possible to do this using binding between view and view model?

This is a good question. Let us not forget the reason why you use MVVM in the first place. The good thing about MVVM is that you can separate the data that is presented from the presentation code. Hopefully, by doing this, your application is easier to test, and you can slap a different UI on it e.g. WinRT and it will work.
The requirements are:
You show a logon page,
The user types in their credentials,
You navigate to an application page.
However, if you use the NavigationService, then when the user hits the back-button, they will go back to the logon page, which you probably don't want.
In this case, you are better off showing a Popup that overlays the entire application page. Then close the popup when the credentials are verified.
Depending on whether you prefer View-First or ViewModel-First, you'd structure things differently. Here's a ViewModel first approach:
LogonViewModel logon = new LogonViewModel();
logon.LogonSucceeded += () =>
{
App.DismissViewFor<LogonViewModel>(logon);
AppViewModel appViewModel = new AppViewModel();
App.ShowViewFor<AppViewModel>(appViewModel);
}
App.ShowViewFor<LogonViewModel>(logon);
You can either roll your own framework to implement App.DismissViewFor, App.ShowViewFor or use one like Caliburn.Micro which provides a ViewLocator.

Related

How to open application from a webbrowser windows phone

I am developing a windows phone application.
In a page I placed a button and on this button click I open the webbrowser and redirects to our website pages for some processes. There are many webpages. In the last webpage we added a button "Close". In the close button click I want to close the webbrowser and open the application back with the last state before opening the webbrowser. How can I do this ?
Thanks.
I will refer to the button in the app as "app button" and the button in the web page as "close button".
Add a WebBrowser control called webBrowser1 to the Windows Phone app. Make it cover the entire screen, and set it's Visibility property to Collapsed.
On the app button's click event, use
webBrowser1.Visibility = System.Windows.Visibility.Visible;
webBrowser1.Navigate(new Uri("http://yourwebsite.com/page");
to show the web browser and navigate it to the first page on your website.
Make the close button on the last page of your site navigate to a new page on your site, called "close.html" or whatever you want. In javascript, this would look like
<Button onclick="window.location.href='http://yourwebsite.com/close.html';">
Back in the app: On webBrowser1's Navigating event use,
if (e.Uri.ToString().Contains("close.html"))
{
webBrowser1.Visibility = System.Windows.Visibility.Collapsed;
}
When the you click the button on the last page of your site, it navigates to "close.html". When this happens, the Web Browser's Navigating event fires. Since this event fires every time you change pages, you need to check to see if the new url contains "close.html", the page your close button is navigating to. If it does, the Web Browser will be hidden and you will see your app again.
.
(In VB, the code would be )
webBrowser1.Visibility = System.Windows.Visibility.Visible
webBrowser1.Navigate(New Uri("http://yourwebsite.com/page")
And
If e.Uri.ToString.Contains("close.html")
webBrowser1.Visibility = System.Windows.Visibility.Collapsed
End If
Edit: I was thinking in generic terms when i wrote the answer and did not remember that you are asking specifically about the webbrowser (hence, using a webbrowsertask launcher). Thank you #Claus for point out the OAuth situation. So, i am amending my answer to explain that it is possible and also mention an issue with using the launcher as there is no way guarantee to return back to a given point in your launcher app (as it is with choosers due to the availability of callbacks).
It is not possible to achieve this in general terms. That is, there is an application A which opens another application B and from application B you would like to close-it-and-open-A. There are many reasons why i think it is not possible:
- How does one get the address/reference to the application A. No API for that at the moment.
- There is no content-handler/plugin where a 3rd party can register an app with the web-browser.
- Most importantly, security, security, security. This would open doors for attacks from the web.
However, for your requirement of application B being a web-browser, it is possible to use a task launcher WebBrowserTask. As #claus suggests, you could have Window.close() javascript in your last page to close the browser and hence reveal the app underneath it (hopefully, A). The problem here is that if, the user opens an app (let's call it C) after the browser has launched (and before the browser is closed), and the user does not close C, then when the browser gets closed, the user will be returned to C and not to the launcher App! This is not what you want based on your requirement.
So, if you would like to achieve the kind of effect you are describing in your question, it is best that you embed the Web-browser in your application (as a full-screen app) and from that vantage point you can interact between the web-browser (control) and the (host) app via Javascript.
Hopefully, this helps.

Silverlight redirecting between project pages

I am having a my problem is I can not redirect my page in silverlight like most of people who are not familiar with this technology .
I design a login page first and if password is correct I want it to direct to MainPage.xaml
as you can see I tried methods which are commented already it did not work . I searched in this web page there are some posts about this problem but I could not solve please help me .
when i try that one;
Uri target = new Uri("MainPage",UriKind.Relative);
NavigationService.Navigate(target);
error message : Object reference not set to an instance of an object.
actually we found a solution with my friend ;
instead of directing a web page , when code goes into if block we are changing the content of page with {this.Content = new MainPage() ; } method and it is working .But System.Windows.Browser.HtmlPage.Window.Navigate(target) this one is directing us same login page or pages like www.---.com outside normal html pages .
Normally you need to use the NavigationService to do this:
NavigationService.Navigate(new Uri("/MainPage", UriKind.Relative));
The NavigationService is a property on the Page object so it will be available in the Login code-behind.
However, after you mentioned that you've "already done this" I realized the issue. Silverlight Navigation uses a Frame control. This Frame control lives in MainPage.xaml for the default project. So you're not navigating inside the frame, but you want to change the entire screen to a different one and do so without the Navigation services built into Silverlight.
I'd suggest you either (a) have the Login run as a page inside of the Frame on MainPage (you can load or unload the links on the top based on if the user is authenticated) or (b) don't use the Navigation for the remainder of the application and use a framework like Caliburn.Micro to handle all the Navigation between views.

Navigation application - Forces a page at startup

I have a silverlight navigation application where I load and initialize a lot of things in the MainPage.aspx loading.
All the Pages (views) are dependent on the loading of that data.
But really often my users add an internet shortcut on their desktop for my application. but the link often points to a specific page like that :
http://myServer/MyApp/default.aspx#/TheView
I want to force the users to go to the "Default" view (and ignore the #/view parameter) when the application Starts up.
Whats the best way I can do that?
I don't think it's a good idea to 'avoid' links. You should construct the application in a way when it's not a problem.
For example you can send a message from your main logic when everything loaded, and until that message you can disable the appropriate UI (while waiting for data). Messenger (or EventAggregator in Prism) is a publisher-subscriber pattern to achive communication between components without reference to each other. If you don't know it yet, take a look at MVVMLight Messenger or Prism EventAggregator
But if the first option is not okay for you, you can play with the Frame control's JournalOwnership property:
http://msdn.microsoft.com/en-us/library/system.windows.controls.frame.journalownership(v=vs.95).aspx
http://msdn.microsoft.com/en-us/library/system.windows.navigation.journalownership(v=vs.95).aspx
OwnsJournal is your value, because it does not integrate with the browser journal and the url stays the original so your users can't bookmark a 'sub page'.
You can also implement your own journal mechanism.
Another :), but pretty ugly option would be if you track the first page load (in OnNavigatedTo Page events for example), and at the very first request you manually navigate to the main page. Or in the App.xaml.cs, when application starts up you forward the page request.
But I recommend the first one.

ViewModel Event Communication

I have a WP7 app I'm creating and I want a login screen to appear if the user hasn't logged in yet. I have Main.xaml which has a view model MainViewModel.cs. For the login or signup portions I have them embedded as a Grid in Main.xaml, but I would think having them as a user control would work fine also. The login and signup portions will have their own view model, possibly the same one for both, AccountViewModel.cs, that the Grid or user control has it's DataContext set to.
After the user signs up or logs in, which occurs in AccountViewModel.cs, what is the best way for MainViewModel.cs or Main.xaml to know that it is complete, and it can begin loading data, or doing whatever it needs to do?
My initial thought is to use MVVM Light's messaging system. After signup/login occurs, broadcast a message that it's complete, and MainViewModel.cs will be registered to the message and can act on it.
Is there another way or more proper way of letting Main know something has occurred in it's child?
If this is too hard to follow I can add code examples.
A messaging system, such as the one in MVVM Light is a great way to decouple these kind of actions and provide notifications in the way you describe. Can't say as I'd advocate anything else really. The Prism library provides an EventAggregator, which does the same thing, but if you're already using MVVM Light, then stick with that.
Another approach would be to store this kind of info (IsLoggedIn) in a "global view model" such as SettingsViewModel.Instance for example. For a viewmodel of global meaning like Settings, it is an approach that makes a lot of sense IMHO. If you make this property raise the PropertyChanged event, this allows you to dynamically modify the UI when the property changes, and hide the login UI smoothly for instance.
cheers,
Laurent

Windows Phone 7 - Using the Application bar and Prism (or MVVM)

I am playing around with the WP7 SDK and the Prism for WP7 beta and have come across a problem I can't figure out (or even a workaround for).
First of all, I'm new into WPF/Silverlight/WP7 and Prism, so I could be overlooking something very obvious.
So I have a Shell page that has my region that is used to hold my content pages, and all of this is working great! Now my problem is that I have a settings control that will allow the users to edit the settings of the application (names, locations, etc). Now I can get this page to work with no problems by having a button on one of my controls that will transition the region manager to the control.
However, I would like to use the application bar on the phone to have the button, but I cannot for the life of me figure out how to get access to my model object from within the page that is opened by the Application bar click. I can only do a NavigationService.Navigate() to a settings page, but the PhoneApplicationPage objects in WP7 do not allow injection on the constructors (the constructors must be parameterless) so I cannot pass in the object instance in that way.
So my question is, how can I access (or pass) objects between pages or controls?
Thanks!
In the examples they use this technique to set the data context of a form after it is navigated to from another form:
NavigationService.Navigate(new Uri("/Page2.xaml", UriKind.Relative));
FrameworkElement root = Application.Current.RootVisual as FrameworkElement;
root.DataContext = some_object;

Resources