【问题标题】:StartActivity from string从字符串开始活动
【发布时间】:2017-05-30 20:25:15
【问题描述】:

我想在 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));

【问题讨论】:

    标签: android string android-activity xamarin


    【解决方案1】:

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

    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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多