首页 > 解决方案 > CS 0102: 类型“ ”已包含“ ”的定义

问题描述

我的标题错误 CS 0302 指出“'EditMemberPage' 类型已经包含 'takePhoto' 的定义,我的代码与第 30 行关联。我正在尝试使用媒体插件通过 iPhone 模拟器拍照。这是代码:

using Relate.Model;
using Plugin.Media;
using Plugin.Media.Abstractions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;


namespace Relate.Views
{
    public partial class EditMemberPage : ContentPage
    {

        public EditMemberPage()
        {
            InitializeComponent();

            takePhoto.Clicked += async (sender, args) =>
            {

                if (!CrossMedia.Current.IsCameraAvailable || 
        !CrossMedia.Current.IsTakePhotoSupported)
                {
                    DisplayAlert("No Camera", ":( No camera avaialble.", "OK");
                    return;
                }

                var file = await CrossMedia.Current.TakePhotoAsync(new 
        Plugin.Media.Abstractions.StoreCameraMediaOptions
                {
                    Directory = "Test",
                    SaveToAlbum = true,
                    CompressionQuality = 75,
                    CustomPhotoSize = 50,
                    PhotoSize = PhotoSize.MaxWidthHeight,
                    MaxWidthHeight = 2000,
                    DefaultCamera = CameraDevice.Front
                    });

                if (file == null)
                    return;

                DisplayAlert("File Location", file.Path, "OK");

                image.Source = ImageSource.FromStream(() =>
                  {
                  var stream = file.GetStream();
                  file.Dispose();
                  return stream;
              });
            };
        }
    }

}

标签: c#xamarinxamarin.forms

解决方案


除非 EditMemberPage.cs 的内容比您向我们展示的更多,否则请查看 EditMemberPage.xaml。您可能有两个带有 x:Name="takePhoto" 的元素。

具有 x:Name 的元素在该类中创建一个成员,其名称是 x:Name 的值。因此,您不能有多个具有相同 x:Name 的元素,也不能在 C# 中声明一个与元素的 x:Name 具有相同名称的成员。


推荐阅读