How to disable the back button in android C# - c#

Im making an Android App using Xamarin.Android in Visual Studio and I've been trying to disable the back button in my secondary activity, and the methods I've been using is adding this code into the activity
#Override
public void onBackPressed() {
}
So the code would look like this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Java.Lang;
namespace The_Coder_Quiz
{
[Activity(Label = "Activity2")]
public class Activity2 : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.easyQuiz);
// Create your application here
}
#Override
public void onBackPressed()
{
//Include the code here
return;
}
}
}
But that gave me an error which was:
member modifier 'public' must precede the member type and name
Im assuming that is because im doing the project in C#, what would be the correct solution since im doing this in C# and not Java (new to android development)
Im trying to keep off StackOverflow when learning but its kinda hard to find references online for Android Development in C#

The correct way to do an override in C# is:
public override void OnBackPressed()
{
//Include the code here
return;
}

For example if it is a login page (Activity):
public override void OnBackPressed(){
//by this way.. user want be able to hit the back button
// unless user is logged in
if (user.IsLoggedIn()){
base.OnBackPressed();
}
}
Check this:
OnBackPressed in Xamarin Android
And about android development using Xamarin, these are some tutorials:
Xamarin.Android Get Started
JoeRock on Youtube

Related

Xamarin: 'CustomBottomNavAppearance' does not implement interface member 'IShellBottomNavViewAppearanceTracker.SetAppearance[...]' error

I've recently tried to customize the tabbar using a custom ShellRenderer in my app using Xamarin and Visual Studio 2019. I've found this question where someone does exactly what I need and the question has an answer which explains it in great detail.
Now, my problem is that when trying to build the app, I get the following error:
'CustomBottomNavAppearance' does not implement interface member 'IShellBottomNavViewAppearanceTracker.SetAppearance(BottomNavigationView,IShellAppearanceElement)' (Errorcode CS0535, line 32)
Now, the sample project that the user in the other question provides works perfectly on my machine, yet implementing it in my project doesn't work, although they are practiacally identical.
I expect this to be very obvious for an advanced user, so sorry about that, I'm still learning and would like to just implement this feature right now, even if my skills might not allow me to do that.
Thanks in advance for any replies!
This is my 'MyShellRenderer.cs' class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Google.Android.Material.BottomNavigation;
using MyProject;
using MyProject.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly: ExportRenderer(typeof(AppShell), typeof(MyShellRenderer))]
namespace MyProject.Droid
{
public class MyShellRenderer : ShellRenderer
{
public MyShellRenderer(Context context) : base(context)
{
}
protected override IShellBottomNavViewAppearanceTracker CreateBottomNavViewAppearanceTracker(ShellItem shellItem)
{
return new CustomBottomNavAppearance();
}
}
public class CustomBottomNavAppearance : IShellBottomNavViewAppearanceTracker
{
public void Dispose()
{
}
public void ResetAppearance(BottomNavigationView bottomView)
{
}
public void SetAppearance(BottomNavigationView bottomView, ShellAppearance appearance)
{
IMenu myMenu = bottomView.Menu;
IMenuItem myItemOne = myMenu.GetItem(0);
if (myItemOne.IsChecked)
{
myItemOne.SetIcon(Resource.Drawable.icon_about);
}
else
{
myItemOne.SetIcon(Resource.Drawable.icon_feed);
}
}
}
}
Edit: I've just found out that the issue actually only exists with the Android version (well, the iOS version at least doesn't throw the error).
Update: I have been able to fix the issue by using "using Google.Android.Material.BottomNavigation;" "using Android.Support.Design.Widget;". My guess it that the old one is now deprecated and only works with the 4.x.x releases, not my 5.x.x release. Thanks everyone!
Per this docs,The error does not implement interface member Errorcode CS0535 means A class must implement all members of interfaces from which it derives or else be declared abstract.So the issue must be that the method SetAppearance has not been correctly implemented.When I Remove or Comment the SetAppearance method, the error appears.
The solution is that using using Google.Android.Material.BottomNavigation instead of using Android.Support.Design.Widget.

How can I match XNA with Windows Form in VS 2019?

I'm trying to create a music player with an audio visualizer starting from a windows game project on VS 2019. To do so I created a custom display, draggable from the toolbox, in order to put it inside a form.
From tutorials I've seen it is necessary to include three classes from GraphicsDevice and I've done it, but when I put them inside the code, the Display class does not inherit and VS does not recognize the keywords.
To be precise WinFormsGraphicsDevice is marked as an error, and its methods as well.
Does anybody have any hints about this?
using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WindowsGame2.CustomControls
{
public class Display : WinFormsGraphicsDevice.GraphicsDeviceControl
{
protected override void Initialize()
{
}
protected override void Draw()
{
GraphicsDevice.Clear();
}
}
}

