Custom MvxTouchViewPresenter not showing subsequent ViewModel - xamarin.ios

I've written a custom MvxTouchViewPresenter that allows me to show either a SlidingPanel (RootView), or show a MvxTabBarViewController (AuthView).
When my app launches,
if I tell it to load the TabBarView (AuthView), it works as expected.
if I tell it to load the SlidingPanelView (RootView), it also works as expected.
The problem occurs when I load the AuthView and then try to ShowViewModel<RootView>()... basically what happens in this scenario is that I stay at the AuthView, even though I see the CustomPresenter.Show() method has run appropriately.
Here's the method
public override void Show(MvxViewModelRequest request)
{
var viewController = (UIViewController)Mvx.Resolve<IMvxTouchViewCreator>().CreateView(request);
RootController = new UIViewController();
// This needs to be a Tab View
if (request.ViewModelType == typeof(AuthViewModel))
{
_navigationController = new EmptyNavController(viewController);
RootController.AddChildViewController(_navigationController);
RootController.View.AddSubview(_navigationController.View);
}
if (request.ViewModelType == typeof(RootViewModel))
{
_navigationController = new SlidingPanelsNavController(viewController);
RootController.AddChildViewController(_navigationController);
RootController.View.AddSubview(_navigationController.View);
AddSlidingPanel<NavigationFragment>(PanelType.LeftPanel, 280);
}
base.Show(request);
}
And here's a Gist of the complete class
What am I missing in trying to make this work appropriately?

not sure if what I've done is "correct" but it's working for now. I'm still very much open to better answers.
What I've done in order to simply move on from this problem is add a call to SetWindowRootViewController(_navigationController); just before I call base.Show(request)
public override void Show(MvxViewModelRequest request)
{
_navigationController = null;
var viewController = (UIViewController)Mvx.Resolve<IMvxTouchViewCreator>().CreateView(request);
RootController = new UIViewController();
// This needs to be a Tab View
if (request.ViewModelType == typeof(AuthViewModel))
{
_navigationController = new EmptyNavController(viewController);
RootController.AddChildViewController(_navigationController);
RootController.View.AddSubview(_navigationController.View);
}
else if (request.ViewModelType == typeof (RootViewModel))
{
_navigationController = new SlidingPanelsNavController(viewController);
RootController.AddChildViewController(_navigationController);
RootController.View.AddSubview(_navigationController.View);
AddSlidingPanel<NavigationFragment>(PanelType.LeftPanel, 280);
}
else
{
throw new Exception("They View Type you're trying to show isn't currently supported.");
}
// RIGHT HERE
SetWindowRootViewController(_navigationController);
base.Show(request);
}

Related

Xamarin iOS Transparent Navigation bar

Need to have transparent navigation bar
https://developer.xamarin.com/recipes/ios/content_controls/navigation_controller/change_the_nav_bar_transparency/
tried with this but not working.
Any Suggestion ?
Although, your question is not fully descriptive and not sure what exactly you want to achieve by saying "Not Working" from the given link.
Write following code in respective override methods of the ViewController in which you want to achieve the Transperent NavigationBar for that specific ViewController only.
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
if (NavigationController != null)
{
NavigationController.NavigationBar.SetBackgroundImage(new UIImage(), UIBarMetrics.Default);
NavigationController.NavigationBar.ShadowImage = new UIImage();
NavigationController.NavigationBar.Translucent = true;
NavigationController.View.BackgroundColor = UIColor.Clear;
NavigationController.NavigationBar.BackgroundColor = UIColor.Clear;
}
}
public override void ViewWillDisappear(bool animated)
{
base.ViewWillDisappear(animated);
if (NavigationController != null)
{
NavigationController.NavigationBar.SetBackgroundImage(null, UIBarMetrics.Default);
NavigationController.NavigationBar.ShadowImage = null;
NavigationController.NavigationBar.BarTintColor = UIColor.Red; //Provide your specific color here
}
}
If you want to set this Globally, Try it in AppDelegate's FinishedLaunching method :
UINavigationBar.Appearance.SetBackgroundImage(new UIImage(), UIBarMetrics.Default);
UINavigationBar.Appearance.ShadowImage = new UIImage();
UINavigationBar.Appearance.BackgroundColor = UIColor.Clear;
UINavigationBar.Appearance.Translucent = true;
Hope this helps!.
A single navigation bar is used for a particular navigation controller in iOS. So if you want to make it transparent for a single VC then you have to make it transparent when you navigate to that VC. And then change it back when you coming back from that VC in viewWillDisappear (or viewDidDisappear) methods.

MVVMCross Data Binding after returning from ViewController

I am using MVVMCross 3.2.2 as part of an iOS/Android app. One of my screens has multiple views that are displayed depending upon the selection in a Tab bar like row of buttons. Different data is displayed in each of these these views individual UITableView. The data binding works perfectly.
I also have a menu, that has a "profile" selection. Changing the profile fires an MvxMessage that my HomeView receives and then uses the message to set the ViewModel up to filter the data to be displayed. This all seems to work perfectly and the data is displayed correctly.
If I do something in the HomeView that displays another view using ShowViewModel(). When I return back to the home view the binding no longer works properly when a profile changes is made. The message gets handled, the data gets filtered, but a call to ReloadDataTable on the UITableView does not change the data.
ViewModel
#region Groupings
public IList<Group> Groups{
get { return _groupService.GetAll(); }
}
public void SetupSubGroups(Group group)
{
if (group == null)
{
_groups = new ObservableCollection<Group> ();
if (_profileService.SelectedProfile != null)
{
var grp = _groupService.GetByGroupName (_profileService.SelectedProfile.Name);
if (grp == null)
grp = new Group { Name = _profileService.SelectedProfile.Name };
_groups.Add (grp);
}
}
else
{
var litsOfGroups = _groupService.GetSubGroups (group);
foreach (var grp in litsOfGroups)
_groups.Add (grp);
}
RaisePropertyChanged(() => AvailableGroups);
}
private ObservableCollection <Group> _groups;
public ObservableCollection<Group> AvailableGroups {
get { return _groups; }
}
#endregion
ViewController
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
var groupSource = new GroupTableViewDataSource (TableViewGroups);
TableViewGroups.Source = groupSource;
_localViewModel.SetupSubGroups (null);
_bindingSet = this.CreateBindingSet<HomeViewController, HomeViewModel> ();
_bindingSet.Bind (groupSource).To (vm => vm.AvailableGroups);
_bindingSet.Apply ();
TableViewReportTags.ReloadData ();
NavigationController.NavigationBarHidden = false;
}
private void OnProfileChanged(ProfileChangedMessage message)
{
_localViewModel.SetupSubGroups (null);
TableViewGroups.ReloadData ();
}
private HomeViewModel _localViewModel { get { return ViewModel as HomeViewModel; } }
Any ideas what I can look at, or change would be really useful. I have spend many hours on this, and have made no progress.

Downloads with JavaFX WebView

