来自字符串的StartActivity

时间:2017-05-30 20:25:16

标签: android string android-activity xamarin

我想在Xamarin Android Visual Studio中使用Kalkulator中的Aktywnosc字符串启动Activity

public class Kalkulator
    {
        public int Id { get; set; }
        public string Nazwa { get; set; }
        public string Aktywnosc { get; set; }
        public string Krotki_opis { get; set; }
        public string Zrodlo { get; set; }
        public string Ineksowanie { get; set; }
    }

活动位于Resource.biblioteka文件夹中。 我尝试着: Intent and start activity from string

和这个

How can I start a new android activity using a string?

var e_nacisniety = Kalkulator.Lista_kalkulatorow().First(p=>p.Id == e.Position);
            var act = "Resources.biblioteka." + e_nacisniety.Aktywnosc;
            StartActivity(new Intent().SetClassName(this, act));

1 个答案:

答案 0 :(得分:1)

假设这是您尝试启动的活动:

namespace SushiHangover
{
    [Activity(Name = "Resources.biblioteka.SomeActivity", Label = "SomeActivity")]
    public class SomeActivity : Activity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
        }
    }
}

使用.Net命名空间/类名:

var intent = new Intent(this, Type.GetType("SushiHangover.SomeActivity"));
StartActivity(intent);

使用Java包/类名:

var intent = new Intent(this, Java.Lang.Class.ForName("Resources.biblioteka.SomeActivity"));
StartActivity(intent);