Open gallery in simulator on image tap - c#

I want to open the gallery in iPhone simulator upon button click.
I am using Xamarin forms - xaml to create pages and image views.
What I have implemented so far :
I have a GalleryService specific to iOS that implements IGalleryService that has a method selectImage. The selectImage creates a UIImagePickerController _imagePicker.
In iOS specific apps, I would just do
NavigationController.PresentModalViewController(_imagePicker, true);
But how to do something similar using Navigation in xamarin forms?
PS: I have created view in .xaml file and move across pages in xaml.cs file like
Navigation.PushModalAsync(page)
Here, the page must basically contain the _imagePicker view, right? How to do that?
Kindly correct me if I am wrong.

Xamarin.Forms actually utilises a single NavigationController on iOS to host all the different pages. All of the Xamarin Content Pages navigation happens internally in XF, inside the single main NavigationController.
You cannot mix other native view controllers inside the NavigationPage stack. But you can use all the native code you normally would and just treat the one NavigationController that forms uses as a single "screen".
Inside the iOS native project, you can use
var topController = UIApplication.SharedApplication.KeyWindow.RootViewController;
while (topController.PresentedViewController != null) {
topController = topController.PresentedViewController;
}
topController.PresentViewController (_imagePicker, true, null);
to access the XF's view controller, find the top one and use that like you would in a "native" ios app to present the modal view.
Note that this RootViewController will be null until "LoadApplication" finishes, so make sure any calls are made after that, which should always be the case if you are triggering this action from the running app and not immediately on startup.
If using PCL's, you would need some form of a DependencyService to trigger this call from your shared code, and you might need a bit more logic in the native app to get the result back into the shared code. You would also need to reimplement this on any other platforms you might be targeting with your app (such as Android or UWP).

Related

Is it possible to have a consistent NavBar and Footer in a Xamarin application that uses native views?

I have a Xamarin forms application, in which I would like to have a consistent header and footer (Like below in white) for every view in the application. The header would be a nav bar and the footer would also have a button on it.
I also want this to only be implemented once instead of having the same code reused for every view.
The Xamarin application has one view in which it's implementation is native platform-specific (It has an activity in The .Android project and a storyboard in the .iOS project). This implementation is accessed using an interface which is implemented in the two projects to create the view. This means I can't use anything like a ControlTemplate, as they cannot be accessed by platform specific views.
With this in mind, How can I implement the header and footer so it shows up each ContentPage instance and every native view with only the one implementation?

Getting Master Detail to behave on UWP just like it does on Android and IOS

I have a prism Xamarin Forms application which is being designed for both UWP, Android on IOS. This is the first UWP app that I have created. I want to have a menu that is only accessible from the first level of navigation. To do this, in the past, I have used a Master-Detail page. However, I have encountered some behaviour differences that I need some help with.
When I use a Master-Detail screen on IOS or Android it behaves like a menu only accessible to the first level of navigation. Any subpage that you might navigate from there you don't see menu it is replaced by the back button.
When I use a Master-Detail screen on UWP it's behaviour is different. The menu is available on all pages no matter how far into the navigation stack you travel. It is by default also a menu tray of items anchored on the left side of your application.
I have figured out how to make the menu collapse into the hamburger button that I am used to on an Android and IOS application by setting the parameter MasterBehavior to the value Popover. But I would like to have the menu completely disappear on any page navigated past the first level as it does on IOS of Android.
Does anyone know of a way to make this happen?
I think I can say safely that this can't be done in pure Xamarin.Forms.
To change this you would have to change to which UWP Frame the content is loaded, and I am not aware that Frame is exposed anywhere in Xamarin.Forms which means that even custom renderers cannot help.
However you may try to build the native navigation that would act like that in the UWP project and then load inside those native controls Xamarin.Forms page, that should work and deliver the results that you have requested. Not trivial and no few lines that I can show you, but if that is important for you it should be possible this way.

Global MVVM ViewModel in UWP app

In WP8.1 or WPF desktop apps I could create my ViewModel as a static instance in App.xaml.cs and refer to it throughout the app as "App.myViewModel" but in UWP that does not seem to work (perhaps by design?).
The reason I want a global ViewModel is that, like many apps, I have one main page that populates the VM by downloading info from websites (I'm working on a business based RSS Reader for a client) and when I navigate to another page then come back to MainPage my vm is empty again (as I appear to have no option but to declare it in MainPage) so I have to reload all the data every time.
Also if I want to access the VM from navigated pages I now have to pass a reference to the VM through to that page whereas with the global approach I could still access App.myViewModel.
The only time I alter the VM contents is from UI actions on the Mainpage so I just want to be able to navigate back to the main page from other pages and have the VM still intact.
Can someone please explain how this is done in UWP? Can I still use App.xaml.cs and if so, how? If this is not possible why was it designed like this?
You are looking for ViewModelLocator. Here you have nice explanation and example how to use it. https://stackoverflow.com/a/14154088/4727426

How to make programmatic changes to views in the controller appear in the storyboard

I recently inherited a Xamarin project where I am to work on the iOS project. Going over several tutorial I figured I was ready, however the person before me did not use storyboards or controllers! They did all customization of views in files named LoginScreens.cs (basically controller files from the looks of it).
For learning purposes and ease of transition I would like to get a storyboard going in this project. So I created a storyboard titled Main and added a ViewController and essential copy and pasted the view customization code used in the LoginScreen.cs into my controller.
To be a little more specific, I have a Main.Storyboard that looks like this:
Where I am using editText boxes and a button to act as place holders for what I actually do to them in the ViewController.cs.
This all seems to register and builds properly however when I run the debugger on iPhone 6s iOS 9.3 I get the following:
The changes appear to show, but all my storyboard iOS designer views remain in place. I am trying to see if there is a way to reflect the changes made in the controller on the storyboard.
TL;DR: I'm trying to alter some views in a programmatic way in my ViewController.cs file. These views were originally added via the iOS designer and for customization purposes, they were edited in the controller. I want to see the visual alteration I make on a view in the controller, reflect in the iOS designer and when I debug.
Sounds like you are trying to go from a project where views were all done programmatically to implementing storyboards. This is a Big change and will take some time to convert the views over.
In the cs file there will be things like Add(passwordTextFeild) which are going to add more views to your storyboard view, hence why you get alot of views in your login view. You wont be able to see these in the storyboard as they are done at runtime.
If you are looking for IBDesignable this is more for custom controls and you still will have to add code to be able to see the changes from the .cs file in the storyboard.
Check my Question for IBInspectable/IBDesignable in Xamarin
Check this official link: https://developer.xamarin.com/guides/ios/user_interface/designer/ios_designable_controls_walkthrough/
Probably not what you wanted to hear but the UI in iOS projects tend to be done in one of the three methods:
All programmatically
Storyboards
Xibs
There are tons of questions/blog posts (even a video), weighting up the pros and cons of each. So possibly the last developer felt it was best to do it programmatically
There is a setting, IBDesignable, that you can add to the declaration of UI classes in Swift or Objective-C. That tells Xcode the those UI objects have a custom interface that you want to be presented in Interface Builder.
I have no idea how (or even if you could) you would use IBDesignable in Xamarin/C#. If its not supported for Xamarin then you're probably out of luck.
I suggest you search on "Xamarin IBDesignable" on the net.

How to create tabbed page in Xamarin.Forms similar to that of Facebook app

I am working with Xamarin.Forms and I would like to create a navigation method similar to that of the Facebook app and also other newer versions of different apps. Here are two pictures, one of Facebook app on Android and the other is of KakaoTalk, also on Android.
I've tried several different ways to implement this. For example I wrapped the MainPage (TabbedPage) inside a NavigationPage. I also tried wrapping the MainPage (ContentPage) inside a NavigationPage then calling
PushAsync(new TabbedPage());
inside the MainPage via the Navigation property. I also tried wrapping the children of the MainPage (TabbedPage) inside separate NavigationPages. None of these attempts resulted in the desired effect.
I would like the following to happen. When a user taps on a Button or something that navigates to another page I'd like a ContentPage to pop in front of everything (tab bar included) except for the Navigation bar (or Action Bar). Obviously this happens via the PushAsync() method which also results in a small back arrow appearing in the Nav bar. My understanding is that a PushAsync() call pushes the chosen page onto the navigation stack provided by the NavigationPage. Therefore if a TabbedPage is wrapped inside a NavigationPage a new ContentPage would not be able to pop in front of the tabs like in the two applications mentioned above.
This, plus the fact that the tab bars of said apps don't get inside the ActionBar (in Android) in landscape mode leads me to believe that what I'm trying to achieve is best done not using TabbedPage but something that acts like a tab bar, e.g. a horizontal stack of Buttons perhaps.
Has anyone implemented this correctly and in an elegant way using Xamarin.Forms? So again, I would like to make this tab like navigation with the tabs staying underneath the ActionBar in landscape mode and a new Page appearing in front of the tabs (hiding them) when pushed onto the navigation stack. Do you think this can be done with Custom Renderers? Of course in iOS these are done with tabs but in Android I'm not sure.
What I would like is not necessarily code examples, but a best practice on this issue. So again, this is in Xamarin.Forms with a common PCL project and an Android and iOS project. Thank you in advance for your answers and advices!
P.S.: I've obviously searched this site before (Google as well) but I've found no real articles on this issue.
I have used ToolbarItems order=primary for navigation bar menu button and ToolbarItems order=secondary for tab bar on top.I have PCL the code written is in XAML. :)
http://codeworks.it/blog/?p=232
My understanding is that a PushAsync() call pushes the chosen page onto the navigation stack provided by the NavigationPage. Therefore if a TabbedPage is wrapped inside a NavigationPage a new ContentPage would not be able to pop in front of the tabs like in the two applications mentioned above.
This is not true actually. According to Xamarin API Doc, the Navigation property is context-aware, i.e. it will find all its parents until there is a NavigationPage.
In Android, only one NavigationPage is allowed and therefore even you call PushAsync() inside the TabbedPage, it will still push via your wrapping NavigationPage.
You can create a custom controller using the grid. it will remove the hassle of writing custom renderers.

Categories