我的Universal WP8应用程序中有几个ContentDialog。问题是,当我刚刚在页面A上使用一个并且我导航到页面B并尝试使用第二个时,我的应用程序崩溃并出现错误WINRT:一次只能打开一个ContentDialog。
我没有使用基本按钮,但我自己用命令实现了按钮,如下所示。
<ContentDialog x:Name="contentDialog"
x:Class="ThuisApp.Content.PostComment"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ThuisApp.Content"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="Post comment" DataContext="{Binding PostCommentViewmodel, Mode=OneWay, Source={StaticResource ViewmodelLocator}}" Foreground="{StaticResource MainAspectColor}" FullSizeDesired="True">
<Grid Margin="0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<StackPanel VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<Grid Margin="0,0,0,12">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="12"/>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Border HorizontalAlignment="Left" Height="50" Margin="0" VerticalAlignment="Top" Width="50" BorderBrush="{StaticResource MainAspectColor}" BorderThickness="1" >
<Grid>
<Image Source="ms-appx:///Assets/profile-placeholder.png"/>
<Image Source="{Binding Report.userID, Converter={StaticResource UserIdToImageUrlConverter}}"/>
</Grid>
</Border>
<TextBlock HorizontalAlignment="Left" Text="{Binding Report.username}" VerticalAlignment="Top" FontFamily="{StaticResource ReportUserFont}" FontSize="{StaticResource ReportUserFontSize}" TextTrimming="CharacterEllipsis" Margin="0" Grid.Column="2" Foreground="{StaticResource ReportHeaderTextColor}"/>
<TextBlock HorizontalAlignment="Right" TextWrapping="Wrap" Text="{Binding Report.createDate, Converter={StaticResource TimePassedConverter}}" VerticalAlignment="Top" FontSize="{StaticResource ReportHeaderInfoFontSize}" FontFamily="{StaticResource ReportHeaderInfoFont}" FontWeight="Light" Grid.Column="3" Foreground="{StaticResource ReportHeaderTextColor}"/>
</Grid>
<TextBlock x:Name="body" Style="{StaticResource MessageDialogContentStyle}" TextWrapping="Wrap" MaxHeight="140" TextTrimming="WordEllipsis" Text="{Binding Report.Message}" FontFamily="{StaticResource ItemBodyFont}" FontSize="{StaticResource ItemBodyFontSize}" Foreground="{StaticResource ItemBodyTextColor}"/>
<TextBox Header="Comment" TextWrapping="Wrap" MinHeight="140" Text="{Binding Comment, Mode=TwoWay}" HeaderTemplate="{StaticResource TextBoxHeader}" SelectionHighlightColor="{StaticResource TextBoxSelectionHighlightColor}"/>
<!-- Content body -->
</StackPanel>
<Grid VerticalAlignment="Bottom" Grid.Row="1" Margin="0,12,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button Content="post comment" HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="0,0,12,0" Style="{StaticResource FullColorButton}" Background="#FF42A5F5" Foreground="White" Command="{Binding PostComment}"/>
<Button Content="cancel" HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="12,0,0,0" Style="{StaticResource FullColorButton}" Background="#FF42A5F5" Foreground="White" Grid.Column="1" Command="{Binding Cancel}"/>
</Grid>
</Grid>
我通过使用Galasoft的Messenger向View发送消息来处理Init和关闭。
public Constructor
Messenger.Default.Register<bool>(true, MessengerToken.OpenClosePostCommentPopup, OpenClosePostCommentPopup);
}
public async void OpenClosePostCommentPopup(bool isOpen)
{
if (isOpen && !_isDialogOpen)
{
_dialog = new PostComment();
_dialog.Opened += dialog_Opened;
_dialog.Closed += dialog_Closed;
await _dialog.ShowAsync();
}
else
{
_dialog.Hide();
_dialog = null;
}
}
void dialog_Closed(ContentDialog sender, ContentDialogClosedEventArgs args)
{
_isDialogOpen = false;
}
void dialog_Opened(ContentDialog sender, ContentDialogOpenedEventArgs args)
{
_isDialogOpen = true;
}
我知道它击中了.Hide而且它隐藏了,但由于某种原因,它仍然认为它是以某种方式打开的。有没有人有任何想法?
按要求:下面是调用打开框的命令
PostComment = new RelayCommand<ReportDB>(item =>
{
//Load information into VM
Messenger.Default.Send<ReportDB>(item, MessengerToken.LoadReportToComment);
//Opens the popup
Messenger.Default.Send<bool>(true, MessengerToken.OpenClosePostCommentPopup);
});
我在带有问题鼠标的Windows 10 Universal App上遇到了同样的问题(双击)。因此,如果双击该按钮,将显示第一个对话框,但已阻止第二个对话框。因此解决方案是不要同时打开两个ContentDialog。
这是我的解决方案,以避免异常:
private static List<MessageDialog> dialogOpenQueue = new List<MessageDialog>();
public string Message { get; set; }
public MessageDialog()
{
this.InitializeComponent();
}
private static bool quickShowRunning = false;
public async static Task QuickShowAsync(string title, string message)
{
MessageDialog dialog = new MessageDialog() { Title = title, Message = message };
dialogOpenQueue.Add(dialog);
if (quickShowRunning)
return;
quickShowRunning = true;
for (int i = 0; i< dialogOpenQueue.Count; i++)
{
await dialogOpenQueue[i].ShowAsync();
dialogOpenQueue.RemoveAt(i);
i--;
}
quickShowRunning = false;
}
如果您想从您提交的特定对话框中获取结果,并避免显式排队和循环,请尝试"Only a single ContentDialog can be open at any time." error while opening another contentdialog中的方法