C# 如何绑定到字符串I';你创造了什么?

C# 如何绑定到字符串I';你创造了什么?,c#,wpf,xaml,data-binding,C#,Wpf,Xaml,Data Binding,我在这个类中创建了一个名为myVar的列表 class FailedIgnoredRetrieved { public static async Task FailedIgnoredRetrievedAndShow(ListView commentLabel) { void AllEventsComment(string comment) { if (commentLabe

我在这个类中创建了一个名为myVar的列表

class FailedIgnoredRetrieved
    {

        public static async Task FailedIgnoredRetrievedAndShow(ListView commentLabel)
        {

            void AllEventsComment(string comment)
            {
                if (commentLabel != null)
                {
                    commentLabel.ItemsSource += comment; //+ "\n";
                }
            }

            AllEventsComment("...");
            var api = new ApiClient();
            var retrieved = ApiToken.Load();
            if (retrieved == null)
            {
                AllEventsComment("Failed, not signed in");
                App.SwitchIcon(NotifyIcons.GreyIcon);
            }
            else
            {
                var result = await api.GetAllEventsAsync(retrieved.Token);
                if (result.IsSuccessful)
                {

                    List<string> myVar = new List<string>();

                    void AddToList(string v)
                    {
                        myVar.Add(v);
                    }

                    foreach (var eventsText in result.Events)
                        if (eventsText.EventStatus == 1)
                        {
                            AddToList($"Red {eventsText.CheckId}");
                        }

                        else if (eventsText.EventStatus == 0)
                        {
                            AddToList($"Orange {eventsText.CheckId}");
                        }
                }
            }
        }
    }     
}

我试图用这些属性创建一个单独的类,但我很难完全理解我需要做什么。

myVar
是在mé方法中声明的,因此它只能在这个方法中访问。 如果要从外部访问它,应将其设置为类
FailedIgnoredRetrieved
的公共属性(注意,在这种情况下,如果要从方法
FailedIgnoredRetrievedAndShow
修改属性,则必须使其不是静态的)

另一种方法是让该方法返回列表,以便您得到结果(声明将变成
TaskFailedIgnoredRetrievedAndShow(…

您的代码中似乎有一些混乱,因此我建议您阅读一篇关于变量范围的教程 您还应该检查关于访问修饰符的回答

我还建议在开始时远离本地函数(函数中的函数),因为我认为这会增加混淆


在阅读我的答案中的链接后,让我知道它是否更有意义。

Ostas,谢谢你花时间,这非常有帮助……我认为函数和方法是一样的吗?现在我只需要正确地进行数据绑定,因为目前它每行打印一个字符!是的,方法是属于b类的函数简单地说,对于c#来说,正确的术语应该是method,但我经常混合使用这两个术语。
 public partial class FailedIgnoredWindow : Window
    {


        public FailedIgnoredWindow()
        { 
            InitializeComponent();

            FailedIgnoredDialogue.ItemsSource = 

        }

      

        private async void AllEvents_Click(object sender, RoutedEventArgs e)
        {
            AllEventsWindow win2 = new AllEventsWindow();
            this.Visibility = Visibility.Hidden;
            win2.WindowStartupLocation = 0;
            //win2.Left = 0;
            //win2.Top = 0;
            win2.Show();
            await AllEventsRetrieved.AllEventsRetrievedAndShowCount(win2.AllEventsDialogue);
        }
    }