Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 警报框不在xamarin表单中显示,DisplayAlert()_C#_Xaml_Xamarin_Xamarin.forms - Fatal编程技术网

C# 警报框不在xamarin表单中显示,DisplayAlert()

C# 警报框不在xamarin表单中显示,DisplayAlert(),c#,xaml,xamarin,xamarin.forms,C#,Xaml,Xamarin,Xamarin.forms,我试图在点击或单击所选的ListView框后显示提醒框。当前selectedItem正在绑定到SelectedTicket,这将触发ViewModel中的对象调用SelectedTicket。触发SelectedTicket后,它将使用DisplayAlert方法执行popup.show方法。执行DisplayAlert方法get,但不显示AlertBox ============================================== ////Xaml页面 <ListView

我试图在点击或单击所选的ListView框后显示提醒框。当前selectedItem正在绑定到SelectedTicket,这将触发ViewModel中的对象调用SelectedTicket。触发SelectedTicket后,它将使用DisplayAlert方法执行popup.show方法。执行DisplayAlert方法get,但不显示AlertBox

==============================================

////Xaml页面

<ListView ItemsSource="{Binding TicketList,Mode=OneWay}" RowHeight="130"
     ItemTemplate="{StaticResource TicketListTileTemplate}"  BackgroundColor="#d9deeb"
     SelectedItem="{Binding SelectedTicket, Mode=TwoWay}" SeparatorVisibility="None">
</ListView>
======================================================

////弹出窗口.cs

async public void Show()
{
    DisplayAlert("Alert", "Your have been alerted", "OK");
}
DisplayAlert仅对ContentPage或NavigationPage等页面对象可用。请参见,您的Popup.cs可能不是页面对象。此外,您也没有等待DisplayAlert,这是您一直希望使用异步方法执行的操作。最后,您的Show方法可能没有在UI线程上运行

与其尝试从ViewModel中显示警报,不如尝试从XAML页面的代码隐藏中显示警报,如下所示:

XAML:


我看不到DisplayAlert的一个最常见的原因是,它是在屏幕上不活动的页面上被调用的

作为快速解决方法/测试,您可以

await Application.Current.MainPage.DisplayAlert("Alert", "Your have been alerted", "OK");
如果这是可行的,我的第一个假设就得到了证实

我总是试图保持代码的整洁,因此从ViewModel调用当然是一个好方法。通常,MVVM库有一些代码来帮助显示警报

<ListView ItemsSource="{Binding TicketList,Mode=OneWay}" 
          RowHeight="130"
          ItemTemplate="{StaticResource TicketListTileTemplate}" BackgroundColor="#d9deeb"
          SelectedItem="{Binding SelectedTicket, Mode=TwoWay}"
          SeparatorVisibility="None"
          ItemSelected="OnItemTapped"> <!-- Notice ItemTapped here will trigger when an item is tapped, imagine that -->
....

private TicketViewModel _viewModel = new TicketViewModel();

....

public async void OnItemTapped (object o, ItemTappedEventArgs e) { //Notice I added 'async' to the method here, that is so we can 'await' the DisplayAlert below (ALWAYS 'await' the DisplayAlert and ANY other async methods)
    TicketListItem item = (TicketListItem)o;

    if (item != null) {
        await DisplayAlert("Alert", "Your have been alerted", "OK"); //Notice the 'await' here
        _viewModel.SelectedTicket = null;
    }
}
await Application.Current.MainPage.DisplayAlert("Alert", "Your have been alerted", "OK");