簡體   English   中英

錯誤:可訪問性不一致:與方法“相比,返回類型的訪問性較差

[英]Error : Inconsistent accessibility: return type is less accessible than method '

我得到這個錯誤

錯誤6不一致的可訪問性:返回類型'HomePage.Models.SaveResult'比方法'HomePage.Models.CarDataStore.SaveFillup(HomePage.Models.Fillup,System.Action)'的訪問性更差

這是課程:

 private const string CAR_PHOTO_DIR_NAME = "FuelTracker";
    private const string CAR_PHOTO_FILE_NAME = "CarPhoto.jpg";
    private const string CAR_PHOTO_TEMP_FILE_NAME = "TempCarPhoto.jpg";
    private const string CAR_KEY = "FuelTracker.Car";
    private static readonly IsolatedStorageSettings appSettings =
        IsolatedStorageSettings.ApplicationSettings;
    private static Car _car;

    public static event EventHandler CarUpdated;

    /// <summary>
    /// Gets or sets the car data, loading the data from isolated storage
    /// (if there is any saved data) on the first access. 
    /// </summary>
    public static Car Car 
    { 
        get
        {
            if (_car == null)
            {
                if (appSettings.Contains(CAR_KEY))
                {
                    _car = (Car)appSettings[CAR_KEY];
                    _car.Picture = GetCarPhoto(CAR_PHOTO_FILE_NAME);
                }
                else
                {
                    _car = new Car
                    {
                        FillupHistory = new ObservableCollection<Fillup>()
                    };
                }
            }
            return _car;
        }
        set
        {
            _car = value;
            NotifyCarUpdated();
        }
    }

    /// <summary>
    /// Saves the car data to isolated storage. 
    /// </summary>
    /// <param name="errorCallback">The action to execute if the 
    /// storage attempt fails.</param>
    public static void SaveCar(Action errorCallback)
    {
        try
        {
            appSettings[CAR_KEY] = Car;
            appSettings.Save();
            DeleteTempCarPhoto();
            SaveCarPhoto(CAR_PHOTO_FILE_NAME, Car.Picture, errorCallback);
            NotifyCarUpdated();
        }
        catch (IsolatedStorageException)
        {
            errorCallback();
        }
    }

    /// <summary>
    /// Deletes the car data from isolated storage and resets the Car property.
    /// </summary>
    public static void DeleteCar()
    {
        appSettings.Remove(CAR_KEY);
        appSettings.Save();
        Car = null;
        DeleteCarPhoto();
        DeleteTempCarPhoto();
        NotifyCarUpdated();
    }

    /// <summary>
    /// Gets the temporary car photo from isolated storage.
    /// </summary>
    /// <returns>The temporary car photo.</returns>
    public static BitmapImage GetTempCarPhoto()
    {
        return GetCarPhoto(CAR_PHOTO_TEMP_FILE_NAME);
    }

    /// <summary>
    /// Saves the temporary car photo to isolated storage.
    /// </summary>
    /// <param name="carPicture">The image to save.</param>
    /// <param name="errorCallback">The action to execute if the storage
    /// attempt fails.</param>
    public static void SaveTempCarPhoto(BitmapImage carPicture, 
        Action errorCallback)
    {
        SaveCarPhoto(CAR_PHOTO_TEMP_FILE_NAME, carPicture, errorCallback);
    }

    /// <summary>
    /// Deletes the car photo from isolated storage.
    /// </summary>
    private static void DeleteCarPhoto()
    {
        DeletePhoto(CAR_PHOTO_FILE_NAME);
    }

    /// <summary>
    /// Deletes the temporary car photo from isolated storage.
    /// </summary>
    public static void DeleteTempCarPhoto()
    {
        DeletePhoto(CAR_PHOTO_TEMP_FILE_NAME);
    }

    /// <summary>
    /// Deletes the photo with the specified file name.
    /// </summary>
    /// <param name="fileName">The name of the photo file to delete.</param>
    private static void DeletePhoto(String fileName)
    {
        using (var store = IsolatedStorageFile.GetUserStoreForApplication())
        {
            var path = Path.Combine(CAR_PHOTO_DIR_NAME, fileName);
            if (store.FileExists(path)) store.DeleteFile(path);
        }
    }

    /// <summary>
    /// Gets the specified car photo from isolated storage.
    /// </summary>
    /// <param name="fileName">The filename of the photo to get.</param>
    /// <returns>The requested photo.</returns>
    private static BitmapImage GetCarPhoto(string fileName)
    {
        using (var store = IsolatedStorageFile.GetUserStoreForApplication())
        {
            string path = Path.Combine(CAR_PHOTO_DIR_NAME, fileName);

            if (!store.FileExists(path)) return null;

            using (var stream = store.OpenFile(path, FileMode.Open))
            {
                var image = new BitmapImage();
                image.SetSource(stream);
                return image;
            }
        }
    }

    /// <summary>
    /// Saves the specified car photo to isolated storage using the 
    /// specified filename.
    /// </summary>
    /// <param name="fileName">The filename to use.</param>
    /// <param name="carPicture">The image to save.</param>
    /// <param name="errorCallback">The action to execute if the storage
    /// attempt fails.</param>
    private static void SaveCarPhoto(string fileName, BitmapImage carPicture,
        Action errorCallback)
    {
        if (carPicture == null) return;
        try
        {
            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                var bitmap = new WriteableBitmap(carPicture);
                var path = Path.Combine(CAR_PHOTO_DIR_NAME, fileName);

                if (!store.DirectoryExists(CAR_PHOTO_DIR_NAME))
                {
                    store.CreateDirectory(CAR_PHOTO_DIR_NAME);
                }

                using (var stream = store.OpenFile(path, FileMode.Create))
                {
                    bitmap.SaveJpeg(stream, bitmap.PixelWidth, bitmap.PixelHeight, 0, 100);
                }
            }
        }
        catch (IsolatedStorageException)
        {
            errorCallback();
        }
    }

    /// <summary>
    /// Validates the specified Fillup and then, if it is valid, adds it to
    /// Car.FillupHistory collection. 
    /// </summary>
    /// <param name="fillup">The fill-up to save.</param>
    /// <param name="errorCallback">The action to execute if the storage
    /// attempt fails.</param>
    /// <returns>The validation results.</returns>
    public static SaveResult SaveFillup(Fillup fillup, Action errorCallback)
    {
        var lastReading =
            Car.FillupHistory.Count > 0 ?
            Car.FillupHistory.First().OdometerReading :
            Car.InitialOdometerReading;
        fillup.DistanceDriven = fillup.OdometerReading - lastReading;

        var saveResult = new SaveResult();
        var validationResults = fillup.Validate();
        if (validationResults.Count > 0)
        {
            saveResult.SaveSuccessful = false;
            saveResult.ErrorMessages = validationResults;
        }
        else
        {
            Car.FillupHistory.Insert(0, fillup);
            saveResult.SaveSuccessful = true;
            SaveCar(delegate { 
                saveResult.SaveSuccessful = false; 
                errorCallback(); });
        }
        return saveResult;
    }

    private static void NotifyCarUpdated()
    {
        var handler = CarUpdated;
        if (handler != null) handler(null, null);
    }
}

SaveResult類無論在何處聲明,要么因為方法是公共的,要么需要將其訪問修飾符更改為public,或者需要將方法的access修飾符更改為該類定義的任何訪問修飾符(或更改為較小的作用域)

您的類SaveResult可能是內部的(未定義時,類的默認訪問器)。 公開解決該問題。

暫無
暫無

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

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