我在我的项目(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);
}
}
}
答案 0 :(得分:3)
这是因为您的代码会在显示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!");
};