Xamarin.forms Xamarin表单:视图模型中出现显示错误
下面是Xamarin Forms项目的教程/示例,其中有一个绑定到视图模型的带有C#代码的视图。但是,我希望捕获视图模型中发生的异常,并将其显示在警报中,或者使用任何其他常用技术来显示错误 以下是使用刷新重新加载数据的视图:Xamarin.forms Xamarin表单:视图模型中出现显示错误,xamarin.forms,Xamarin.forms,下面是Xamarin Forms项目的教程/示例,其中有一个绑定到视图模型的带有C#代码的视图。但是,我希望捕获视图模型中发生的异常,并将其显示在警报中,或者使用任何其他常用技术来显示错误 以下是使用刷新重新加载数据的视图: <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyCompany.App.Views.DashboardPage"
xmlns:vm="clr-namespace:MyCompany.App.ViewModels"
xmlns:dashboard="clr-namespace:MyCompany.App.ViewModels.Dashboard;assembly=MyCompany.App"
Title="{Binding Title}">
...
<RefreshView x:DataType="dashboard:DashboardViewModel" Command="{Binding LoadItemsCommand}" IsRefreshing="{Binding IsBusy, Mode=TwoWay}">
... content
</RefreshView>
</ContentPage>
最后是加载发生的视图模型。它继承自教程中提供的BaseViewModel
public class DashboardViewModel : BaseViewModel
{
private DashboardItemViewModel _selectedItem;
public ObservableCollection<DashboardItemViewModel> Items { get; }
public Command LoadItemsCommand { get; }
public Command<DashboardItemViewModel> ItemTapped { get; }
public DashboardViewModel()
{
Title = "Dashboard";
Items = new ObservableCollection<DashboardItemViewModel>();
LoadItemsCommand = new Command(async () => await ExecuteLoadItemsCommand());
ItemTapped = new Command<DashboardItemViewModel>(OnItemSelected);
}
async Task ExecuteLoadItemsCommand()
{
IsBusy = true;
try
{
Items.Clear();
var items = await GetItems();
foreach (var item in items)
{
Items.Add(item);
}
}
finally
{
IsBusy = false;
}
}
private static async Task<List<DashboardItemViewModel>> GetItems()
{
// Where errors happen
return items;
}
public void OnAppearing()
{
IsBusy = true;
SelectedItem = null;
}
public DashboardItemViewModel SelectedItem
{
get => _selectedItem;
set
{
SetProperty(ref _selectedItem, value);
OnItemSelected(value);
}
}
async void OnItemSelected(DashboardItemViewModel item)
{
if (item == null || item.Uri.IsNotSet())
return;
await Shell.Current.GoToAsync(item.Uri);
}
}
公共类仪表板视图模型:BaseViewModel
{
专用仪表板项目视图模型_selectedItem;
公共可观察集合项{get;}
公共命令LoadItemsCommand{get;}
公共命令{get;}
公共仪表板视图模型()
{
Title=“仪表板”;
Items=新的ObservableCollection();
LoadItemsCommand=new命令(async()=>await ExecuteLoadItemsCommand());
ItemTapped=新命令(已选择);
}
异步任务ExecuteLoadItemsCommand()
{
IsBusy=true;
尝试
{
Items.Clear();
var items=等待GetItems();
foreach(项目中的var项目)
{
项目。添加(项目);
}
}
最后
{
IsBusy=false;
}
}
私有静态异步任务GetItems()
{
//发生错误的地方
退货项目;
}
公开无效
{
IsBusy=true;
SelectedItem=null;
}
公共仪表板项目视图模型SelectedItem
{
get=>\u选择编辑项;
设置
{
SetProperty(参考选择项,值);
未选择(值);
}
}
已选择异步项目(仪表板项目视图模型项目)
{
if(item==null | | item.Uri.IsNotSet())
返回;
wait Shell.Current.GoToAsync(item.Uri);
}
}
我在ContentPage中看不到任何用于捕获异常的可重写方法。捕获异常并将其显示在警报中的最佳方法是什么?我不确定您到底想要什么,但为了捕获错误,我使用try/catch方法
try
{
//your code here
}
catch(Exception exc)
{
await App.Current.MainPage.DisplayAlert("Warning", "Error: " + exc, "Ok");
}
您可以在VM上创建错误属性,并将其绑定到标签或其他文件UI@Jason这种方法的优点是标签保持可见,不像警报,一旦关闭标签,信息就会丢失。您还可以设置标签样式。谢谢,这是从任何地方访问警报的便捷方式。我不知道视图模型可以像那样与UI交互。我很高兴你发现我的答案很有用
try
{
//your code here
}
catch(Exception exc)
{
await App.Current.MainPage.DisplayAlert("Warning", "Error: " + exc, "Ok");
}