如何在 Xamarin 中设置新保存文件的名称?

How to set name of new saved file in Xamarin?

要点是我想为要保存的图片命名,但我不知道如何命名,然后我需要获取该保存图片的路径。你有什么想法如何做到这一点? 我现在有这个:

using System.IO.Path;

namespace Spotter.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class ImportPage : ContentPage
    {
        public ImportPage()
        {
            InitializeComponent();
            NavigationPage.SetHasNavigationBar(this, false);
            
        }
        
        string _dbPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "myDB.db3");
        
        private async void buttonImport_Clicked(object sender, EventArgs e)
        {
            var db = new SQLiteConnection(_dbPath);
            db.CreateTable<Airplane>();
            try
            {
                if (liveryEntry == null)
                    liveryEntry.Text = "N/A";
                if (registrationEntry == null)
                    registrationEntry.Text = "N/A";
                if (airportEntry == null)
                    airportEntry.Text = "N/A";
                if (dateEntry == null)
                    dateEntry.Text = "N/A";                
                if (planeEntry.Text != null && airlineEntry.Text != null && liveryEntry.Text != null && registrationEntry.Text != null && dateEntry.Text != null)
                {
                    var url = PhotoPick();
                    var maxPK = db.Table<Airplane>().OrderByDescending(c => c.Id).FirstOrDefault();
                    Airplane airplane = new Airplane()
                    {
                        Id = (maxPK == null ? 1 : maxPK.Id + 1),
                        Plane = planeEntry.Text,
                        Airline = airlineEntry.Text,
                        Livery = liveryEntry.Text,
                        Registration = registrationEntry.Text,
                        Airport = airportEntry.Text,
                        Date = dateEntry.Text,
                        Comment = commentEntry.Text,
                        Url = await url
                    };
                    CreateThumbnail(await url);
                    db.Insert(airplane);                    
                    await DisplayAlert("Saved", planeEntry.Text + " of " + airlineEntry.Text + " was saved", "OK");
                    planeEntry.Text = "";
                    airlineEntry.Text = "";
                    liveryEntry.Text = "";
                    registrationEntry.Text = "";
                    airportEntry.Text = "";
                    dateEntry.Text = "";
                    commentEntry.Text = "";
                }
                else
                    await DisplayAlert("Fill all needed fields", "You have to fill all fields except livery and comment", "OK");
            }
            catch
            {
                await DisplayAlert("Error", "Select picture again", "OK");
            }
        }

        public async Task<string> PhotoPick()
        {
            var result = await MediaPicker.PickPhotoAsync(new MediaPickerOptions { Title = "Please pick photo!"});
            await result.OpenReadAsync();
            string url = result.FullPath;
            return url;
        }

        public async void CreateThumbnail(string Path)
        {
            
            var bitmap = SKBitmap.Decode(Path);
            int h = bitmap.Height;
            int w = bitmap.Width;
            if (h > 1920 || w > 1920)
            {
                int rectHeight = 1920;
                int rectWidth = 1920;

                //aspect ratio calculation   
                float aspect = w / h;

                //new dimensions by aspect ratio
                int newWidth = (int)(rectWidth * aspect);
                int newHeight = (int)(newWidth / aspect);

                //if one of the two dimensions exceed the box dimensions
                if (newWidth > rectWidth || newHeight > rectHeight)
                {
                    //depending on which of the two exceeds the box dimensions set it as the box dimension and calculate the other one based on the aspect ratio
                    if (newWidth > newHeight)
                    {
                        newWidth = rectWidth;
                        newHeight = (int)(newWidth / aspect);
                    }
                    else
                    {
                        newHeight = rectHeight;
                        newWidth = (int)(newHeight * aspect);
                    }
                }
                


                var resizedImage = bitmap.Resize(new SKImageInfo(newWidth, newHeight), SKBitmapResizeMethod.Lanczos3);
                var image = resizedImage.Encode(SKEncodedImageFormat.Jpeg, 80);


                var path = System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

                var filepath = Path.Combine(path, "myfilename.ext");

                using (var stream = File.OpenWrite(filepath))
                {
                    image.SaveTo(stream);
                }

                await DisplayAlert(null, "Height is: " + newHeight + " pixels and width is: " + newWidth + " pixels", "OK");
            }
            else
                await DisplayAlert(null, "Height is: " + h + " pixels and width is: " + w + " pixels", "OK");

        }
    }
}

将文件名附加到路径

var path = System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

var filepath = Path.Combine(path, "myfilename.ext");

using (var stream = File.OpenWrite(filepath))
        {
            image.SaveTo(stream);
        }