How can I start an Activity from a non-Activity in Xamarin.Forms?
In the main startup project, MyApp.Droid, I have MyActivity.cs.
In the PCL project, MyApp.Plugin.Android, I have a non-activity class, MyClass.cs.
In MyClass.cs I need to run some code in MyActivity.cs.
How can I do that?
Thanks.
MyActivity.cs:
public void MyMethod()
{
// [Working Code...]
}
MyClass.cs:
public void OnSuccess
{
// What should I put here to call MyMethod?
}
something like this should do the trick! (I think, it been a while...)
MyClass.cs:
using System;
namespace MyClass {
public static class Program {
public static void MyMethod()
{
// [Working Code...]
}
}
}
MyActivity.cs:
using System;
using MyClass; //get your other file by the namespace
namespace MyActivity {
public static class Program {
public static void OnSuccess()
{
Program.MyMethod(); //[class name]..MyMethod();
}
}
}
Related
This question already has answers here:
When to use static methods
(24 answers)
Closed 2 years ago.
I always knew that I have to use static methods but I wonder why?
As you can see below I have to make "MigrateDatabase" Static
using System;
namespace OdeToFood
{
public class Program
{
public static void Main(string[] args)
{
MigrateDatabase();
}
private static void MigrateDatabase()
{
//.....
}
}
}
Let's just be clear, the only reason MigrateDatabase has to be static in this case is because you're calling it from a static method (Main). If instead MigrateDatabase was an instance method on a class, you could instantiate that class and call it
using System;
namespace OdeToFood
{
public class Program
{
public static void Main(string[] args)
{
var migration = new Migration();
migration.MigrateDatabase();
}
}
public class Migration
{
private void MigrateDatabase()
{
//.....
}
}
}
You could also put it as a instance method on Program if you're instantiating an instance of that class
using System;
namespace OdeToFood
{
public class Program
{
public static void Main(string[] args)
{
var program = new Program();
program.MigrateDatabase();
}
private void MigrateDatabase()
{
//.....
}
}
}
so I want to make it easy to call Console.WriteLine() (from the namespace system) so I wrote:
using System;
namespace main
{
namespace easier
{
public class print
{
public print(string input)
{
Console.WriteLine(input);
}
}
}
}
But when I try to call print("Hello World); it says CS1955: Non-invocable member 'print' cannot be used like a method.
Is there a way so I don't have to do class.print("Hello World"); just print("Hello World");?
Thanks in advance!
You can use using static
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-static
using System;
using static Easier;
class Program
{
static void Main(string[] args)
{
Print("message");
}
}
public static class Easier
{
public static void Print(string value)
{
Console.WriteLine(value);
}
}
So i'm trying to organize my functions into nested classes so i can call them like: "Player.Trigger_Functions.TakeDamage()" rather than calling it as such: "Player.TakeDamage()". I suppose it is a less efficient way to call the functions the way I'm suggesting but it would help separate the functions into distinct categories while remaining on the same file.
Here is some test code but i can't get it to compile online to see if it works.
(some of the functions need to be able to interact with each-other despite being in separate containers which i think is a problem)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class Program
{
public class meme{
public int thicc = 0;
public oof nest1 = new oof();
public watermelone nest2 = new watermelone();
public class oof : meme
{
public void here(){
thicc++;
}
public void call(){
nest2.here();
System.Console.WriteLine("oof" + thicc);
}
}
public class watermelone : meme
{
public void here(){
thicc++;
}
public void call(){
nest1.here();
System.Console.WriteLine("watermelone" + thicc);
}
}
}
public static void Main(){
meme me = new meme();
me.nest1.call();//adding 1
me.nest2.call();//adding 1
System.Console.WriteLine("here is the current thicc value of the me class:" + me.thicc);
}
}
Ok yeah so this code wouldn't work at all, i didn't put that much thought into it but you get the idea of what i'm trying to accomplish.
You can use interfaces to break up the functionality of your class into related groups.
From this:
class Person
{
void sayHello() { }
void sayGoodbye() { }
void walkForward() { }
void walkBackward() { }
}
Refactor into this:
interface ISpeak
{
void sayHello();
void sayGoodbye();
}
interface IWalk
{
void walkForward();
void walkBackward();
}
class Person : ISpeak, IWalk
{
void ISpeak.sayHello() { }
void ISpeak.sayGoodbye() { }
void IWalk.walkForward() { }
void IWalk.walkBackward() { }
}
class Program
{
static void Main(string[] args)
{
Person person = new Person();
IWalk walk = person;
ISpeak speak = person;
speak.sayHello();
walk.walkForward();
}
}
I am trying to assign the property dynamically using post sharp. Such as in the below example I would like to have custom logging messages. But the problem is how could I set xname in the attribute part.In the first log I would like to have the logging message would have the persons name in the beginning but the second logging message would have the persons name at the end.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplicationLogging
{
internal static class Program
{
private static void Main()
{
Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
SayHelloInTurkish("Ayse");
SayGoodByeSayHelloInEnglish("Elizabeth");
}
[Trace("xname Loglanıyor Gunaydın")]
private static void SayHelloInTurkish(string personName)
{
Console.WriteLine("Hello, world.");
}
[Trace("Good Bye Logging for xname")]
private static void SayGoodByeSayHelloInEnglish(string personName)
{
Console.WriteLine("Good bye, world.");
}
}
}
using System;
using System.Diagnostics;
using PostSharp.Aspects;
namespace ConsoleApplicationLogging
{
[Serializable]
public sealed class TraceAttribute : OnMethodBoundaryAspect
{
private readonly string category;
public TraceAttribute(string category)
{
this.category = category;
}
public string Category { get { return category; } }
public override void OnEntry(MethodExecutionArgs args)
{
if (args.Method.DeclaringType != null)
Trace.WriteLine(string.Format("Entering {0}.{1}.",
args.Method.DeclaringType.Name, args.Method.Name), this.category);
}
public override void OnExit(MethodExecutionArgs args)
{
if (args.Method.DeclaringType != null)
Trace.WriteLine(string.Format("Leaving {0}.{1}.",
args.Method.DeclaringType.Name, args.Method.Name), this.category);
}
}
}
If I understand correctly, you need something like this (no sanity checking included):
this.category.Replace("xname", (string)args.Arguments[0])
If I did not understand correctly, feel free to comment below.
When the only code in there is for a class how would you code it adding public to the default class like so
namespace HW2_2_Spaceship
{
public class Spaceship //added public to the default class
{
static void Main(string[] args)
{
}
or
namespace HW2_1_Book
{
class Book
{
static void Main(string[] args)
{
}
public class Book // added a new class with in the default class
{
In general, each class should have their own file.
Main should be in Program.cs
There are usecases where you can use Inner classes, see Using Inner classes in C#.
//Program.cs, if u use visual studio then ensure you add
// the public access modifier yourself
namespace HW2_2_Spaceship
{
public class Program
{
public static void Main(string[] args)
{
//Do something here
}
}
}
//Book.cs, add the public modifier to the class
namespace HW2_2_Spaceship
{
public class Book
{
//add method and properties here
}
}