Is it possible to show DisplayAlert on startup (Xamarin.Forms) - c#

In my mobile application (xamarin forms), I'm getting data from internet so it needs internet connection. Since I have a dictionary which I initialize in App.xaml.cs and I use data from internet, I need to check for internet connection. I have seen this question where OP asks for something similar, but the answer doesn't work for me since I need to check for internet connection whenever app launches, not after MainPage is launched. For example, Clash of Clans. Whenever the app launches, the app checks for internet connection and if there's no connection, it displays a alert to user repetitively until there's a connection.
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using System.Collections.Generic;
using HtmlAgilityPack;
using System.Text.RegularExpressions;
using System;
[assembly: XamlCompilation(XamlCompilationOptions.Compile)]
namespace Multi
{
public partial class App : Application
{
static GroupStage groupstage = new GroupStage();
public static HtmlWeb web = new HtmlWeb();
public static HtmlDocument doc = LoadUrlAndTestConnection();
//The reason why I have put a method is because I wanted to try if I can use try-catch to display alert, however this didn't work.
public static HtmlDocument LoadUrlAndTestConnection()
{
bool con = true;
while (con)
{
try
{
doc = web.Load(someURL);
}
catch (Exception ex)
{
var sth = new ErrorPage();
sth.InternetErrorDisplay();
con = true;
continue;
}
con = false;
}
return docSK;
}
public static Dictionary<string, Country> _countries = new Dictionary<string, Country>
{
["Australia"] = new Country(1, "Australia", false, "AU", "ausFlag.png", 3, groupstage, GetScore("Australia", 3)),
public static string[] GetScore(string name, int GroupID)
{
//Gets the score data from internet
}
public App()
{
InitializeComponent();
TwitchClass.MainAsync().Wait();
MainPage = new OpeningPage();
}
protected override void OnStart()
{
}
protected override void OnSleep()
{
}
protected override void OnResume()
{
}
}
}
//GetScore method requires internet connection as it gets the score data from internet.
and the InternetErrorDisplay method is,
public void InternetErrorDisplay() => DisplayAlert("Connection Error", "Could not detect internet connection. This application requires access to internet.", "Retry");
Is it possible to have this behaviour in xamarin forms app? How can I achieve it?

Yes, why should it not be possible?
Here is an example which uses async/await
using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using System.Threading.Tasks;
[assembly: XamlCompilation(XamlCompilationOptions.Compile)]
namespace LoadingSample
{
public partial class App : Application
{
public App()
{
InitializeComponent();
//MainPage = new MainPage();
}
protected override async void OnStart()
{
// shows Loading...
MainPage = new LoadPage();
await Task.Yield();
// Handle when your app starts
// Just a simulation with 10 tries to get the data
for (int i = 0; i < 10; i++)
{
await Task.Delay(500);
// await internet_service.InitializeAsync();
await MainPage.DisplayAlert(
"Connection Error",
"Unable to connect with the server. Check your internet connection and try again",
"Try again");
}
await Task.Delay(2000);
// after loading is complete show the real page
MainPage = new MainPage();
}
protected override void OnSleep()
{
// Handle when your app sleeps
}
protected override void OnResume()
{
// Handle when your app resumes
}
}
}

Related

Xamarin forms - can't scan BLE devices

I new in Xamarin forms and C#.
I am trying to scan BLE device using the native Xamarin forms API, the code attached. I am using the Xiomi note 5 running android 9.
using Android.Bluetooth;
using Android.Bluetooth.LE;
using Android.Runtime;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using Xamarin.Forms;
namespace bletest
{
// Learn more about making custom code visible in the Xamarin.Forms previewer
// by visiting https://aka.ms/xamarinforms-previewer
[DesignTimeVisible(false)]
public class MyScanCallback : ScanCallback
{
public override void OnBatchScanResults(IList<ScanResult> results)
{
base.OnBatchScanResults(results);
}
public override void OnScanResult([GeneratedEnum] ScanCallbackType callbackType, ScanResult result)
{
base.OnScanResult(callbackType, result);
}
public override void OnScanFailed([GeneratedEnumAttribute] ScanFailure errorCode)
{
base.OnScanFailed(errorCode);
}
}
public class newbtle
{
private readonly BluetoothAdapter _ba;
private readonly BluetoothLeScanner _sc;
private readonly MyScanCallback _scCb;
public newbtle()
{
_ba = BluetoothAdapter.DefaultAdapter;
_sc = _ba.BluetoothLeScanner;
_scCb = new MyScanCallback();
}
public void BleScan()
{
if (_ba.Enable() == true)
{
_sc.StartScan(_scCb);
}
}
public void BleScanStop()
{
_sc.StopScan(_scCb);
_sc.FlushPendingScanResults(_scCb);
_ba.Disable();
}
public string GetScanMode()
{
return _ba.ScanMode.ToString();
}
public string GetStateMode()
{
return _ba.State.ToString();
}
}
public partial class MainPage : ContentPage
{
newbtle bt = new newbtle();
public MainPage()
{
InitializeComponent();
}
private void Button_Clicked(object sender, EventArgs e)
{
bt.BleScan();
}
private void Button_Clicked_1(object sender, EventArgs e)
{
bt.BleScanStop();
}
}
}
When a call is made to bt.BleScan no call back is called, when the bt.BleScan called in the second time without turning the scanner off the OnScanFailed is called with 'already active error'.
When I run the BLE explorer utility on the same mobile in the same environment it scan few BLE devices.
Any suggestion what can be the problem?
Thanks

Xamarin displayalert not showing

I have xamarin form application and i connected it signalr not running my void. I searched on internet but I can't find anything about this. And this is my code
Myhub.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR.Client;
namespace PharmClient
{
class MyHub
{
string url = "https://webapplication11-co5.conveyor.cloud/";
HubConnection Connection;
IHubProxy ProxyOFServer;
public delegate void Error();
public delegate void MessageRecieved(string _data);
public event Error CoonectionError;
public event MessageRecieved OndataRecieved;
public delegate void Completed();
public event Completed OnCompleted;
public void Connect()
{
Connection = new HubConnection(url);
ProxyOFServer = Connection.CreateHubProxy("MuHub");
Start().ContinueWith(task => { if (task.IsFaulted) { CoonectionError?.Invoke(); } else { OnCompleted?.Invoke(); } });
}
public Task Start()
{
return Connection.Start();
}
public void SendData(string data)
{
ProxyOFServer.Invoke<string>("SendMessage", data);
}
public void Recive( )
{
ProxyOFServer.On<string>("Sentdata", data => { OndataRecieved?.Invoke(data); });
}
}
}
MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using System.Threading;
namespace PharmClient
{
// Learn more about making custom code visible in the Xamarin.Forms previewer
// by visiting https://aka.ms/xamarinforms-previewer
[DesignTimeVisible(false)]
public partial class MainPage : ContentPage
{
MyHub ConnectServer = new MyHub();
public MainPage()
{
InitializeComponent();
NavigationPage.SetHasNavigationBar(this, false);
ConnectServer.OnCompleted += ConnectServer_OnCompleted;
ConnectServer.CoonectionError += ConnectServer_CoonectionError;
ConnectServer.Connect();
}
private void ConnectServer_OnCompleted()
{
DisplayAlert("Connected", "Good", "O");
}
private void ConnectServer_CoonectionError()
{
DisplayAlert("Failed", "Bad", "Ok");
}
private void SerchDrug_Clicked(object sender, EventArgs e)
{
Navigation.PushAsync(new SearchDrug());
}
}
}
When connection failed ConnectionError event run but Connection will be successfully OnCompleted event won't run. I am student. This is part of group work. What is problem my code any Help. I can't found anything. Thanks for attention
As your title suggest, you have an issue with displaying a dialog box.
Try going through the documentation (here) once for complete understanding, you have to await the process of displaying DisplayAlert.
Add await & async to your methods.
Try this -
private async void ConnectServer_OnCompleted()
{
await DisplayAlert("Connected", "Good", "O");
}
private async void ConnectServer_CoonectionError()
{
await DisplayAlert("Failed", "Bad", "Ok");
}
If you have some issues regarding, let me know.
You should await connection not fire and forget. Example:
private HubConnection connection;
private IHubProxy proxy;
public event EventHandler<ChatMessageObject> OnMessageReceived;
public async Task Connect()
{
try
{
await connection.Start();
await proxy.Invoke("Connect"); // example method in your backend
proxy.On("messageReceived", (int userId, string name, string message, DateTime messageDateTime) => OnMessageReceived(this, new ChatMessageObject
{
FromUserId = userId,
UserName = name,
MessageText = message,
MessageDateTime = messageDateTime
}));
}
catch (Exception ex)
{
//handle exceptions
}
}

Close with timeout is not working in Console to close splashscreen

I have made a class, I make an instance of. In said instance I have these lines of code to show and close the splashscreen.
// Open (show)
public void ShowSplashScreen(bool autoClose = false)
{
splashscreen.Show(autoClose, true);
}
// Close (don't show)
public void CloseSplashScreen()
{
splashscreen.Close(TimeSpan.FromSeconds(0.3));
}
It shows up fine, but never closes, just stays there.
This is the documentation of splashscreen Close: https://learn.microsoft.com/en-us/dotnet/api/system.windows.splashscreen.close?view=netframework-4.8
[System.Security.SecurityCritical]
public void Close (TimeSpan fadeoutDuration);
Note: I am using the show method with the parameters AutoClose set to false, and TopMost set to true, this makes it not auto close as I want to close it programmatically and not subscribe to existing events.
I am running the lines of code from a Console (.NET framework) application for testing purposes before implementing it into my UI fully.
What I have tried:
Debugging and even trying to call show again before calling close.
It is definitely something going wrong with the class, as calling the class and directly manipulating the property works:
ClassSplashScreen rss = new ClassSplashScreen();
rss.splashscreen.Show(false);
rss.splashscreen.Close(TimeSpan.FromSeconds(1));
My best guess is something is hanging the UI and freezing it? But I am unsure what to do about it.
Code to run to test this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace NamespaceName
{
public class StackOverFlowCode
{
static void Main(string[] args)
{
ClassSplashScreen screen = new ClassSplashScreen();
screen.ShowSplashScreen();
screen.CloseSplashScreen();
}
}
public class ClassSplashScreen
{
public SplashScreen splashscreen { get; set; }
public ClassSplashScreen()
{
splashscreen = new SplashScreen("Resource Image Link");
}
public void ChangeSplashResource(SplashScreen resource)
{
splashscreen = resource;
}
public void ShowSplashScreen(bool autoClose = false)
{
splashscreen.Show(autoClose, true);
}
public void CloseSplashScreen()
{
splashscreen.Close(TimeSpan.FromSeconds(1));
}
}
}
The SplashScreen relies on a dispatcher but there is no one in a console application by default. If you create a System.Windows.Application, it should work as expected:
public class StackOverFlowCode
{
[STAThread]
static void Main(string[] args)
{
Application app = new Application();
app.Startup += (s, e) =>
{
ClassSplashScreen screen = new ClassSplashScreen();
screen.ShowSplashScreen();
screen.CloseSplashScreen();
};
app.Run();
}
}
public class ClassSplashScreen
{
private readonly SplashScreen splashscreen;
public ClassSplashScreen() => splashscreen = new SplashScreen("Resource Image Link");
public void ShowSplashScreen() => splashscreen.Show(false);
public void CloseSplashScreen() => splashscreen.Close(TimeSpan.FromSeconds(1));
}

errors when adding a microsoft login page

I'm using this step by step guide (https://blog.xamarin.com/authenticate-mobile-apps-using-microsoft-authentication-library/) to implement a microsoft login page to my app in which users have to login with a microsoft account. But I've gotten to step 3 and can't find a way to get rid of the errors.
Code in XAML:
<StackLayout HorizontalOptions="Center" VerticalOptions="Center">
<Button Text="Login" x:Name="LoginButton"/>
</StackLayout>
</ContentPage.Content>
Code in code behind (C#):
using System;
using System.Collections.Generic;
using Microsoft.Identity.Client;
using Xamarin.Forms;
namespace RoseySports
{
public partial class Login : ContentPage
{
public IPlatformParameters PlatformParameters { get; set; }
public Login()
{
InitializeComponent();
LoginButton.Clicked += LoginButton_Clicked;
}
protected override void OnAppearing()
{
App.ClientApplication.PlatformParameters = PlatformParameters;
base.OnAppearing();
}
private async void LoginButton_Clicked(object sender, EventArgs e)
{
try
{
AuthenticationResult ar = await App.ClientApplication.AcquireTokenAsync(App.Scopes);
WelcomeText.Text = $"Welcome {ar.User.Name}";
}
catch (MsalException ex)
{
WelcomeText.Text = ex.Message;
}
}
}
}
Code in App.xaml.cs:
using Xamarin.Forms;
using Microsoft.Identity.Client;
namespace RoseySports
{
public partial class App : Application
{
public static PublicClientApplication ClientApplication { get; set; }
public static string[] Scopes = { "User.Read" };
public App()
{
InitializeComponent();
ClientApplication = new PublicClientApplication("your-app-id");
var content = new Login();
MainPage = new NavigationPage(content);
MainPage = new Login_Page();
}
protected override void OnStart()
{
// Handle when your app starts
}
protected override void OnSleep()
{
// Handle when your app sleeps
}
protected override void OnResume()
{
// Handle when your app resumes
}
}
}
And here is the screenshot with all the errors:error 1error 2error 3error 4
Also, could someone please explain to me where I should put this code in:
[assembly: ExportRenderer(typeof(Login), typeof(LoginPageRenderer))]
namespace MSALForForms.iOS
{
class LoginPageRenderer : PageRenderer
{
Login _page;
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
_page = e.NewElement as Login;
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
_page.PlatformParameters = new PlatformParameters(this);
}
}
}
You should add Microsoft.IdentityModel.Clients.ActiveDirectory as you're missing this reference as per the screenshot errors. You can get this by added by NuGet package in VS.
Right Click on References and click on "Manage NuGet Packages"
On Browse tab, type 'Microsoft.IdentityModel.Clients.ActiveDirectory' and install it by adding package.
Now use this namespace in your project. as - using Microsoft.IdentityModel.Clients.ActiveDirectory;

How I do a function C# for i can receive messages from Server Chat in PC and Chat Client in Android device?

In my C # application Chat Client I don`t not know how to do a function to receive messages from the Chat Server in C # and presents them in a TextView Chat Client
I'm programming in Visual Studio in C # using Mono for Android.
The question is... How I do a function for i can receive messages from Chat Server in PC and Chat Client in Android device? When i use Chat Client in windows 7 works good but when i make for a android device i can`t receive mensages from Server Chat to Chat Client.
I think the problem as in public void RecievedMessage and private void UpdateTextbox.
Sorry, my bad english.
Thanks your atention.
The Source Code, Chat Client:
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Widget.EditText;
using Android.Widget.Button;
using Android.Widget.TextView;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
using System.Runtime.InteropServices;
namespace ChatClient_Android
{
[Activity(Label = "ChatClient_Android", MainLauncher = true, Icon = "#drawable/icon")]
public class MainChat : Activity
{
public delegate void OnRecievedMessage(string message);
public MainChat form;
const int WM_VSCROLL = 0x115;
const int SB_BOTTOM = 7;
string msgrecebida;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
Button enviar = FindViewById<Button>(Resource.Id.btenviar);
Button ligar = FindViewById<Button>(Resource.Id.btligar);
TextView text1 = FindViewById<TextView>(Resource.Id.text1);
EditText text2 = FindViewById<EditText>(Resource.Id.text2);
//Conexão com o servidor
ligar.Click += delegate
{
Connect();
ligar.Enabled = false;
text1.Text = "Connected";
};
enviar.Click += delegate
{
if (text2.Text != "")
{
if (text2.Text.ToLower().StartsWith("/"))
{
text2.Text = "";
}
else
{
SendMessage("MSG :" + text2.Text);
text2.Text = "";
}
}
};
}
private void Invoke(OnRecievedMessage onRecievedMessage, string message)
{
throw new NotImplementedException();
}
public bool InvokeRequired { get; set; }
public void RecievedMessage(string message)
{
if (InvokeRequired)
{
this.Invoke(new OnRecievedMessage(RecievedMessage), message);
}
else
{
UpdateTextbox(message);
}
}
private void UpdateTextbox(string text)
{
msgrecebida += "\r\n";
msgrecebida += text;
}
//Interligações Classes MainChat & Connection
public void Disconnected(string reason)
{
form.Disconnected(reason);
}
//TCP Connection
public StreamWriter Outgoing;
private StreamReader Incoming;
private TcpClient Connection;
private Thread Messages;
private IPAddress IP;
//public string host;
//public string nick;
//MainChat m_ParentForm;
bool isConnected;
//Função Conectar
public void Connect()
{
try
{
IP = IPAddress.Parse("10.0.2.2");
Connection = new TcpClient();
Connection.Connect(IP, 1986);
Outgoing = new StreamWriter(Connection.GetStream());
Outgoing.WriteLine("EcoDuty");
Outgoing.Flush();
//m_ParentForm.Vis(true);
Messages = new Thread(new ThreadStart(this.Communication));
Messages.Start();
}
catch (Exception e) { Disconnected(e.Message); }
}
private void Communication()
{
Incoming = new StreamReader(Connection.GetStream());
string check = Incoming.ReadLine();
if (check[0] == '1')
{
//m_ParentForm.Vis(true);
isConnected = true;
}
else
{
string Reason = "Disconnected: ";
Reason += check.Substring(2, check.Length - 2);
Disconnected(Reason);
return;
}
while (isConnected == true)
{
try
{
ServerMessage(Incoming.ReadLine());
}
catch (Exception e)
{
if (isConnected == true)
{
Disconnected("Connection to server lost");
Console.WriteLine("Connection Lost: " + e.ToString());
}
break;
}
}
}
private void ServerMessage(string message)
{
try
{
RecievedMessage(message);
}
catch { }
}
public void CloseConnection()
{
isConnected = false;
Incoming.Close();
Outgoing.Close();
Connection.Close();
Messages.Abort();
}
public void SendMessage(string message)
{
Outgoing.WriteLine(message);
Outgoing.Flush();
}
}
}
It looks to me like (one) problem is that you are trying to access text1 as if it is a field or property when it is a local variable declared in onCreate. You either need to push text1 up a level in scope, or grab a new reference to it in RecievedMessage. This is not a problem with Android, specifically, but is a difference with how the code is set up with Mono for Android.
Typically, when working with Winforms in Windows, you'll be working in a partial class with all your controls defined in the designer.cs file as fields. But in your Mono code, you are binding to TextViews instead, and in your OnCreate instead of at instance scope. When you copy/pasted your code, you likely didn't account for that difference.
As an aside, I highly doubt that the user32.dll function you are importing is going to do anything useful on Android. Unless the Mono folks are doing something magical, the win32 API will not be available on Android.

Categories