Xamarin Android: Can I not create a new object of my MainActivity? - object

I have a location mocking method in my main activity. Unfortunately, I cant put this method into another class (yet!). So, I need a service, to call this method from my main activity every 5 seconds. So i created a countdown in within a service that, while the app is in the background, should run the method in my MainActivity. But it doesnt.
public void OnTimedEvent(object sender, System.Timers.ElapsedEventArgs e)
{
Log.Info("2", "CountDown ausgeführt!");
var test = new MainActivity();
test.getMockLocation();
}
This is my code. As you can see, I'm installing a new object of my Main Activity and then ask for the method in within this activity. This does work. Well at least Visual Studio does not complain. If I now debug my app on my phone, nothing happens. I dont get no errors or anything.
Now, when I run this app Step by Step and it reaches this point
"var test = new MainActivity();"
I get "Frame not in Module".
So, it basically crashes as soon as I ask it to install a new object of my Main Activity.
May anybody tell me why this is?
THANKS :)

Unfortunately in Android you cannot create Activities like this, they need to be instantiated by the OS. Also, instantiate a whole Activity only for a method is not ideal, I suggest you to find the way to get that method/function out of that Activity so you can use it anywhere in your program.

Did you create your app via Xamarin Forms? If you did, you can utilize the Xamarin Forms MessagingCenter for background services and then you can call your mock location tasks.
This is the link for a very helpful walk-through and example of MessagingCenter.

Related

Events not tracked in App Center

I am trying new Visual Studio App Center platform for mobile apps. It gives me the crashes and the installed versions OK, so the app secret and SDK are configured OK.
But when I try to track custom Events, according to this tutorial I get "No events found" in the Mobile Center dashboard. I try with my app in release and debug mode, without results.
My code (Xamarin.Forms):
public MyClass()
{
InitializeComponent();
Analytics.SetEnabledAsync(true);
Analytics.TrackEvent("Video clicked", new Dictionary<string, string> {
{ "Category", "Music" },
{ "FileName", "favorite.avi"}
});
}
There is the constructor, so I am sure that these lines are executed.
MobileCenter.Start needs to be called before Analytics.TrackEvent or Analytics.SetEnabledAsync.
If you are using constructor, then you need to move MobileCenter.Start to constructor as well.
Your solution is working probably because you made that code execute later (and thus after MobileCenter.Start) with async but you don't need to do that (and you don't need to call SetEnabledAsync at all, it's true by default and persisted).
Solved. I need to execute the lines in an async method, not in the constructor.

Hosting and initialising multiple MVVMCross ViewModels for the MvxTabViewController

I need to create a Tab view in iOS. I tried to do this by simply adding a UITabBar to an MvxViewController but I could not get that to work (See this question if interested)
I am now trying to go use the approach MVVMCross and Apple seem to push me towards, a TabController.
In our Core project we have 4 ViewModels that are displayed from one or more places using a ShowViewModel call and are initialised with a customerNumber.
CustomerViewModel
CustomerOrdersViewModel
CustomerHistoryViewModel
ReturnsViewModel
In Android and Windows Store we have separate Views for each of these ViewModels. I do not want to change our Core implementation to support iOS. I will create a new ViewModel called TabbedCustomerViewModel and that will have 4 properties, one for each of the hosted ViewModels.
Using a custom MvxTouchViewPresenter registered in the iOS project I can listen for requests to Show one of the Customer views and then switch the Request to TabbedCustomerViewModel. I have that working, the new ViewModel gets created and the initialisation paramaters that would have been passed to the original VM are passed to the intercepting VM's Init method.
The problem I am having is knowing how I should be initialising the hosted VMs. I assume I am going to have to ctor, init and Start them manually. Has anyone got any thoughts on how to do this?
The N-25 Tab Tutorial does not have to worry about this as its hosted VMs are not standalone so don't have Init and Start dependencies
I have the initialisation of the VMs working now too. When the View loads it checks if it is within a Tabbed UI by looking at the ParentViewController.
If it is it calls a custom method on the new TabbedCustomerViewModel. I have copied the code that MVVMCross use and added it to the new Method
try
{
mvxViewModel.CallBundleMethods("Init", this.initialisationParameters);
if (reloadedState != null)
{
mvxViewModel.CallBundleMethods("ReloadState", reloadedState);
}
mvxViewModel.Start();
}
catch (Exception exception)
{
throw exception.MvxWrap("Problem initialising viewModel of type {0}", mvxViewModel.GetType().Name);
}
The initialisationParameters and reloadedState are stored by TabbedCustomerViewModel when it is initialised so that it can pass it down to the ViewModels it is hosting

