我的代码没有在“;序列”;

本文关键字:序列 代码 我的 | 更新日期: 2023-09-27 18:19:58

我在我的项目(Coding4Fun.Toolkit)中使用API来显示带有文本框的messageBox。因此,当我调用MessagePrompt代码时,它没有在正确的时间运行。

private void holdListAdd(object sender, System.Windows.Input.GestureEventArgs)
{
    var dc = ((FrameworkElement)sender).DataContext;
    Fields fi = (Fields)dc;
    if (List2.Items.Contains(fi))
    {
        MessageBox.Show("Esse item já foi adicionado!");
    }
    else
    {
        MessagePrompt messagePrompt = new MessagePrompt();
        messagePrompt.Body = new BodyUserControl();
        messagePrompt.Show();
        IsolatedStorageSettings iso = IsolatedStorageSettings.ApplicationSettings;
        if (iso.TryGetValue<string>("isoQuantity", out retornaNome))
        {
            qtd = retornaNome;
        }
        fi.Quantity = qtd;
        List2.Items.Add(fi);
        MessageBox.Show("Item Adicionado com sucesso!");
    }

请注意,在holdListAdd中,最后一个代码是List2.Items.Add(fi);,但它在MessagePrompt之前运行。为什么?

BodyUserControl.xaml:

<StackPanel x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}"  Margin="0,0,0,10">
    <Grid Height="157">
        <TextBlock HorizontalAlignment="Center" Margin="55,0,51,86" Width="374">
            <Run Text="       "/>
            <Run Text="Digite a quantidade a adicionar:"/>
        </TextBlock>
        <TextBox TextWrapping="Wrap" Name="Quantity" Margin="103,68,222,17"/>
        <Button Content="Adicionar" HorizontalAlignment="Left" Margin="225,68,0,0" VerticalAlignment="Top" Name="addQuantity" Click="addQuantity_Click"/>
    </Grid>
</StackPanel>

BodyUserControl.cs:

public partial class BodyUserControl : PhoneApplicationPage
{
    public BodyUserControl()
    {
        InitializeComponent();
    }
    private void addQuantity_Click(object sender, RoutedEventArgs e)
    {
        IsolatedStorageSettings iso = IsolatedStorageSettings.ApplicationSettings;
        {
            if (iso.Contains("isoQuantity"))
            {
                iso["isoQuantity"] = Quantity.Text;
            }
            else
            {
                iso.Add("isoQuantity", Quantity.Text);
            }
        }
    }

我的代码没有在“;序列”;

这是因为在显示MessagePrompt时代码仍在继续。如果你想在MessagePrompt完成后做一些事情,你必须向MessagePrompt.Completed分配一个事件处理程序。类似的东西

messagePrompt.Completed += messagePrompt_Completed;
messagePrompt.Show();
void messagePrompt_Completed(object sender, PopUpEventArgs<object, PopUpResult> e)
{
    IsolatedStorageSettings iso = IsolatedStorageSettings.ApplicationSettings;
    if (iso.TryGetValue<string>("isoQuantity", out retornaNome))
    {
        qtd = retornaNome;
    }
    fi.Quantity = qtd;
    List2.Items.Add(fi);
    MessageBox.Show("Item Adicionado com sucesso!");
};