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)
Related
I started using MVVMCross several weeks ago and so far I love it, but I've noticed that whenever I'm working on the UI and making changes in the XAML and pressing save which activates the Hot Reload in Visual Studio 2019, I'm getting Null Reference Exceptions.
This is down to the fact that the MVVMCross' Prepare method is not getting called. According to their doc's this method is used to pass a parameter to the VM which can then be used throughout the VM.
public override void Prepare(MyClass param)
{
MyClass = param;
}
public override Task Initialize()
{
MyClass.DoSomething();
return base.Initialize();
}
So this is the basic usage (I think). However when I Hot Reload the XAML changes Prepare isn't called but Initialize is, which causes the Exception.
Is this a bug ?
Prepare in normal cases will only be called when calling NavigationService.Navigate.
What Hot Reload actually does is unclear. This use case is currently not officially supported by MvvmCross.
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;
}
}
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()
}
}
Thanks for looking.
I am working on an Outlook plugin that includes a pop-up Form that loads a browser inside of it to allow the user to log in via a 3rd party auth service.
This works great when running from a debug session: I see the custom tab in the ribbon, click the "login" button, and the form pops up as a modal using .ShowDialog().
I am using Outlook 2016.
Problem
When I publish this VSTO and then install it on my machine, the plugin loads and I can see the "login" button in the custom ribbon tab, but clicking it does nothing. I have checked to be sure that the dialog isn't simply popping under the main form. If it's there--I can't find it.
Back to debug session--everything works great. I suspect a permissions issue, but I don't get any prompts or errors from Outlook.
Last, I don't know if it's related, but I sent the VSTO installer to a colleague and they get the following error when attempting to install:
System.Security.SecurityException: Customized functionality in this
application will not work because the certificate used to sign the
deployment manifest for {APP NAME REMOVED} or its location is not
trusted. Contact your administrator for further assistance.
Any help is greatly appreciated.
Most probably your form is shown behind the Outlook window. You need to specify the parent window handle if you want to see the form all the time on top of Outlook windows. The Show and ShowDialog methods of the System.Windows.Forms.Form class allows to specify the parent window handle by passing an instance of the IWin32Window interface as a parameter.
First, you need a class which implements that interface:
public class WindowWrapper : System.Windows.Forms.IWin32Window
{
public WindowWrapper(IntPtr handle)
{
_hwnd = handle;
}
public IntPtr Handle
{
get
{
return _hwnd;
}
}
private IntPtr _hwnd;
}
In Outlook you can cast an instance of the Explorer or Inspector class to the IOleWindow interface and get the window handle which can be used for the IWin32Window implementation.
I've developed a WP7 client that uses the Facebook C# SDK, using OAUTH and the web browser control.
Everything works fine except that on the page where the user is requested to accept/reject the access permissions I am asking for, the "Don't Allow" and "Allow" buttons are off the bottom of the browser's screen, and it isn't obvious that the user must scroll down to click on them.
I've tried using all the different display modes (touch, wap, page, popup) and "page" is the only one that shows the buttons on the same page, but then the fonts are tiny. I've also tried different sizes for the browser control.
The example in the SDK has the same behavior.
Has anyone found a work-around for this?
The solution I have found is to use Javascript to change the CSS properties for the element:
private void FacebookLoginBrowser_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
// check for when this approve page has been navigated to
if (FacebookLoginBrowser.Source.AbsolutePath == "/connect/uiserver.php")
{
showBrowser();
// do the script injection on the LoadCompleted event - doing it here will appear to work when you have a fast connection, but almost certainly fails over 3G because the elements aren't ready in time to be modified
FacebookLoginBrowser.LoadCompleted += new System.Windows.Navigation.LoadCompletedEventHandler(FacebookLoginBrowser_LoadCompleted);
}
// etc ...
}
void FacebookLoginBrowser_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
FacebookLoginBrowser.LoadCompleted -= FacebookLoginBrowser_LoadCompleted;
// Facebook will likely change this and break our code soon, so make sure you anticipates this
try
{
FacebookLoginBrowser.InvokeScript("eval", "document.getElementById('platform_dialog_bottom_bar').style.position = 'relative';document.getElementById('platform_dialog_bottom_bar').style.top = '-60px';");
}
catch
{
// TODO: display instruction to scroll down if we ever end up here
}
}
I hope that helps. Feel free to contact me if you run into problems
I haven't tried this, but could you use the WebBrowser control's Scale(SizeF) method to change the zoom level of the page?