C# 我怎么能得到一个“a”;计数不能小于零;代码中没有计数的异常?

C# 我怎么能得到一个“a”;计数不能小于零;代码中没有计数的异常?,c#,xamarin,xamarin.forms,C#,Xamarin,Xamarin.forms,我有以下代码: public void ABtn() { if (ABtnState == "E") { phrase.Points += -(int)Settings.ABtn; ABtnState = "D"; } else { phrase.Points += (int)Settings.ABtn;

我有以下代码:

    public void ABtn()
    {
        if (ABtnState == "E")
        {
            phrase.Points += -(int)Settings.ABtn;
            ABtnState = "D";
        }
        else
        {
            phrase.Points += (int)Settings.ABtn;
            ABtnState = "E";
        }
        if (BBtnState == "E")
        {
            phrase.Points += -(int)Settings.BBtn;
            BBtnState = "D";
        }
        if (CBtnState == "E") { phrase.Points += -(int)Settings.CBtn; CBtnState = "D"; }
        if (DBtnState == "E") { phrase.Points += -(int)Settings.DBtn; DBtnState = "D"; }
        if (EBtnState == "E") { phrase.Points += -(int)Settings.EBtn; EBtnState = "D"; }
        App.DB.SetPoints(phrase.PhraseId, phrase.Points);
        App.viewablePhrases = App.DB.GetViewablePhrases(Settings.Mode, Settings.Pts, true);
        Cif2aInfo = new String('☆', phrase.Points);
    }
appcenter在ABtn期间报告了以下崩溃:

DeckTabViewModel.ABtn()System.ArgumentOutOfRangeException:计数 不能小于零。参数名称:count

用这个stacktrace

Xamarin异常堆栈:System.ArgumentOutOfRangeException:计数 不能小于零。参数名称:计数在 System.String.Ctor(System.Char c,System.Int32计数)[0x0000d]输入 :0位于System.String.CreateString (System.Char c,System.Int32计数)[0x00000]英寸 :0处(包装器托管到托管) System.String..ctor(char,int)位于Japanese.DeckTabViewModel.ABtn() [0x00178]英寸:0英寸 Xamarin.Forms.Command+c__DisplayClass4_0.b_0(System.Object o) [0x00000]英寸:0英寸 中的Xamarin.Forms.Command.Execute(System.Object参数)[0x00000] :0 at Xamarin.Forms.ButtonElement.element已单击 (Xamarin.Forms.VisualElement VisualElement, Xamarin.Forms.Internals.iButtoneElement按钮元素管理器)[0x0001a] in:0 at Xamarin.Forms.Button.SendClicked()[0x00000]位于 :0 at Xamarin.Forms.Platform.Android.ButtonElementManager.OnClick (Xamarin.Forms.VisualElement元素,Xamarin.Forms.IButtonController 按钮控制器,Android.Views.View v)[0x00003]位于 :0 at Xamarin.Forms.Platform.Android.FastRenders.ButtonRenderer.Android.Views.View.IOnClickListener.OnClick (Android.Views.View v)[0x0000c]在 :0 at Android.Views.View+ion单击ListenerInvoker.n\u OnClick\u Landroid\u View\u视图_ (System.IntPtr jnienv,System.IntPtr native_u_uthis,System.IntPtr 本机_v)[0x00011]位于:0处 (包装器动态方法) Android.Runtime.DynamicMethodNameCounter.60(intptr、intptr、intptr)

有没有人知道是什么原因导致了这一问题,或者我如何纠正这一问题?

来自:

字符串(Char c,Int32 count)将新实例初始化为 由指定的Unicode字符重复指定的数字表示 时代

因此,如果您的短语.Points小于0,它将提示您该错误,请在中尝试以下操作:

在此之前,您需要检查值,如下所示:

Cif2aInfo = phrase.Points != null && phrase.Points > 0 ? new String('☆', phrase.Points) : string.Empty;

Cif2aInfo=新字符串('☆', 短语(点)这里有什么值
短语.Points
?正如错误所说的
count at System.String.Ctor
。问题在于
新字符串('☆', 短语(点)
Cif2aInfo = phrase.Points != null && phrase.Points > 0 ? new String('☆', phrase.Points) : string.Empty;