簡體   English   中英

從 Xamarin 自定義渲染器打開新活動

[英]Open new Activity from Xamarin Custom Renderer

這是我的第一個自定義渲染器項目,剛剛獲得 xamarin。 我有一個相機自定義渲染器,並且想將圖像預覽(通過文件路徑)傳遞給另一個活動以獲得額外的功能,在 Android 中。 使用經典:

            Intent intent = new Intent(this, typeof(ResultPage));
        StartActivity(intent);

它會引發錯誤: -“this”中的第一個,表示“錯誤 CS1503 參數 1:'CustomRenderer.Droid.CameraPageRenderer' 可以轉換為 'Android.Content.Context'” -其次,在“StartActivity”中說“錯誤 CS0103 名稱‘StartActivity’在實際上下文‘CustomRenderer.Android’中不存在”

這是 go 的方法:

        async void TakePhotoButtonTapped(object sender, EventArgs e)
    {
        camera.StopPreview();

        var image = textureView.Bitmap;

        try
        {
            var absolutePath = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDcim).AbsolutePath;
            var folderPath = absolutePath + "/Images";
            var filePath = System.IO.Path.Combine(folderPath, string.Format("image_{0}.jpg", Guid.NewGuid()));

            var fileStream = new FileStream(filePath, FileMode.Create);
            await image.CompressAsync(Bitmap.CompressFormat.Jpeg, 50, fileStream);
            fileStream.Close();

            image.Recycle();

            var intent = new Android.Content.Intent(Android.Content.Intent.ActionMediaScannerScanFile);
            var file = new Java.IO.File(filePath);
            var uri = Android.Net.Uri.FromFile(file);
            intent.SetData(uri);
            MainActivity.Instance.SendBroadcast(intent);

            CameraPageRenderer cameraPageRenderer = this;
            Intent intent = new Intent(this, typeof(ResultPage));
            StartActivity(intent);

        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(@"               ", ex.Message);
        }

CustomRenderer不是 Android Context 您需要檢索當前的 Android Context並使用它:

var context = this.Context;
Intent intent = new Intent(context, typeof(ResultPage));
context.StartActivity(intent);

請記住,您的自定義渲染器需要實現接受 Context 作為其參數的參數化構造函數才能使其工作:

public MyCustomRenderer(Context context) : base(context) { }

暫無
暫無

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

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