API Breaks past 8-9 Devices?

It would seem that streaming breaks when there is too many devices in the account. After about 8 or 9 it just stops streaming data to me all together.
Are you using Firebase? I came on here to post a similar issue myself. If I change the temp through the nest device or the web tool, my Firebase listeners are updated. If I try to set a value, the value that I try to set is echoed back to my listener (like there has been an update on the thermostat even though it wasn't changed successfully) then, the correct value (unchanged) comes immediately after.
The weird thing is that it works.... then it just doesn't. Is this similar to what you've been experiencing?
Update:
Now it appears as if my listeners are not working either. I can query the server using REST successfully.
Update #2:
Now my listeners are working again but still no control.
Update#3:
Well... I think I see my problem at least. I don't know if it will help you (or me for that matter) but here it is...
protected void setHighTemp(int value){
fb.child("target_temperature_high_f").setValue(value, new CompletionListener() {
public void onComplete(FirebaseError arg0, Firebase arg1) {
System.out.println("Communicaiton error: " + arg0);
}
});
Output:
Communicaiton error: FirebaseError: Too many requests
I remember reading the following paragraph in https://developer.nest.com/documentation/glossary#client
Client
An integration of your application or service with Nest
devices. When you create a Nest account and sign up for the Developer
Program, you can add up to 10 clients to the account.
This might be your problem.

Handling WinJS app upon relaunch

When back home is pressed app exists but it is not terminated yet.
When user press primary or secondary tile app is relauched.
Default way is to let application navigate to the last visited page in the navigation history.
I don't know if there is a bug but this way doesn't work as expected because any code inside page ready function executes but it doesn't count later when page is rendered. Static binding works but not dynamic.
I need to know what is the proper way of handling relaunch in an app that uses default navigation template?
What to do if I want clean start, destroy everything and than navigate to home?
How to overcome problem with framework not taking into consideration code inside page ready function?
Upon app initialization you should check for the ApplicationExecutionState, and do whatever you want in either case.
Thanks for your answer but it is quite clear from the start how to obtain ApplicationExecutionState.
Actually what I need was to execute all bindings and other post processing after DOM has been loaded in a promise timeout.
if (app.sessionState.previousExecutionState === 1) {
WinJS.Promise.timeout().then(function () {
performeAfterProcessing();
});
}
else {
performeAfterProcessing();
}
So if everyone encounters some strange behavior after application has been relaunched try to execute your code using promise timeout.

An exception of type 'Microsoft.VisualStudio.TestTools.UITest.Extension.FailedToPerformActionOnHiddenControlException'

Im getting the following error when I run the coded UI application:
An exception of type 'Microsoft.VisualStudio.TestTools.UITest.Extension.FailedToPerformActionOnHiddenControlException' occurred in Microsoft.VisualStudio.TestTools.UITesting.dll but was not handled in user code
This exception arises in the Mouse.Click() function in the below code.
public static void DestinationMaster()
{
Mouse.Hover(PPI.PPIHome.PPI_Main.PPI_Window.MastersPane);
Mouse.Click(PPI.PPIHome.PPI_Main.PPI_Window.DestinationMasterPane.DestinationMasterHyperlink);
}
The application doesnt run after this exception.I am using IE 8 as my browser to run the application.But when I run the application ,IE mode is changed automatically to compatibility mode.Is this related to the exception?
Is there a way to resolve this issue and get my application to running.Thanks in advance.
Does the hover work? Can you hover over the link instead to make sure that works? What happens when you find the control from the UIMap?
What does PPI.PPIHome.PPI_Main.PPI_Window.DestinationMasterPane.DestinationMasterHyperlink.TryGetClickablePoint() return?
Does the UI need to scroll to see the link? If so you could use PPI.PPIHome.PPI_Main.PPI_Window.DestinationMasterPane.DestinationMasterHyperlink.EnsureClickable() to scroll to the control.
Try to use WaitForControlReady() to make sure the page is fully loaded before coded ui acts on it. Sometimes coded ui can move faster than the application under test.
Make sure you have the latest update for VS2012

Resources