名称'核心'在当前上下文中不存在

时间:2018-06-12 15:21:33

标签: c# .net xamarin xamarin.forms

我是Xamarin的新手,我正在关注Xamarin大学的教程。一切正常,直到我开始遇到错误core does not exist。我查看了一个已发布的旧问题,我按照说明操作,但这根本没有帮助我。我已经发布了以下旧问题的链接。

The type or namespace name 'Core' does not exist in the namespace

MainPage.xml.cs的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace MyTelephone
{   
public partial class MainPage : ContentPage
{
    Entry phoneNumberText;
    Button translateButton;
    Button callButton;
    string translatedNumber;




    public MainPage()
    {

        this.Padding = new Thickness(20, 20, 20, 20);

        StackLayout panel = new StackLayout
        {
            Spacing = 15
        };

        panel.Children.Add(new Label
        {
            Text = "Enter a password:",
            FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label))
        });

        panel.Children.Add(phoneNumberText = new Entry
        {
            Text = "1-855-XAMARIN",
        });

        panel.Children.Add(translateButton = new Button
        {
            Text = "Translate"
        });

        panel.Children.Add(callButton = new Button
        {
            Text = "Call",
            IsEnabled = false,
        });
        translateButton.Clicked += OnTranslate;

        this.Content = panel;

        //InitializeComponent();

    }


    private void OnTranslate(object sender, EventArgs e)
    {
        translatedNumber = 
    Core.PhonewordTranslator.ToNumber(phoneNumberText.Text);
        if (!string.IsNullOrEmpty(translatedNumber))
        {
            callButton.IsEnabled = true;
            callButton.Text = "Call" + translatedNumber;
        }
        else
        {
            callButton.IsEnabled = false;
            callButton.Text = "Call";
        }
    }


    }
}

PhonewordTranslator.cs的代码

using System.Text;
namespace MyTelephone
{
    public static class PhonewordTranslator
   {
    public static string ToNumber(string raw)
    {
        if (string.IsNullOrWhiteSpace(raw))
            return null;

        raw = raw.ToUpperInvariant();

        var newNumber = new StringBuilder();
        foreach (var c in raw)
        {
            if ("-0123456789".Contains(c))
                newNumber.Append(c);
            else
            {
                var result = TranslateToNumber(c);
                if (result != null)
                    newNumber.Append(result);
                else
                    return null;
            }
        }
        return newNumber.ToString();
    }
    static bool Contains(this string keyString, char c)
    {
        return keyString.IndexOf(c) >= 0;
    }

    static readonly string[] digits = {
        "ABC","DEF","GHI","JKL","MNO","PQRS","TUV","WXYZ"
    };

    static int? TranslateToNumber(char c)
    {
        for (int i = 0; i < digits.Length; i++)
        {
            if (digits[i].Contains(c))
                return 2 + i;
             }
            return null;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

这个问题与xamarin有关,而且与你所关联的问题无关。

这是错误的原因:

translatedNumber = 
Core.PhonewordTranslator.ToNumber(phoneNumberText.Text);

检查你的文件PhoneTranslator.cs吧。您需要重命名命名空间。

来源:

https://forums.xamarin.com/discussion/13839/core-phonewordtranslator-tonumber-compilation-error

  

对于那些需要更多信息的人来说,这个问题可能是因为没有将PhoneTranslator.cs文件中的命名空间重命名为“Core”。