Can't get Process.Start to work in C#

I've been trying to run a command from an app written in C#, and all I've read had to do with using System.Diagnostics.Process.Start to run it. However, whenever I try to use this function, the compiler throws an error telling me that 'The type or namespace name 'Process' does not exist in the namespace System.Diagnostics'.
When I try to write the command, intellisense doesn't show anything about "Process" inside the System.Diagnostics namespace.
This doesn't work:
using Windows.ApplicationModel.Activation;
using Windows.UI.Xaml;
using System.Diagnostics;
namespace App2
{
sealed partial class App : Application
{
public App()
{
}
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
System.Diagnostics.Process.Start("CMD.exe", "help"); // This is giving me an error
}
}
}
EDIT: I just tried creating a new Windows Forms project, and I could find System.Diagnostics.Process.Start. Does anyone know how can I run a command from an Universal Windows App, since it's apparently not supported?

C# "await" error when using WinRT from Desktop app

I trying to get my GPS position from a Desktop app, using Windows Runtime, in C#.
I followed this how-to to set up Visual Studio and this code sample to create the following trivial code as a C# console app :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Runtime;
using System.Windows;
using Windows;
using Windows.Devices.Geolocation;
using Windows.Foundation;
using Windows.Foundation.Collections;
namespace GeoLocCS
{
public sealed partial class Program
{
static void Main(string[] args)
{
Test test = new Test();
Console.Read();
}
}
public class Test
{
private Geolocator geolocator;
public Test()
{
Console.WriteLine("test");
geolocator = new Geolocator();
this.getPos();
}
async void getPos()
{
Geoposition pos = await geolocator.GetGeopositionAsync();
Console.WriteLine(pos.Coordinate.Latitude.ToString());
}
}
}
When trying to compile, I get the error :
Error 1 'await' requires that the type 'Windows.Foundation.IAsyncOperation<Windows.Devices.Geolocation.Geoposition>' have a suitable GetAwaiter method. Are you missing a using directive for 'System'?
I tried to reference every DLL that I could, like "System.runtime", "System.Runtime.WindowsRuntime", "Windows.Foundation"...
What I am missing ?
Thanks for the answer,
Serge
I finally figured out my problem :
I am on 8.1 so I changed this in the .csproj file (targetingPlatform) instead of targeting 8.0
After that modification, I could reference "Windows"
I manually referenced the two following DLLs :
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5\Facades\System.Runtime.dll
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.Runtime.WindowsRuntime.dll
If you are On Win8.1 Please Use:
<TargetPlatformVersion>8.1</TargetPlatformVersion>
and not 8.0.
Adding to the existing answers:
I suddenly got this problem with a multiproject solution in WPF (still don't know why). I tried switching between TargetPlatformVersions 8.1 and 10.0, tried using the v4.5- and v4.5.1-System.Runtime.WindowsRuntime.dll, always alternating between BadImageFormatException and FileNotFoundException.
Turned out to be references in some of the app.config-files (not the .csproj-files!). Visual Studio 2017 must have added them at some point and as soon as I removed these entries, the above solutions finally worked.

Run Selenium C# Test from a Window Form

I am trying to launch my Selenium Launch when a button is press on my windows form. I am unsure on how to link the class library with the Windows Form. I am getting stuck because for the script/class to run correctly the project Output Type must be set to "Class Library" otherwise it gives out loads of errors.
Here is the class I am trying to launch :
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace SeleniumTest
{
[TestClass]
public class SeleniumTest
{
[TestMethod]
public void TestMethod1()
{
// Set what browser to use
ChromeDriver driver = new ChromeDriver(#"C:\Users\Alex\Documents\Selenium");
// Set the base website
string baseURL = "http://kd.svr-webdev-01.df.local";
driver.Navigate().GoToUrl(baseURL + "/");
driver.Close();
}
}
}
All you need to do is put your unit test (TestMethod1) body into a button click event. Easiest way to make that event is just to drop a button onto the form using the designer and double click it.
Visual Studio will create a blank event for you, and you just need to copy/paste your current code into there.
Even, to make it simpler, create a console application and stick that body into the the "Main" method.

Categories