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

时间:2017-10-31 09:24:28

标签: c# wpf callback delegates

我正在创建一个WPF应用程序,我希望对象显示在列表框中,但我错过了回调的内容:b.CreateGuest()。参数应该是什么?

主:

 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());

        }

    }
}

保镖班:

   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.");

    }
}

patron类只有一个具有get set name的属性。

2 个答案:

答案 0 :(得分:0)

当您呼叫b.CreateGuest()时,您没有通过回叫

使用回调执行此操作(请注意,这不是最佳方法)

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

更好的是拥有

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

在CreateGuest()结束时,您的原始代码将起作用

或者您可以尝试mvvm aproach并使用ObservableCollection<string>在栏上创建一个消息列表,然后将其绑定到列表框的itemSource,然后将消息添加到栏消息列表

尽管无用的变量被创建为无用的

,但其余的代码确实非常糟糕

清理你的代码应该看起来像这样

窗口(后面没有添加代码)

<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>

您的对象模型(使用棱镜)

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; }
}

答案 1 :(得分:0)

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

但是b.CreateGuest()应该返回一个Object,这就是BouncerListBox.Items.Insert(0,b.CreateGuest());期望的。

你的代码应该是

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.";
}