简体   繁体   English

C#/WPF:这个回调我错过了什么?

[英]C#/WPF: What am i missing with this callback?

I'm creating a WPF app and i want objects to show up in a listbox, but i'm missing something with the callback: b.CreateGuest() .我正在创建一个 WPF 应用程序,我希望对象显示在列表框中,但我在回调中遗漏了一些东西: b.CreateGuest() What should be in the parameters?参数里应该有什么?

Main:主要的:

 public partial class MainWindow : Window
{
    bool isBarOpen = false;

    public MainWindow()
    {
        InitializeComponent();

        BouncerListBox.DisplayMemberPath = "name";
    }

    private void CloseButton_Click(object sender, RoutedEventArgs e)
    {
        isBarOpen = false;
    }

    private void OpenButton_Click(object sender, RoutedEventArgs e)
    {
        isBarOpen = true;
        if (isBarOpen == true)
        {
            Bouncer b = new Bouncer();
            BouncerListBox.Items.Insert(0, b.CreateGuest());

        }

    }
}

Bouncer class:保镖类:

   public class Bouncer
{
    public void CreateGuest(Action <string> callback)
    {
        List<string> guests = new List<string>();
        //long list of names here

        Random rTime = new Random();
        int randomTimePosition = rTime.Next(3, 10) * 1000;
        Thread.Sleep(randomTimePosition);
        Random rGuest = new Random();
        int randomGuestPosition = rGuest.Next(guests.Count);
        string randomName = guests[randomGuestPosition];

        var patron = new Patron();
        patron.Name = randomName;

        callback($" The bouncer lets in {patron.Name} into the bar.");

    }
}

The patron class just have a property with get set name.赞助人类只有一个带有 get set name 的属性。

When you are calling b.CreateGuest() you are not passing a callback当您调用b.CreateGuest()您没有传递回调

to do this with a callback (note this is not the best approach)用回调来做到这一点(注意这不是最好的方法)

CreateGuest(msg => BouncerListBox.Items.Insert(0, msg ) );

better would be to just have最好是有

return " The bouncer lets in {patron.Name} into the bar."; 

at the end of CreateGuest() then your original code would work在 CreateGuest() 结束时,您的原始代码将起作用

or you could try an mvvm aproach and create a message list on the bar using ObservableCollection<string> then bind that to the listbox's itemSource then just add your message to the bars message list或者您可以尝试使用 mvvm 方法并使用ObservableCollection<string>在栏上创建消息列表,然后将其绑定到列表框的 itemSource,然后将您的消息添加到栏消息列表中

the rest of your code is truly awful though with useless variables being created to no purpose尽管无用的变量被无意义地创建,但您的其余代码确实很糟糕

cleaned up your code should look something like this清理你的代码应该是这样的

Window (no added code behind)窗口(后面没有添加代码)

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        >
    <Window.DataContext>
        <local:Bar/>
    </Window.DataContext>
    <StackPanel>
        <ToggleButton IsChecked="{Binding IsOpen}">Is Open</ToggleButton>
        <ListBox ItemsSource="{Binding Guests}" DisplayMemberPath="Name"/>
        <Button Command="{Binding Bouncer.AllowGuestAccess, Mode=OneWay}">Allow guest in</Button>
        <ListBox ItemsSource="{Binding Message}"/>

    </StackPanel>
</Window>

your object model (using prism)您的对象模型(使用棱镜)

public class Bar :BindableBase
{
    public Bar()
    {
        Bouncer = new Bouncer() { Bar = this };
    }

    private bool _IsOpen;

    public bool IsOpen
    {
        get => _IsOpen;
        set => SetProperty(ref _IsOpen, value,()=>Bouncer.AllowGuestAccess.RaiseCanExecuteChanged());
    }
    public Bouncer Bouncer { get;  }
    public ObservableCollection<string> Message { get; } = new ObservableCollection<string>();
    public ObservableCollection<Patron> Guests { get; } = new ObservableCollection<Patron>();
}

public class Bouncer
{
    public Bouncer()
    {
        AllowGuestAccess = new DelegateCommand(() =>
        {
            var patron = new Patron();
            Bar.Guests.Add(patron);
            Bar.Message.Insert(0, $"{patron.Name} has been admitted");
        },()=>Bar.IsOpen);
    }
    public Bar Bar { get; set; }

    public DelegateCommand AllowGuestAccess { get; }
}
public class Patron
{
    public static Random Rnd { get; } = new Random();
    public static List<string> Names { get; } = new List<string>();

    public Patron()
    {
        Name = Names[Rnd.Next(Names.Count)];
    }

    public string Name { get; }
}
BouncerListBox.Items.Insert(0, b.CreateGuest( status => {
  // do something with status here...
}));

But b.CreateGuest() should return an Object, which is what BouncerListBox.Items.Insert(0, b.CreateGuest());但是 b.CreateGuest() 应该返回一个对象,这就是 BouncerListBox.Items.Insert(0, b.CreateGuest()); expect.预计。

May you code should be愿你的代码应该是

public string CreateGuest()
{

    List<string> guests = new List<string>();
    //long list of names here

    Random rTime = new Random();
    int randomTimePosition = rTime.Next(3, 10) * 1000;
    Thread.Sleep(randomTimePosition);
    Random rGuest = new Random();
    int randomGuestPosition = rGuest.Next(guests.Count);
    string randomName = guests[randomGuestPosition];

    var patron = new Patron();
    patron.Name = randomName;

    return $" The bouncer lets in {patron.Name} into the bar.";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM