首页 > 解决方案 > 当应用程序开始共享时如何调用旧意图?

问题描述

我想解释我的应用程序逻辑。我的应用程序正在查看 PDF。有两种显示 pdf 的方法。

  1. 用户登录应用程序并单击“显示 PDF”按钮。然后,App 会打开一个新窗口,用户选择一个 pdf 文件进行查看。

  2. 用户从他的手机存储中打开一个 pdf 并将该 pdf 共享给我的应用程序。(如果用户之前没有登录过应用,应用会打开登录页面。)

第一个工作正常。但是当用户从他的手机存储中向我的应用程序共享一个 pdf 文件时,应用程序会自动打开我的 HomePape。

应用中有 2 个公共 LoginPage(){}。

1.公共登录页面()

2.公共登录页面(字符串_pdfUri)

第一个允许用户登录应用程序。第二个,如果用户没有登录应用程序并将 pdf 共享给应用程序。通过这种方式,应用程序获取 pdfpath 并打开 PdfShowPage。

让我们检查代码,

应用程序.xaml.cs:

/* 我调试了这个。即使用户共享 Pdf,应用程序也总是会出现在这里。因此,PdfShowPage LoginPage 有效,而 LoginPage(string _pdfUri) 无效。该应用程序打开主页。基本上;

LoginPage() -> 主页

LoginPage(string _pdfUri) -> PdfShowPage

*/

 public App()
        {
            InitializeComponent();            
                MainPage = new NavigationPage(new LoginPage())
                {
                    BarBackgroundColor = Color.FromHex("008000"),
                    BarTextColor = Color.FromHex("E8E8E8")
                };            
        }

protected override void OnStart()
        {
           // Handle when your app starts
        }

        protected override void OnSleep()
        {
            // Handle when your app sleeps
        }

        protected override void OnResume()
        {
            // Handle when your app resumes
        }

MainActivity.cs,

protected override async void OnCreate(Bundle savedInstanceState)
        {

            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;

            base.OnCreate(savedInstanceState);
            ZXing.Net.Mobile.Forms.Android.Platform.Init();
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
            LoadApplication(new App());

            Share();           

        }

private void Share()
{
   //Some codes for share
   Navigate(path);
}

public async void Navigate(string path)
{
            await Xamarin.Forms.Application.Current.MainPage.Navigation.PushModalAsync(new LoginPage(path));

}

登录页面.cs,

public LoginPage(string _pdfUri)
{
 this.PdfUri = _pdfUri;

 CheckSessionWithUri(_pdfUri); //it checks session and send pdf path to PDFshowPage.
}

public LoginPage()
{     

 CheckSession(); //it sends user to the HomePage if the user correctly login or the user has login before.
}

我想控制用户何时将 pdf 共享到我的应用程序,该应用程序必须将他发送到 PdfShowPage。我们应该在 App.xaml.cs 还是其他地方控制它?

谢谢。

标签: c#androidpdfxamarin.forms

解决方案


也许你可以通过_pdfUritoApp类,然后LoginPage_pdfUri. 喜欢:

protected override async void OnCreate(Bundle savedInstanceState)
    {

        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(savedInstanceState);
        ZXing.Net.Mobile.Forms.Android.Platform.Init();
        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);


        Share();           

    }

private void Share()
{
  //Some codes for share
   LoadApplication(new App(_pdfUri));
}

然后

public App(string _pdfUri)
    {
        InitializeComponent();   
        if(path !=null){
           MainPage = new NavigationPage(new LoginPage(_pdfUri))
            {
                BarBackgroundColor = Color.FromHex("008000"),
                BarTextColor = Color.FromHex("E8E8E8")
            };    
        }else{
           MainPage = new NavigationPage(new LoginPage())
            {
                BarBackgroundColor = Color.FromHex("008000"),
                BarTextColor = Color.FromHex("E8E8E8")
            };     
        }

    }

推荐阅读