在单击打开一个窗口的按钮后,我无法理解如何填充我的 ObservableCollection,然后在一个单独的类中运行一些逻辑,该类(目前)使用具有 Name 属性的字符串填充 ObservableCollection。
我的按钮点击看起来像这样
public partial class FailedIgnoredWindow : Window
{
ObservableCollection<BindingToXaml> MyVar = new ObservableCollection<BindingToXaml>();
public FailedIgnoredWindow()
{
InitializeComponent();
FailedIgnoredDialogue.ItemsSource = MyVar;
}
private async void IgnoredFailed_Click(object sender, RoutedEventArgs e)
{
FailedIgnoredWindow win1 = new FailedIgnoredWindow();
await FailedIgnoredRetrieved.FailedIgnoredRetrievedAndShow(win1.FailedIgnoredDialogue);
this.Visibility = Visibility.Hidden;
win1.Show();
}
这(等待 FailedIgnoredRetrieved.FailedIgnoredRetrievedAndShow(win1.FailedIgnoredDialogue);) 转到我的另一个运行逻辑以填充 ObservableCollection 的类。
看起来像这样...
public static async Task FailedIgnoredRetrievedAndShow(ListBox commentLabel)
{
void AllEventsComment(string comment)
{
if (commentLabel != null)
{
commentLabel.ItemsSource += comment;
}
}
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)
{
commentLabel = null;
MyVar = new ObservableCollection<BindingToXaml>();
void AddToList(string v)
{
MyVar.Add(new BindingToXaml($"{v}"));
}
foreach (var eventsText in result.Events)
if (eventsText.EventStatus == 1)
{
AddToList($"Red {eventsText.CheckId}");
}
else if (eventsText.EventStatus == 0)
{
AddToList($"Orange {eventsText.CheckId}");
}
}
AllEventsComment($"{MyVar}");
}
}
一个断点显示我的 OC 填充在它的类中,但我正在努力理解如何将填充的变量 MyVar 通过到 FailedIgnoredWindow 进行绑定!