簡體   English   中英

在 OnCreate (Android) 或 AppDelagate (iOS) [Xamarin.Forms] 中使用消息中心時出現問題

[英]Problem with Messaging Center when using it in OnCreate (Android) or AppDelagate (iOS) [Xamarin.Forms]

我正在使用消息中心在整個應用程序中發送和接收消息/數據。 該應用程序處理自定義文件擴展名並將它們加載到應用程序中並處理這些文件。

將文件導入應用程序有兩種方法:

1-按下應用程序中的瀏覽按鈕並選擇文件(這工作正常)。

2-直接按文件,因為文件擴展名與應用程序相關聯,應用程序打開並導入所選文件。 這就是問題開始的地方。

如果應用程序之前沒有打開並且不在后台(這意味着還沒有訂閱者),則應用程序啟動並且不執行任何操作,如預期的那樣。 應用程序將所選文件發送給訂閱者,但由於表單未完成打開(在MainActivity或 AppDelagate 之后執行的頁面和視圖模型),還沒有可用的訂閱者。

如果應用程序在后台,單擊文件會打開應用程序並按預期工作。

我知道這個問題,但我不知道如何解決它。 有什么建議么?

這是 Android 中的MainActivity.cs

 protected override async void OnNewIntent(Intent intent)
        {
            if (intent?.Data == null)
                return;

            // Open a stream from the URI 
            var lasStream = ContentResolver.OpenInputStream(intent.Data);

            // Save it over 
            var memStream = new MemoryStream();
            await lasStream.CopyToAsync(memStream);
            var byteArray = memStream.ToArray();

            MessagingCenter.Send(byteArray, "ByteArrayReceived");
        }

此方法在應用程序的其中一個 ViewModel 中調用:

private async Task ByteArrayReceived(byte[] byteArray)
    {
        // Handle incoming file as a byte array
    }

更新:

我認為問題不是那么清楚。 我將嘗試解釋更多。

我想要做的是,當用戶選擇一個外部文件(這可能是一個 txt 文件或其他文件)時,這個應用程序應該啟動並將所選文件的內容加載到應用程序中。

在打開應用程序時首先執行的MainActivity.cs中檢測到傳入文件,這意味着還沒有任何訂閱者可用,因為應用程序尚未完成加載並且尚未調用訂閱者方法。 訂閱者方法在MainPageView.cs

我知道應該在發送消息之前調用訂閱者方法,但是我該如何在MainActivity.csAppDelagate.cs中調用呢?

更新 2:

MainPageView 在 MainActivity.cs 執行后被調用。

這是MainActivity.cs

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.Window.RequestFeature(WindowFeatures.ActionBar);
        base.SetTheme(Resource.Style.MainTheme);

        if (Intent.Action == Intent.ActionSend || Intent.Action == Intent.ActionView)
            HandleIntent(Intent);

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

        base.OnCreate(savedInstanceState);

        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);

        LoadApplication(new App());
    }

這是App.cs

   public App()
    {
        InitializeComponent();

        MainPage = new NavigationPage(new MainPageView());
    }

我發現的一種解決方案是您可以在X秒后使用Device.StartTimer發送消息:

protected override async void OnNewIntent(Intent intent)
{
    if (intent?.Data == null)
        return;

    // Open a stream from the URI 
    var lasStream = ContentResolver.OpenInputStream(intent.Data);

    // Save it over 
    var memStream = new MemoryStream();
    await lasStream.CopyToAsync(memStream);
    var byteArray = memStream.ToArray();

    // run task after 2 second
    Device.StartTimer(TimeSpan.FromSeconds(2), () =>
    {
        MessagingCenter.Send(byteArray, "ByteArrayReceived");

        // No longer need to recur. Stops firing task
        return false;
    });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM