xamarin - Xamarin.forms 如何将字符串转换为页面
问题描述
这是我在 ViewModel 中的代码
async Task ExecuteMenu(object obj)
{
Page page = new Page();
System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();
Type t_form = asm.GetType(asm.GetName().FullName + "." + obj.ToString());
page = Activator.CreateInstance(t_form) as Page;
try
{
await Application.Current.MainPage.Navigation.PushAsync(new page());
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
finally
{
}
}
我有一个从命令加载的菜单元素。在 obj 中,我传递包含所选内容页名称的字符串。我不知道如何将字符串名称转换为页面对象名称。此代码不起作用。我正在尝试动态调用页面。
解决方案
尝试这个:
var pageType= Type.GetType($"NamespaceOfYourView.{obj}");
var page = Activator.CreateInstance(pageType) as Page;
await Application.Current.MainPage.Navigation.PushAsync(page );
推荐阅读
- c# - C# 使用 Regex.Split 拆分大字符串。必须保留分隔符
- python-2.7 - 尝试使用“python setup.py install”安装 Pyramid 包时搜索包时出错
- android - 有没有办法以编程方式随机设置芯片背景颜色?
- arrays - 如何在 PowerShell 的字符或字符串数据上使用 ConvertTo-Json @()?
- spring - Spring MVC 在遍历应用程序时继续用户的表单(Dto)直到显式取消 - 控制器 @SessionScope 组件
- java - Java Swing - 实时更新 Jtable 时出现问题
- c - 为什么并非所有 C 程序都使用调用约定
- revit-api - 我无法使 Revit 中这些对象的 ElementID 与 Revit 文件中的元素匹配的原因是什么?
- python - 如何使用 API 3.9 中的 Tweepy 检索一周前的推文
- python - 在每一行上创建 df 列连接值