How can I run a specific page in my UWP app? - visual-studio

So I am working on a UWP app that has multiple pages with full UI.
Currently I would run the entire app and then navigate to the page I want to test. I just want to know if there is an easier way to test a certain page and not have to go through the entire app?
I know you can go into App.xaml and change the code there to launch the page I want but is there a way to do it without hard coding it?

Here is how you can do it in VS2017:
in your solution properties set up a new configuration "Debug_MyPage" for testing MyPage:
in your project properties, under 'Build' add a compilation symbol "MYPAGE" for the new configuration:
in App.xaml.cs add a conditional line for navigating to MyPage:
Now you can switch between "Debug" and "Debug_MyPage" configurations to launch to either the regular main page or the specific page you want to test without having to hardcode/change your source.

If I understand correctly, you want to change application startup point to test specific Pages, so you should edit just startup Page OnLaunched(LaunchActivatedEventArgs e) event in App.xaml.cs to achieve your goals:
if (rootFrame.Content == null)
{
rootFrame.Navigate(typeof(yourPageToTestHere), e.Arguments);//edit this line
}
Another solution is : You can navigate to test pages from MainPage.xaml.cs , just override it OnNavigatedTo event :
protected override void OnNavigatedTo(NavigationEventArgs e)
{
try
{
contentFrame.Navigate(typeof(yourTestPageHere));//navigate to yourTestPage page
}
catch (Exception)
{
}
}

Related

Xamarin Forms WebView open external link

I have a webview inside my application and when an external link is clicked (that in normal browser is open in a new tab), I can't then go back to my website.
It is possible when a new tab is open to have the menu closed that tab like Gmail do ?
The objective is that, whenever a link is clicked, the user would have the choice to choose which option to view the content with, e.g. Clicking a link would suggest open youtube app or google chrome. The purpose is to appear the google chrome option
Or what suggestions do you have to handle this situation ?
If I understood you correctly, you want to have the option to select how to open the web link - inside your app, or within another app's (browser) context.
If this is correct, then you can use Xamarin.Essentials: Browser functionality.
public async Task OpenBrowser(Uri uri)
{
await Browser.OpenAsync(uri, BrowserLaunchMode.SystemPreferred);
}
Here the important property is the BrowserLaunchMode flag, which you can learn more about here
Basically, you have 2 options - External & SystemPreferred.
The first one is clear, I think - it will open the link in an external browser.
The second options takes advantage of Android's Chrome Custom Tabs & for iOS - SFSafariViewController
P.S. You can also customise the PreferredToolbarColor, TitleMode, etc.
Edit: Based from your feedback in the comments, you want to control how to open href links from your website.
If I understood correctly, you want the first time that you open your site, to not have the nav bar at the top, and after that to have it. Unfortunately, this is not possible.
You can have the opposite behaviour achieved - the first time that you open a website, to have the nav bar and if the user clicks on any link, to open it externally (inside a browser). You have 2 options for this:
To do it from your website - change the a tag's target to be _blank like this;
To do it from your mobile app - create a Custom renderer for the WebView. In the Android project's renderer implementation, change the Control's WebViewClient like so:
public class CustomWebViewClient : WebViewClient
{
public override bool ShouldOverrideUrlLoading(Android.Webkit.WebView view, IWebResourceRequest request)
{
Intent intent = new Intent(Intent.ActionView, request.Url);
CrossCurrentActivity.Current.StartActivity(intent);
return true;
}
}

Xamarin.Forms How to switch pages using MVVMLight