my web application offers a download. Javascript creats at the click the url (it depends on the user input) and the browser should open it, so that the page isn't reloaded.
For that, I think I have to alternatives:
// Alt1:
window.open(pathToFile);
// Alt2:
var downloadFrame = document.getElementById('downloads');
if (downloadFrame === null) {
downloadFrame = document.createElement('iframe');
downloadFrame.id = 'downloads';
downloadFrame.style.display = 'none';
document.body.appendChild(downloadFrame);
}
downloadFrame.src = pathToFile;
Both works under Firefox. Problem with open new window method: If the creation of the file at the server needs more time, the new empty tab will be closed late.
Problem with iframe: If there is an error at the server, no feedback is given.
I think at firefox the iframe is the better solution. But the web application must run with an JavaFX WebView, too. JavaFX haven't a download feature, I have to write it. One possible way is to listen on the location property:
final WebView webView = new WebView();
webView.getEngine().locationProperty().addListener(new ChangeListener<String>() {
#Override public void changed(ObservableValue<? extends String> observableValue, String oldLoc, String newLoc) {
if (newLoc.cotains("/download")) {
FileChooser chooser = new FileChooser();
chooser.setTitle("Save " + newLoc);
File saveFile = chooser.showSaveDialog(webView.getEngine().getScene().getWindow());
if (saveFile != null) {
BufferedInputStream is = null;
BufferedOutputStream os = null;
try {
is = new BufferedInputStream(new URL(newLoc).openStream());
os = new BufferedOutputStream(new FileOutputStream(saveFile));
while ((readBytes = is.read()) != -1) {
os.write(b);
}
} finally {
try { if (is != null) is.close(); } catch (IOException e) {}
try { if (os != null) os.close(); } catch (IOException e) {}
}
}
}
}
}
There are some problems:
The download start depends on a part of the url, because JafaFX supports no access to the http headers (that is bearable)
If the user starts the download with the same url two times, only the first download works (the change event only fires, if the url is new). I can crate unique urls (with #1, #2 and so on at the end). But this is ugly.
Only the "window.open(pathToFile);" method works. Loading an iframe don't fire the change location event of the website. That is expectable but I haven't found the right Listener.
Can someone help me to solve 2. or 3.?
Thank you!
PS: Sorry for my bad english.
edit:
For 2. I found a way. I don't know if it is a good one, if it is performant, if the new webview is deleted or is in the cache after download, ....
And the user don't get an feedback, when some a problem is raised:
webView.getEngine().setCreatePopupHandler(new Callback<PopupFeatures, WebEngine>() {
#Override public WebEngine call(PopupFeatures config) {
final WebView downloader = new WebView();
downloader.getEngine().locationProperty().addListener(/* The Listener from above */);
return downloader.getEngine();
}
}
I think you may just need to use copyURLtoFile to get the file...call that when the location changes or just call that using a registered java class. Something like this:
org.apache.commons.io.FileUtils.copyURLToFile(new URL(newLoc), new File(System.getProperty("user.home")+filename));
Using copyURLToFile the current page doesn't have to serve the file. I think registering the class is probably the easiest way to go... something like this:
PHP Code:
Download $filename
Java (in-line class in your javafx class/window... in this case my javafx window is inside of a jframe):
public class JavaApp {
JFrame cloudFrameREF;
JavaApp(JFrame cloudFrameREF)
{
this.cloudFrameREF = cloudFrameREF;
}
public void getfile(String filename) {
String newLoc = "http://your_web_site.com/send_file.php?filename=" + filename;
org.apache.commons.io.FileUtils.copyURLToFile(new URL(newLoc), new File(System.getProperty("user.home")+filename));
}
}
This part would go in the main javafx class:
Platform.runLater(new Runnable() {
#Override
public void run() {
browser2 = new WebView();
webEngine = browser2.getEngine();
appREF = new JavaApp(cloudFrame);
webEngine.getLoadWorker().stateProperty().addListener(
new ChangeListener<State>() {
#Override public void changed(ObservableValue ov, State oldState, State newState) {
if (newState == Worker.State.SUCCEEDED) {
JSObject win
= (JSObject) webEngine.executeScript("window");
// this next line registers the JavaApp class with the page... you can then call it from javascript using "app.method_name".
win.setMember("app", appREF);
}
}
});
You may not need the frame reference... I was hacking some of my own code to test this out and the ref was useful for other things...

How to get current used color theme of Visual Studio

I'm creating my own IntelliSense Presenter, since Visual Studio2012 support change theme, so I want my background color of the presenter can be auto-changed when the theme been changed. Is there a way to track the theme changes event, or get the current color theme of the Visual Studio?
Yes, this is possible. I had to solve a similiar issue with one of my extensions...
The current theme is stored in the Windows Registry; so I implemented the following utility class.
public enum VsTheme
{
Unknown = 0,
Light,
Dark,
Blue
}
public class ThemeUtil
{
private static readonly IDictionary<string, VsTheme> Themes = new Dictionary<string, VsTheme>()
{
{ "de3dbbcd-f642-433c-8353-8f1df4370aba", VsTheme.Light },
{ "1ded0138-47ce-435e-84ef-9ec1f439b749", VsTheme.Dark },
{ "a4d6a176-b948-4b29-8c66-53c97a1ed7d0", VsTheme.Blue }
};
public static VsTheme GetCurrentTheme()
{
string themeId = GetThemeId();
if (string.IsNullOrWhiteSpace(themeId) == false)
{
VsTheme theme;
if (Themes.TryGetValue(themeId, out theme))
{
return theme;
}
}
return VsTheme.Unknown;
}
public static string GetThemeId()
{
const string CategoryName = "General";
const string ThemePropertyName = "CurrentTheme";
string keyName = string.Format(#"Software\Microsoft\VisualStudio\11.0\{0}", CategoryName);
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(keyName))
{
if (key != null)
{
return (string)key.GetValue(ThemePropertyName, string.Empty);
}
}
return null;
}
}
Okay; this just helps to figur out the current settings... listening for the theme changed notification is a bit trickier. After your package is loaded, you must obtain an IVsShell instance via the DTE; once you have this object you can utilize the AdviceBroadcastMessages method to subscribe for event notifications. You have to provide an object whose type implements the IVsBroadcastMessageEvents interface...
I don´t want to post the whole implementation, but the following lines might illustrate the key scenario...
class VsBroadcastMessageEvents : IVsBroadcastMessageEvent
{
int IVsBroadcastMessageEvent.OnBroadcastMessage(uint msg, IntPtr wParam, IntPtr lParam)
{
const uint WM_SYSCOLORCHANGE = 0x15;
if (msg == WM_SYSCOLORCHANGE)
{
// obtain current theme from the Registry and update any UI...
}
}
}
Consider implementing IDisposable on that type as well, in order to be able to unsubscribe from the event source, when the package gets unloaded.
This is how I subscribe for event notifications...
class ShellService
{
private readonly IVsShell shell;
private bool advised;
public ShellService(IVsShell shellInstance)
{
this.shell = shellInstance;
}
public void AdviseBroadcastMessages(IVsBroadcastMessageEvents broadcastMessageEvents, out uint cookie)
{
cookie = 0;
try
{
int r = this.shell.AdviseBroadcastMessages(broadcastMessageEvents, out cookie);
this.advised = (r == VSConstants.S_OK);
}
catch (COMException) { }
catch (InvalidComObjectException) { }
}
public void UnadviseBroadcastMessages(uint cookie)
{
...
}
}
Keep the value of the cookie parameter; you´ll need it to successfully unsubscribe.
Hope that helps (-:
Just wanted to put an update just in case anyone else comes along.. #Matze and #Frank are totally right.. However in VS 2015.. they added a easy way to detect the theme change. So you need to include PlatformUI an dyou get a super easy event
using Microsoft.VisualStudio.PlatformUI;
....
//Then you get an event
VSColorTheme.ThemeChanged += VSColorTheme_ThemeChanged;
You should make sure your control is disposable so you can unsubscribe from the event...
BONUS!
It also give you easy access to the colors.. even if the user has changed them from the default .. so you can do stuff like this in when set your colors
var defaultBackground = VSColorTheme.GetThemedColor(EnvironmentColors.ToolWindowBackgroundColorKey);
var defaultForeground = VSColorTheme.GetThemedColor(EnvironmentColors.ToolWindowTextColorKey);
For VS 2015 this has changed, the solution #Matze has still works but you need to update the GetThemeId() function to check for the version and if it's 14.0 (VS2015) look in a different place in the registry. The way the value is stored has changed also, it's still a string but now contains other values seperated by a '*'. The theme guid is the last value in the list.
if (version == "14.0")
{
string keyName = string.Format(#"Software\Microsoft\VisualStudio\{0}\ApplicationPrivateSettings\Microsoft\VisualStudio", version);
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(keyName))
{
if (key != null)
{
var keyText = (string)key.GetValue("ColorTheme", string.Empty);
if (!string.IsNullOrEmpty(keyText))
{
var keyTextValues = keyText.Split('*');
if (keyTextValues.Length > 2)
{
return keyTextValues[2];
}
}
}
}
return null;
}

MKAnnotationView RightCallOut button crashes my app when I click on it

I'm calling a service and returning a bunch of latitudes and longitudes which I'm then placing on a map using MapKit.
using MKAnnotationView I'm adding a RightCallOutButton to each annotation.
So I had to create a new MapDelegate. Code below.
If I click on the button I create the app crashes and I get an error from MonoTouch saying the selector is accings omething that has already been GC'd (garbage collected).
So my question would be, where should I set the RightCalloutAccessoryView and where should I create the button, if not in this code below?
public class MapDelegage : MKMapViewDelegate {
protected string _annotationIdentifier = "BasicAnnotation";
public override MKAnnotationView GetViewForAnnotation (MKMapView mapView, NSObject annotation) {
MKAnnotationView annotationView = mapView.DequeueReusableAnnotation(this._annotationIdentifier);
if(annotationView == null) {
annotationView = new MKPinAnnotationView(annotation, this._annotationIdentifier);
} else {
annotationView.Annotation = annotation;
}
annotationView.CanShowCallout = true;
(annotationView as MKPinAnnotationView).AnimatesDrop = true;
(annotationView as MKPinAnnotationView).PinColor = MKPinAnnotationColor.Green;
annotationView.Selected = true;
var button = UIButton.FromType(UIButtonType.DetailDisclosure);
button.TouchUpInside += (sender, e) => {
new UIAlertView("Testing", "Testing Message", null, "Close", null).Show ();
} ;
annotationView.RightCalloutAccessoryView = button;
return annotationView;
}
}
annotationView = new MKPinAnnotationView(annotation, this._annotationIdentifier);
...
var button = UIButton.FromType(UIButtonType.DetailDisclosure);
You should avoid declaring local variables to hold references you expect to outlive the method itself. Once there's no reference to annotationView or button the Garbage Collector (GC) is free to collect them (the managed part) even if it's native counterparts still exists. However when a callback to them is called you'll get a crash.
The easiest solution is to keep a list of them and (at the class level, i.e. a List<MKPinAnnotationView> field) clear the list when you destroy the view. The UIButton should not be necessary since there's a reference between the view and it.
NOTE: work is being done to hide this complexity from developers in future versions of MonoTouch. Sadly you cannot ignore such issues at the moment.

Resources