XAML Xamarin:NameScope中已存在键为“X”的元素

xsuvu9jc  于 2023-03-16  发布在  其他
关注(0)|答案(4)|浏览(119)

当我尝试推送到新页面时,出现以下错误。
(我正在使用Xamarin,表格4.6.0.800)

{System.ArgumentException: An element with the key 'NewAccountLogo' already exists in NameScope
Parameter name: name
  at Xamarin.Forms.Internals.NameScope.Xamarin.Forms.Internals.INameScope.RegisterName (System.String name, System.Object scopedElement) [0x0000e] in D:\a\1\s\Xamarin.Forms.Core\Internals\NameScope.cs:21 
  at amici.NewAccount.InitializeComponent () [0x00078] in C:\Users\X\source\repos\amici\amici\amici\obj\Debug\netstandard2.0\NewAccount.xaml.g.cs:46 
  at amici.NewAccount..ctor () [0x0000f] in C:\Users\X\source\repos\amici\amici\amici\NewAccount.xaml.cs:26 
  at amici.Logon+<OnCreateAccountClick>d__3.MoveNext () [0x0002a] in C:\Users\X\source\repos\amici\amici\amici\logon.xaml.cs:119 }

这个错误看起来很直接,所以我回去重命名了元素,然后在我的项目中搜索了相同的名称。(只有一个)。重新编译并再次尝试,相同的消息,但与新的名称?所以我删除了元素,并尝试重新编译并再次尝试,有趣的是,我得到了同样的信息,但是有了下一个元素?所以现在我我想可能是错误信息不太正确或者试图告诉我其他的东西。我只是不明白。下面是我的xaml。

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             NavigationPage.HasNavigationBar="True"
             Title="New Account"
             x:Class="amici.NewAccount">
    <ContentPage.Content >
        <ScrollView>
            <Grid>
                <StackLayout Margin="20" Padding="10" AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0,0,1,1">
                    <Image x:Name="NewAccountLogo"/>
                    <Entry x:Name="NewEmail" Placeholder="Email"/>
                    <Entry x:Name="EmailConfirm" Placeholder="Confirm Email"/>

                    <Label Text="Password between 6 and 20 characters; must contain at least one lowercase letter, one uppercase letter, one numeric digit." Margin="0,0,0,5"></Label>

                    <Entry x:Name="NewPassword" IsPassword="true" Placeholder="Password"/>
                    <Entry x:Name="PasswordConfirm" IsPassword="true" Placeholder="Confirm Password"/>

                    <Label x:Name="bntCreatAccountButton" TextColor="Blue" Text="Create Account" HorizontalOptions="Center" Margin="0,25,0,0">
                        <Label.GestureRecognizers>
                            <TapGestureRecognizer Tapped="OnCreateAccount" NumberOfTapsRequired="1" />
                        </Label.GestureRecognizers>
                    </Label>
                </StackLayout>
                <ActivityIndicator x:Name="WaitIcon"  IsRunning="{Binding IsBusy}" VerticalOptions="Center" HorizontalOptions="Center" />
            </Grid>
        </ScrollView>
    </ContentPage.Content>
</ContentPage>

代码隐藏:

[XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class NewAccount : ContentPage
    {
        public NewAccount()
        {
            InitializeComponent ();

            this.InitializeComponent();
            this.BindingContext = this;
            this.IsBusy = false;

            var bkgrndGradient = new Gradient()
            {
                Rotation = 150,
                Steps = new GradientStepCollection()
                {
                    new GradientStep(Color.White, 0),
                    new GradientStep(Color.White, .5),
                    new GradientStep(Color.FromHex("#ccd9ff"), 1)
                }
            };

            ContentPageGloss.SetBackgroundGradient(this, bkgrndGradient);
            NewAccountLogo.Source = ImageSource.FromFile("logo.png");
        }


        async private void OnCreateAccount(object sender, EventArgs e)
        {

            string Message = string.Empty;
            string title = string.Empty;
            bool results = false;
            IsBusy = true;

            try
            {

                if (ComparisonBehavior.CompareValues(NewEmail.Text, NewPassword.Text, EmailConfirm.Text,PasswordConfirm.Text, ref Message, ref title))
                {

                    await Task.Delay(1000);
                    results = RestController.CreateAccount(NewEmail.Text, NewPassword.Text);
                    IsBusy = false;

                    if (results)
                    {
                        await DisplayAlert("New Account Created.", "Thank You! Please check your email (junk mail) to activate your subscription.", "OK");
                        await Application.Current.MainPage.Navigation.PopAsync();

                    }else
                    {
                        if (Application.Current.Properties["resp"].ToString() == "Account exist")
                        {
                            await DisplayAlert("Account Exist", "Sorry this email already exist!", "OK");
                        }

                    }
                }else
                {
                    await DisplayAlert(title, Message, "OK");
                }

            }catch (Exception ex)
            {
                 await DisplayAlert("internal Error", ex.Message, "OK");
            } finally{
                IsBusy = false;
           }

        }
    }

任何帮助都将不胜感激。

elcex8rz

elcex8rz1#

在我的代码后面我有**InitializeComponent();**申报两次。

qlvxas9a

qlvxas9a2#

这种类型的问题通常发生在系统尝试初始化相同名称的相同组件时。请检查InitializeComponent();,它可能声明了两次。

7rtdyuoh

7rtdyuoh3#

在我的例子中,我试图将一个组件绑定到它的“代码隐藏类”而不是它的“视图模型”。

<Picker
    BindingContext={x:Reference x:Name=ThisPage} 
/>

问题是x:Name=ThisPage实际上是试图为X命名空间设置一个新名称,而不是引用一个名称,当我将其更改为Name=ThisPage时,它工作得很好。

dvtswwa3

dvtswwa34#

是的,我也得到了同样的异常,当我检查我的错误背后的代码时,有两个InitializeComponent();在代码隐藏中。

相关问题