I'm currently working on a Xamarin.forms project using .NET Standard as code sharing strategy. I try to use the MVVM pattern by using the MvvmLightLibsStd10 library. I already successfully setup the MVVM structure by using this tutorial:
https://www.linkedin.com/pulse/build-xamarinforms-net-standard-mvvm-light-app-rafael-carvalho
I can't use Navigation.PushAsync(new Page()); because it only works in code behind and not in the ViewModel.
I already tried to Pass Navigation trough the VM constructor, like describe over here:
Xamarin.form Page Navigation in mvvm
But when I try this method, an error occurred at "LoadApplication(new DemoMVVM2.App());" in MainPage.
How can I switch pages using MVVM Xamarin.Forms with MVVMLight (based on the code from my first url)?
but I have no Idea how I can switch Pages via the ViewModel and keeping the header with back button.
Generally when working with MVVMLight you'll be using a NavigationService.
This class can be constructor injected in your VM, thanks to the build in IOC in MVVMLight.
With it you can do Navigate and GoBack in your VM, triggering a real navigation on the current stack.
Only thing that you maybe missed, is the fact that you need to write one yourself for Xamarin forms.
But Laurent Bugnion ( the owner of MVVMLight ) supplied an example available here:
https://github.com/lbugnion/sample-2016-vslive-crossplatform/blob/master/Flowers/Flowers.Forms/Flowers.Forms/Helpers/NavigationService.cs
You can pass a callback to your ViewModel(VM) and on Command or whatever action call your navigation code which is in your page (View). this way you can keep your navigation code in your page and your binding logic in your ViewModel.
interface NavHandler{
void navigateToSomeView();
}
public class MyPage : ContentPage,NavHandler{
public MyPage(){
BindingContext = new MyViewModel(this);
}
void navigateToSomeView(){
Navigation.PushAsync(new Page2());
}
}
public class MyViewModel{
NavHandler handler;
public MyViewModel(NavHandler handler){
this.handler = handler
}
//Your action
this.btnClicked = new Command(async()=>{
handler.navigateToSomeView()
}
}

How can i use NavigationServices in Application_Activated windows phone 7?

im developin an app for wp7 that it holds pictures and notes with password login. But when app running if user press windows button app is running at background and if user press back button it resumes without asking password again.
i tried to Navigate when app activated but i couldnt manage it in Application_Activated method. is there a way to do that? Or could you advice me sth else that solve my problem.
ty.
here is my code im using to navigate,
(Application.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
I got around this issue by using UserControls on the MainPage, showing one if the user had not yet logged in and the other if they had, I set these controls up to show/hide based on certains states in the MainPage and then bind that to the MainViewModel:
private void Application_Activated(object sender, ActivatedEventArgs e)
{
// Ensure that application state is restored appropriately
....your code here to load stuff...
App.ViewModel.MainPageState = "ShowThemTheLogin";
}
}

How to go to main menu on a button click in WP7

I have a button in the main page and on the click of it i want to exit the app and return to the phone's main menu page. My concern is that i cannot use CancelEventArgs for button_click event and another concern is that this is the first page in theNavigation stack, so Navigetion.goBack is false. Suggestions and help are welcome.
private void btnDeclineClick(object sender,RoutedEventArgs e)
{
exitApplication();
}
public void exitApplication()
{
try
{
NavigationService.CanGoBack.Equals(true);
if (NavigationService.CanGoBack)
{
NavigationService.GoBack();
}
}
catch(Exception e)
{
Logger.log(TAG, e.Message);
}
}
Programmatically exiting the application is not supported and against the certification requirements. I agre with Praetorian that you should just not have a Decline button or have it simply cover the application UI with some non-interactive content.
The only supported mechanisms for exiting the application are for the user to press the back button or navigating forward out of the application using the home/windows button or any launcher and then being killed by the OS.
Is this are you facing problem of exiting application programmatically ??
If yes you can do that without worrying which page user into currently
For exiting programmatically you can check this my another post answer.

Custom BasePage causing design view to break

I was getting fed up with typing this.NavigationService.Navigate(new Uri(page.xaml, UriKind.Relative));, every time I need to navigate to a different page in my app.
So I've created a custom BasePage with a virtual to help with Navigating around my app.
The problem I have is in VS2010, if I have the source and design view open, the design just shows the windows phone background and I get some blue wiggly lines right from the top to the bottom of my xaml and messages along the lines of x isn't supported. This happens on any page that I have set up to Inherit from my custom BasePage.
However, if I run the application on my Windows Phone or in the Emmulator it will work.
Does anyone have any suggestions of what I could try to keep my Design view working whilst apply my custom base, or if I have missed something off?
A slightly cut down version of my BasePage is:
public class BasePage : PhoneApplicationPage
{
public virtual void NavigateTo(string pageName, params Tuple<string,string>[] queryString)
{
// Code to perform this.NavigationService.Navigate
}
}
EDIT 2011-08-16
Part of this base page overrides the PhoneApplicationPage's OnNavigatedTo method, in which I perform a security check to see if:
security has been enabled
User is logged in
If the security is enabled but the user is not logged in, they are immediately redirected to a Login Page.
I found this useful as I don't then have to add any code to existing or new pages to handle this, so long as they derive from the BasePage.
I wouldn't recommend using a BasePage for this. Instead, simply add your NavigateTo method in the App.xaml.cs file, as a static method.
public static void NavigateTo(string pageName, params Tuple<string,string>[] queryString)
{
// Code to perform this.NavigationService.Navigate
}
Also, remember to wrap the call to .Navigate in Dispatcher.BeginInvoke so all transition effects are properly executed.
And as a bonus tip: Don't use the designer in Visual Studio. Instead, set the 'default editor' for XAML files to be the "Source Code" editor, so the designer is never opened. This makes Visual Studio much more stable.
If you want a designer, you should get Microsoft Expression (Blend)

Resources