Xamarin forms MVVM Remove the previous VM in my current VM - xamarin

I have a main page : HomeVM.
When the app starts, I use PushModalAsync for RegistrationVM
In my RegistrationVM, I have a button for pushModalAsync -> LoginVM
When the user is connected, I want go back in the VM HomeVM, but impossible, with PopModalAsync, I come back to RegistrationVM.
I would like to close all popmodalasync.
I have tried this
Here in my app.xaml :
var mainPage = (Page)ViewFactory.CreatePage(typeof(HomeVM));
MainPage = new NavigationPage(mainPage)
{
BarBackgroundColor = (Color)Resources["PrimaryColor"],
BarTextColor = Color.White,
};
Navigation = MainPage.Navigation;
protected override void OnStart()
{
var registrationpage = (Page)ViewFactory.CreatePage(typeof(RegistrationVM));
MainPage.Navigation.PushModalAsync(registrationpage);
}
RegistrationVM :
public RegistrationVM()
{
Task.Run(async () => await ConnectionAPI());
}
async Task ConnectionAPI()
{
try
{
applicationContext.Device = mydevice;
await Navigation.PushModalAsync<LoginVM>(async (vm, p) => await vm.InitializeAsync(this));
}
catch (Exception e)
{
Log.Error(e, "Unhandled exception while loading touch points : {e}", e);
}
finally
{
}
}
LoginVM :
When the user click on "Login"
PreviousVM = RegistrationVM.
async Task Login()
{
try
{
Log.Information("Logging in");
applicationContext.User = employe;
await Navigation.PopModalAsync();
await Navigation.RemoveAsync(previousVM);
}
catch (Exception e)
{
Log.Error(e, "Unhandled exception during log in : {e}", e);
}
finally
{
}
}
I come back to Registration view

If you want to just pop all modal windows, you can should be able to keep calling PopModalAsync() until Application.Current.MainPage.Navigation.ModalStack.Count is 0, or you get an ArgumentOutofRangeException.
That said, you might want to consider using a different approach here. Most apps that require a login page don't want to have any other pages loaded while it's displayed. In other words, you don't want the user to be able to back out to your main page unless they are logged in, so you don't want anything else on the stack.
To do this, you can replace the Application.Current.MainPage with your login and/or registration page. When login is complete, you replace it with your app's main navigation page.

Related

Fetching data from the internet after a page opens in Xamarin forms

