如何从Xamarin.Forms调用特定于平台的页面

10

我是新手,正在学习xamarin应用程序开发,我有一些问题:

  1. 是否可以从Xamarin.Forms页面调用特定于平台的页面?
  2. 能否从视图模型进行页面导航?

让我解释得更清楚一些。

我在PCL(Xamarin.Forms)项目中有一个Page XFView.xamlXFView.xaml.csXFViewModel.cs。从 XFView.xaml.csXFViewModel.cs 中,我想要调用位于 Xamarin.Andriod 项目中的 MAActivity.cs 页面。

我尝试了很多次,但没有任何想法。

1个回答

11

每当您想从本地调用某个东西(在您的情况下为 MAActivity.cs),您必须使用 DependencyService。

例如:

  1. 设置依赖服务

  2. // PCL Project
    public interface INativePages
    {
        void StartMA();
    }
    
    // MonoDroid Project
    [assembly: Dependency(typeof(NativePages))]
    
    public class NativePages : INativePages
    {
        public void StartMA()
        {
            var intent = new Intent(Forms.Context, typeof(MAActivity));
            Forms.Context.StartActivity(intent);
        }
    }
    
  3. 在PCL中调用

  4. // PCL Project
    DependencyService.Get<INativePages>().StartMA();
    

这对于Android很有效 - 但我正在努力寻找WP8的等效物。似乎有一些涉及自定义渲染的解决方案,但我更愿意避免那个... - Joseph Redfern
1
明白了! (Application.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri("/PageName.xaml", UriKind.Relative)); - Joseph Redfern

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接