Objective
Update a Xamarin.Forms ListView when the iOS Application Enters the foreground
Approach
The closes example I have seen is found in the monotouch samples
https://github.com/xamarin/monotouch-samples/blob/master/SimpleBackgroundFetch/SimpleBackgroundFetch/AppDelegate.cs
public override void PerformFetch (UIApplication application, Action<UIBackgroundFetchResult> completionHandler)
{
UINavigationController navigationController = Window.RootViewController as UINavigationController;
UIViewController topViewController = navigationController.TopViewController;
if (topViewController is RootViewController) {
(topViewController as RootViewController).InsertNewObjectForFetch (completionHandler);
UIApplication.SharedApplication.ApplicationIconBadgeNumber++;
} else
completionHandler (UIBackgroundFetchResult.Failed);
}
This is using PerformFetch but the WillEnterForegroundMethod will accomplish what I am trying to do.
Problem
The problem is I am using a Xamarin.Form Page and don't know how to get access to the Page or the Pages' ViewModel from the App Delegate Class. Any ideas?
You could register the ViewModel with an IoC container when you set it as the BindingContext for the View and then get it using that.
If you're using MVVMLight, SimpleIoC is included in that package.
Related
I tried to implement Reactive UI Example using Avalonia with ReactiveUI. The search works, I can print on the console the elements resulting from it and there is a "slot" for each of them in the UI (the lines appear but are empty), but the NuggetDetailView does not show as the list's items.
I have activated View for ViewModel scan in the Initialize method of my Avalonia app :
public class App : Application
{
public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
Locator.CurrentMutable.RegisterViewsForViewModels(Assembly.GetCallingAssembly());
}
....
}
I do not get any error so I'm a bit lost on what I did wrong.
Thank you in advance,
Turns out the reflection-based View scanning was not working.
I changed
Locator.CurrentMutable.RegisterViewsForViewModels(Assembly.GetCallingAssembly());
to
Locator.CurrentMutable.Register(() => new NugetDetailsView(), typeof(IViewFor<NugetDetailsViewModel>));
And it worked
I am new to using caliburn.micro and currently learning MVVM.
I am using the windows template studio to create my UWP app, and it works great! But unfortunately, I am not familiar with MVVM and still learning UWP.
I get how the navigation works etc and how the shellpage is loaded. However, I want to prompt the user to log in upon opening the app (i.e. a login in page will start with no navigation sidebar).
I also want to make sure I'm following best practices...
I have tried substituting the MainViewModel with LoginViewModel which I get works, however, I don't want to create the navigation pane. I get that this is triggered by the "new Lazy(CreateShell)". I'm just not sure if I want to remove this from the activation service and call a method upon login?
Below is the default code supplied by the windows template studio which triggers on activation of the app if I understand correctly.
private ActivationService CreateActivationService()
{
return new ActivationService(_container, typeof(ViewModels.LoginViewModel), new Lazy<UIElement>(CreateShell));
}
private UIElement CreateShell()
{
var shellPage = new Views.ShellPage();
_container.RegisterInstance(typeof(IConnectedAnimationService), nameof(IConnectedAnimationService), new ConnectedAnimationService(shellPage.GetFrame()));
return shellPage;
}
I just need to be pointed in the right direction or lead to a video/tutorial as I'm struggling!!! any help much appreciated.
If you want to show login page,you can remove the ShellPage.It is a navigation view.
in App.xaml.cs
private ActivationService CreateActivationService()
{
return new ActivationService(this, typeof(LoginPage));
}
private UIElement CreateShell()
{
return new Views.ShellPage();
}
When login successfully,if you want to show the navigation view,you can set the ShellPage to the content of current window.
Window.Current.Content = new Views.ShellPage();
I'm trying to implement Login screen in first page but mvvmcross version 4.1.2 had implement Navigation Bar white transparent like image below. So how can i remove it (not hidden) ?
Thank for your help so much!
The answer is simple by not using navigation controller. You can do that by don't use the default presenter within MVVMCross.
You can learn more about it within this post: http://gregshackles.com/presenters-in-mvvmcross-a-primer/
ou cannot completely remove the navigation controller when using MVVMCross, instead you would hide it. Which gives you the exact same effect as what you are looking for (I assume). As mentioned by #mafis above.
You can use a Custom iOS Presenter and do the following:
protected override UINavigationController CreateNavigationController(UIViewController viewController)
{
var navBar = base.CreateNavigationController(viewController);
navBar.NavigationBarHidden = true;
return navBar;
}
This will hide the navigationBar for every view and make sure you get fullscreen views.
Let me know if this helps.
I was found best solution for this by override ViewWillAppear to hide navigation bar.
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
NavigationController.SetNavigationBarHidden(false, false);
}
I am learning WPF with M-V-VM. And I am using ICommand, RelayCommand.
I have several Views, Models, and ViewModels.
The MainWIndowView open upon on application start. The MainWindowView has a button that opens another WPF window called “FileListview” via MainWindowViewModel.
The FileListView has a button “View Lookup”, which supposed to open another WPF window called “LookupView” via FileListViewModel. But I could not make this button to work unless I specify FileListView in App.xaml.cs instead of MainWIndowView. I could not understand why “View Lookup” button work if I make application to start from “FileListView” . I also don’t understand whether I need model for MainWindowView, and FileListView since I don’t have anything going except one view’s button is opening another view.
On code behind file “App.xaml.cs” I have
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
WPFProject. MainWIndowView window = new MainWIndowView ();
MainWIndowViewModel VM = new MainWIndowViewModel ();
window.DataContext = VM;
window.Show();
}
}
I would appreciate if somebody can point me to good article or sample code using WPF with M-V-VM that reflect my issue.
Here is my approach to use dialogs/child windows with mvvm and wpf. please note the comment from sllev and post all relevant code.
After rethinking the issue, I was able to figure out the solution.
The cause of the issue: I was not associating View with it’s ViewModel class.
So I put the following code in code behind of FileListView.xaml.cs.
public partial class FileListView: Window
{
private FileListViewModel _ fileListViewModel = new FileListViewModel ();
public FileListViewModel ()
{
InitializeComponent();
base.DataContext = _fileListViewModel;
}
}
Thank you
I'm using Monotouch to develop an app for the iPhone.
In my iPhone only app, I have an InputAccessoryView appear whenever the user selects a textfield. The accessory view provides buttons which aid the user (undo/redo etc).
It works fantastically on the simulator and on iPhone devices.
However, out of the blue, the Input Accessory View is not appearing on the iPad. I've made no changes to the code regarding the views; I've even rolled back to a version that I know displayed the accessory view correctly.
I was wondering if anyone else has come across this behaviour before / would know why this is happening?
EDIT
I've seen this accross all of my iphone projects running on the ipad. I made a fresh project which only contains 1 view, a UITextField and override the Input Accessory View and I'm still seeing nothing.
Code I'm using to test the override of the input view is:
public override UIView InputAccessoryView
{
get
{
UIView view = new UIView(new RectangleF(0,0,320,30));
view.BackgroundColor = UIColor.Blue;
return view;
}
}
Nothing too complex, on the iPhone just returns a blue bar above the keyboard.
I've reinstalled Mono, MonoDevelop, Monotouch and the iOS SDK multiple times to no avail. Apps that I've downloaded from the store still show the Input Accessory View, so I'm beginning to wonder if it's an issue with my Monotouch/iOS SDK combo? I'm using Monotouch 3.1.3 personal and 4.1 iOS SDK - version 2.6 of Mono. I'm going to try updating to version 2.8 of Mono.
The thing I don't understand is why it would work previously then all of a sudden just stop working?
When I'm deploying the code on the iPad, I'm selecting "Rebuild all", then uploading to the device. It doesn't matter if I pick either Release/Debug as a build, both yield the same result.
EDIT 2
If I subclass a UITextField and override the InputAccessoryView within that subclass, then the view appears on the iPad. This means that the InputAccessoryView which is being overriden in the View class isn't being assigned to the Textfield on the ipad.
EDIT 3
It would appear this is fixed in iOS 4.2 with Monotouch 3.2 !
My first suggestion is to reset the device in iTunes. You may have done so already, but just in case you did not, please do it first (since it's quick and easy to do).
If that didn't help: To identify if it is a problem with Monotouch and/or the iOS SDK could you repeat what you did above using only Cocoa Touch?
I.e., start a View-based app in XCode, add one UITextField in Interface Builder and connect it to the controller. Here is a corresponding Objective-C code to do it:
Instance variables in your controller (add to the #interface section in the <controllername>.h file):
IBOutlet UITextField *text;
UIView *accessory;
Hook up the UITextField in Interface Builder with the text outlet and then add the following implementation of viewDidLoad to the controller (the #implementation section in the <controllername>.m file):
- (void)viewDidLoad {
accessory = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
[accessory setBackgroundColor:[UIColor blueColor]];
[text setInputAccessoryView:accessory];
[super viewDidLoad];
}
If this displays the blue bar correctly, the problem is with the setup of Monotouch. If it does not, it is an issue with your device.
I had the same issue where a UITextView contained within a UIView would show the accessory bar on the simulator but not on the iPhone or iPad.
I'm doing some strange things with BeginFirstResponder to allow tapping anywhere in the larger parent area (containing a label and the text field) and activating the contained field and suspected this was involved. I added code in the parent UIView to override the InputAccessoryView and have it return the the value from the child UITextView and this fixed.
I suspect that Monotouch has an issue routing the event to the wrong object and my fix resolved it.
pseudo code:
public class ParentView : UIView
{
UILabel Label;
MyTextView Text;
/*Magic line here that fixed the problem */
public override UIView InputAccessoryView {get{return Text.InputAccessoryView;}}
}
public class MyTextView : UITextView
{
UIView Accessory;
public override UIView InputAccessoryView {get{return Accessory;}}
}
How are you showing your accessory view?
Can you try keeping a reference to the view?
Try this, and let me know if this fails:
public class Foo {
static UIView track;
}
And then when you create your view, stick it in that global:
Foo.track = new MyAccessoryView ()