I am having some performance issues with my code. I am working with basic MVVM in Xamarin forms project, and I want to fetch data from the internet when someone navigates to another page. Below is what I have done;
This is how I am navigating to another page via Command; (To be honest I don't really know if this method of navigation has some performance penalties)
if (Application.Current.MainPage.Navigation.NavigationStack.Last().GetType() != typeof(SubcategoryPage))
{
await Application.Current.MainPage.Navigation.PushAsync(new SubcategoryPage());
}
Here, I am passing Id of a particular category to get its corresponding subcategories
public SubcategoryPage(int id)
{
InitializeComponent();
this.BindingContext = new SubcategoryPageViewModel(id);
}
In the constructor of the SubcategoryPageViewModel, I used the Id to fetch the data online like so;
public SubcategoryPageViewModel(int id)
{
SubcategoryLoader(id);
}
Below is the method that is fetching the data from the internet through my DataService class. The code below work well in getting the data from the internet;
private async Task SubcategoryLoader(int id)
{
try
{
var subCategories = await SubcategoryDataService.GetSubcategories(id);
if (subCategories.code == 1) // StatusCode = Successful
{
SubCategories = subCategories.document;
}
else
{
await Application.Current.MainPage.DisplayAlert("Oops!","Something went wrong", "Ok");
}
}
catch (Exception ex)
{
await Application.Current.MainPage.DisplayAlert("Oops!", ex.Message, "Ok");
}
}
Now my problem is that the SubcategoryPage doesn't open until the online service is over which leads to a serious lags. So what I want to have happen is to open the SubcategoryPage before the internet services happen.
Please any assistance is appreciated.
Move it to OnAppearing method .
Add a simple check .
protected override void OnAppearing()
{
base.OnAppearing();
if(BindingContext == null)
{
BindingContext = new SubcategoryPageViewModel(id);
}
}

IntentService in Xamarin PCL Solution

I am busy writing an application where the user needs to capture a lot of images and then they get packaged together with some text data and then they get uploaded to a local server. I want to implement the uploading on the Android platform through an Intent Service but I cannot find a good Xamarin Forms PCL example to show me how.
This is the method where I initialize the Intent to pass to the IntentService:
public async Task<bool> UploadAsync(Uri serviceAddress,
CaptureEntity capture,
List<ImageEntity> images)
{
try
{
Intent uploadIntent = new Intent();
uploadIntent.PutExtra("serviceAddress", serviceAddress.ToString());
uploadIntent.PutExtra("captureId", capture.WorkflowId.ToString());
StartService(uploadIntent);
return true;
}
catch (Exception exc)
{
App.logger.LogError(DateTime.Now, "Uploader", exc.ToString());
throw exc;
}
}
And this is the IntentService itself.
[Service]
public class ServiceIntent : IntentService
{
public ServiceIntent() : base("ServiceIntent")
{
}
//[return: GeneratedEnum]
public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
{
return base.OnStartCommand(intent, flags, startId);
}
public override void OnCreate()
{
base.OnCreate();
}
protected override void OnHandleIntent(Intent intent)
{
Uri serviceAddress = new Uri(intent.GetStringExtra("serviceAddress"));
Guid captureId = Guid.Parse(intent.GetStringExtra("captureId"));
CaptureEntity capture = new DatabaseConnection_Android().CreateConnection().Query<CaptureEntity>("SELECT * FROM [CaptureEntity]").Single(c => c.WorkflowId == captureId);
var images = new DatabaseConnection_Android().CreateConnection().Query<ImageEntity>("SELECT * FROM [ImageEntity]").Where(i => i.CaptureEntityId == capture.Id);
try
{
MultipartFormDataContent content = new MultipartFormDataContent();
StringContent strContent = new StringContent(
capture.XmlData,
Encoding.UTF8,
"text/xml");
IImageHandler handler = new ImageHandler_Droid();
HttpRequestMessage request = new HttpRequestMessage();
request.Headers.Add("workflow", capture.WorkflowId.ToString());
request.Method = HttpMethod.Post;
request.RequestUri = serviceAddress;
foreach (var image in images)
{
byte[] imageByte = handler.ReadAllBytes(image.ImagePath);
ByteArrayContent byteContent = new ByteArrayContent(imageByte);
byteContent.Headers.Add("Content-Type", "image/jpeg");
content.Add(byteContent, "file", image.ImageName);
}
content.Add(strContent, "text/xml");
request.Content = content;
using (HttpClient client = new HttpClient())
{
client.Timeout = TimeSpan.FromSeconds(180);
var response = client.SendAsync(
request,
HttpCompletionOption.ResponseContentRead).Result;
var readResponse = response.Content.ReadAsStringAsync().Result;
if (readResponse == "File uploaded.")
MessagingCenter.Send<CaptureEntity, string>(
capture,
"Completed",
"Success");
else if (readResponse.Contains("An error has occurred."))
MessagingCenter.Send<CaptureEntity, string>(
capture,
"Uploader",
String.Format(CultureInfo.InvariantCulture,
"Failed: {0}",
readResponse));
else
MessagingCenter.Send<CaptureEntity, string>(
capture,
"Uploader",
String.Format(CultureInfo.InvariantCulture,
"Failed: {0}",
readResponse));
}
}
catch (WebException webExc)
{
MessagingCenter.Send<string, string>("Uploader", "Failed",
String.Format(CultureInfo.InvariantCulture,
"{0} upload failed.\n{1}",
capture.DisplayName,
webExc.Message));
}
catch (TimeoutException timeExc)
{
MessagingCenter.Send<string, string>("Uploader", "Failed",
String.Format(CultureInfo.InvariantCulture,
"{0} upload failed.\n{1}",
capture.DisplayName,
timeExc.Message));
}
catch (Exception exc)
{
MessagingCenter.Send<string, string>("Uploader", "Failed",
String.Format(CultureInfo.InvariantCulture,
"{0} upload failed.\n{1}",
capture.DisplayName,
exc.Message));
}
}
}
Can anyone tell me what I am doing wrong as I am getting the following error when I want to start the service:
Java.Lang.NullPointerException: Attempt to invoke virtual method 'android.content.ComponentName android.content.Context.startService(android.content.Intent)' on a null object reference
In your Intent declaration you need to tell the service you want to call
Something like this:
var uploadIntent = new Intent(this, typeof(ServiceIntent));
Note: this represents the Context.
Update:
As mentioned in the comments your interface implementation cannot derive from Activity class. In order to have access to the Context to be able to call the StartService method and also create your Intent you can make it in two ways:
Using the Xamarin.Forms.Forms.Context:
public async Task<bool> UploadAsync(Uri serviceAddress,
CaptureEntity capture,
List<ImageEntity> images)
{
try
{
var context = Xamarin.Forms.Forms.Context;
var uploadIntent = new Intent(context, typeof(ServiceIntent));
uploadIntent.PutExtra("serviceAddress", serviceAddress.ToString());
uploadIntent.PutExtra("captureId", capture.WorkflowId.ToString());
context.StartService(uploadIntent);
return true;
}
catch (Exception exc)
{
App.logger.LogError(DateTime.Now, "Uploader", exc.ToString());
throw exc;
}
}
If you are using latest versions of Xamarin.Forms this global context was deprecated and they suggest to you local context instead. You can still use it though but in future updates of XF your app might break.
using CurrentActivity plugin:
public async Task<bool> UploadAsync(Uri serviceAddress,
CaptureEntity capture,
List<ImageEntity> images)
{
try
{
var context = CrossCurrentActivity.Current.Activity;
var uploadIntent = new Intent(context, typeof(ServiceIntent));
uploadIntent.PutExtra("serviceAddress", serviceAddress.ToString());
uploadIntent.PutExtra("captureId", capture.WorkflowId.ToString());
context.StartService(uploadIntent);
return true;
}
catch (Exception exc)
{
App.logger.LogError(DateTime.Now, "Uploader", exc.ToString());
throw exc;
}
}
This plugin can be installed from nugget and the setup is very straight forward. Basically it gives you access to the current activity and you can use it as your context to call the IntentService
Hope this helps.-
Here is the IntentService.
IntentService is a base class for Services that handle asynchronous requests (expressed as Intents) on demand. Clients send requests through startService(Intent) calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work.
In Android, we usually use IntentService to do asynchronous operator. As we all know, thread is also used to do asynchronous operator. The difference between IntentService and Thread is IntentService is Service which belongs to Android Component. So, the priority of IntentService is higher than Thread.
For example, there is a ActivityA which has a IntentService, and there is a ActivityB which has a Thread, both IntentService and Thread are working, and both ActivityA and ActivityB are al background Activity. Now, if your phone's system doesn't have extra resources, your ActivityB will be killed firstly.
About the Exception:
Java.Lang.NullPointerException: Attempt to invoke virtual method 'android.content.ComponentName android.content.Context.startService(android.content.Intent)' on a null object reference
That means you should use android.content.Context to call the StartService method. In Android, there are three kinds of Context. Application, Activity and Service. So you can call the StartService method in these three classes directly. If you are not in these three classes, you need pass the Context to your class, and then use the Context to call StartService.
I added Activity for this class' inheritance.
If you do this, your class will be a Activity, and you need to register it in your manifiest, add layout for your class, and it should have the lifecycle, and etc. It will not be what you want to get class. In Android, Activity is a Component, not normal class, so you can't inherit it unless you want your class to be a Activity.
Demo:
I have made a demo for you,

App.OnResume error in Xamarin forms on Android and IOS devices

We are using xamarin forms. After an Android or IOS device resumes from background, we are making a REST call in .net that is being triggered by a timer. The first attempt on IOS returns a "The Descriptor is not a socket" error and the Android returns a "Connection refused" error. The same code works fine in Windows. Future attempts (every few seconds) in all 3 platforms work fine. Has anyone seen this and have a fix?
Code
//app on resume event
protected async override void OnResume()
{
// Handle when your app resumes
if (MainPage is RootPage)
{
RootPage mainPage = MainPage as RootPage;
if (mainPage.Detail is NavigationPage)
{
NavigationPage nvPage = mainPage.Detail as NavigationPage;
if(nvPage.CurrentPage is ThingsPage)
{
ThingsPage thPage = nvPage.CurrentPage as ThingsPage;
thPage.TurnOnTimer();
}
}
}
}
//code on the page
public void TurnOnTimer()
{
if (viewModel != null)
{
viewModel.ContinueTimer = true;
viewModel.StartAnotherTimer();
}
}
//code in view model
public async void StartAnotherTimer()
{
while (ContinueTimer)
{
try
{
DevicesUpdate devicesUpdate = await DataSource.GetDevices(LocationID, ControllerID, lastDevicesUpdateReceivedAt);
}
catch (Exception ex)
{
}
// Update the UI (because of async/await magic, this is still in the UI thread!)
if (ContinueTimer)
{
await Task.Delay(TimeSpan.FromSeconds(3));
}
}
}
public static async Task<DevicesUpdate> GetDevices(Guid locationID, Guid controllerID, DateTime lastUpdateReceivedAt)
{
DevicesUpdate devicesUpdate = await GetLastUpdatedDevices(controllerID, lastUpdateReceivedAt);
}
//code in view model
public static async Task<DevicesUpdate> GetLastUpdatedDevices(Guid controllerID,
DateTime lastUpdate)
{
System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
string url = string.Format("http://appname.azurewebsites.net/api/devices?controllerid={1}&lastUpdate={2}"
, Constants.WebServerURL, controllerID, lastUpdate);
System.Net.Http.HttpResponseMessage response = await client.GetAsync(new Uri(url));
string result = await response.Content.ReadAsStringAsync();
if (response.IsSuccessStatusCode)
{
DevicesUpdate devices = JSONHelper.Deserialize<DevicesUpdate>(result);
return devices;
}
else
{
if (response.ReasonPhrase == "UserException")
{
throw new UserException(result);
}
else
{
//throw error because the response from rest api is not a success
throw new System.Net.Http.HttpRequestException(result);
}
}
}
You might have a few things happening here that's causing problems.
GetDevices doesn't return anything. (I hope you just left out the return for brevity sake)
You are never setting ContinueTimer to false.
What iOS version are you on? In later versions, you HAVE to use HTTPS or explicitly allow non-secure connections. This shouldn't be a problem because Azure has ssl.
If you plan on running this in the background, you need to register your app as a background process.
If you don't plan on running this in the background, you might have issues with previous attempts being ran (or still trying to execute, or just have failed) and then calling more.
What is the reason for calling the 3 second timer for the network calls? What if the call takes more than 3 seconds (then you are making duplicate calls even though the first might succeed).
If you want to make your network calls more robust, check out this Blog Post by Rob Gibbons about resilient network calls.
First thing I would do is remove it from the timer because it seems like the underlying sockets are having issues cross-thread.

Unable to clear cache of WebBrowser control in windows phone 8

I have used a WebBrowser control in my windows phone application. Using webbrowser control i am logging into my application. On log-out i simply redirect the user to log-in page again and trying to clear the cache and cookies of the webbrowser control.
mybrowser.ClearCookiesAsync();
mybrowser.ClearInternetCacheAsync();
instead of showing log-in screen, it get the previous credentials and logged-in into the application. Can anyone please help me in clearing the cookies and cache of webbrowser conteol.
Complete code:
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
App.AccessToken = "";
myBrowser.Navigate(new Uri(login URL));
}
void Navigating(object sender, NavigatingEventArgs e)
{
if (Login complete)
{
clearCahe();
}
}
public async void clearCahe()
{
await myBrowser.ClearCookiesAsync();
await mybrowser.ClearInternetCacheAsync();
}
async functions are hard to get use to. If you put a break point at
await myBrowser.ClearCookiesAsync(); // put a break point here then step-in (F11 on the keyboard)
You will see that will not actually await and will continue on its merry way.
To fix this you have to do something like this
using System.Threading.Task;
public async Task<bool> clearCahe()
{
await myBrowser.ClearCookiesAsync();
await mybrowser.ClearInternetCacheAsync();
return true;
}
and a calling function that also async
like so
private async void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
await clearCache();
// then navigate the page
}

OAuthException not catched with C# FacebookSDK

I try to get my code working with catching certain errors. I store the token for a user after he or she grants permission to my app (this is a WP7 app). When I try to post on the wall by using the stored token it works. When I remove the permissions on facebook it throws an OAuthException. I can't catch it it seems. My app just crashes. This is the code I used:
private object PostToFacebook()
{
_fbApp = new FacebookClient(_appsettings.faceBookToken);
FacebookAsyncCallback callback = new FacebookAsyncCallback(this.postResult);
var parameters = new Dictionary<string, object>();
parameters.Add("message", "message on wall");
try
{
_fbApp.PostAsync("me/feed", parameters, callback);
}
catch (Exception ex)
{
}
return null;
}
private void postResult(FacebookAsyncResult asyncResult)
{
if (asyncResult.Error == null)
{
status = "succes";
}
else
{
status = "error" + asyncResult.Error.Message;
}
}
The try catch doesn't catch anything and the generic exception handler in my app.xaml.cs either.
Any ideas how to catch this error so I can ask the user to authenticate again?
Put your try..catch in the callback.
You can also catch exceptions globally by handling the UnhandledException event on the App object.

Resources