我可以处理XAML中抛出的异常吗?

时间:2022-09-11 14:40:49

In my XAML I get all customers by binding to a GetAll property:

在我的XAML中,我通过绑定到GetAll属性获得所有客户:

<ListBox ItemsSource="{Binding GetAll}" 
     ItemTemplate="{StaticResource allCustomersDataTemplate}"
     Style="{StaticResource allCustomersListBox}">
</ListBox>

The GetAll property is an observable collection in my view model, which calls the Model to get all collection of customers:

GetAll属性是我的视图模型中的一个可观察集合,它调用Model来获取所有客户集合:

public class CustomersViewModel
{
    public ObservableCollection<Customer> GetAll {
        get
        {
            try
            {
                return Customer.GetAll;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }
}

If anything goes wrong in the model (badly formed XML file, etc.) then an exception bubbles up all the way to that GetAll property in the ViewModel.

如果模型中出现任何问题(格式错误的XML文件等),则异常会一直冒泡到ViewModel中的GetAll属性。

First Question: I was surprised that XAML doesn't seem to do anything with the exception and just goes ahead and displays nothing. Is this by design? Is this part of the "decoupling approach"?

第一个问题:我很惊讶XAML似乎没有对异常做任何事情,只是继续并且什么都不显示。这是设计的吗?这是“脱钩方法”的一部分吗?

Second Question: This leads me to think I could handle the exception in XAML somehow, such as

第二个问题:这使我认为我可以在某种程度上处理XAML中的异常,例如

Pseudo Code:

<Trigger>
    <Trigger.OnException>
        <TextBox Text="The customer data could not be loaded."/>
    </Trigger.OnException>
</Trigger>

Is something like the above code possible?

是否可能像上面的代码?

4 个解决方案

#1


4  

Firstly, I would imagine that it is not intended that XAML exceptions should be caught. They exist more as a tool to help the developer see how they need to fix their XAML code, though they have to of course occur at runtime (initialisation) because of the dynamic nature of XAML markup

首先,我认为不应该捕获XAML异常。它们更多地作为工具来帮助开发人员了解他们需要如何修复他们的XAML代码,尽管由于XAML标记的动态特性,它们当然必须在运行时(初始化)发生。

Saying that, you can handle XAML exceptions quite easily by wrapping the call to InitializeComponents within the constructor of your Windows class. You can then either catch all exceptions or specifically XamlParseException, whichever you find appropiate.

这样说,您可以通过在Windows类的构造函数中包含对InitializeComponents的调用来轻松处理XAML异常。然后,您可以捕获所有异常或特别是XamlParseException,无论您找到合适的。

Example from this blog post:

此博客文章的示例:

public partial class Window1 : System.Windows.Window 
{
    public Window1()
    {
        try
        {
            InitializeComponent();
        }
        catch (Exception ex)
        {
            // Log error (including InnerExceptions!)
            // Handle exception
        }
    }
}

#2


2  

You can use the FallBackValue to provide a value to use in the binding if an error occurs. Other than using FallBackValue you can't handle exceptions in xaml.

如果发生错误,您可以使用FallBackValue提供在绑定中使用的值。除了使用FallBackValue之外,您无法处理xaml中的异常。

You might also want to look into TargetNullValue which make a certain value equivalent to Null (e.g. if you set TargetNullValue=5 and your user enters 5 your setter will get Null and if your getter supplies Null the binding will display 5).

您可能还想查看TargetNullValue,它使某个值等于Null(例如,如果您设置TargetNullValue = 5并且您的用户输入5,则您的setter将获得Null,如果您的getter提供Null,则绑定将显示5)。

#3


1  

My first thought is that you could use a ValueConverter, check for value==null and then set some arbitrary property of the view model, through a converter parameter. You could then use a regular property trigger in your xaml to show the error.

我的第一个想法是你可以使用ValueConverter,检查value == null然后通过converter参数设置视图模型的一些任意属性。然后,您可以在xaml中使用常规属性触发器来显示错误。

I'm pretty sure you could do this with a binding validator too though so I'd look there first.

我很确定你也可以用绑定验证器做到这一点,所以我先看看那里。

EDIT: Yeah, take a look at http://msdn.microsoft.com/en-us/library/ms753962.aspx for an intro to binding validation rules,..that'll put you on the right path

编辑:是的,请查看http://msdn.microsoft.com/en-us/library/ms753962.aspx以获取绑定验证规则的介绍,...这将使您走上正确的道路

#4


0  

Yes, you may handle exception using XAML. It exists for unhandled exceptions. This link might help.. http://www.wpf-tutorial.com/wpf-application/handling-exceptions/

是的,您可以使用XAML处理异常。它存在未处理的异常。这个链接可能会有所帮助.. http://www.wpf-tutorial.com/wpf-application/handling-exceptions/

#1


4  

Firstly, I would imagine that it is not intended that XAML exceptions should be caught. They exist more as a tool to help the developer see how they need to fix their XAML code, though they have to of course occur at runtime (initialisation) because of the dynamic nature of XAML markup

首先,我认为不应该捕获XAML异常。它们更多地作为工具来帮助开发人员了解他们需要如何修复他们的XAML代码,尽管由于XAML标记的动态特性,它们当然必须在运行时(初始化)发生。

Saying that, you can handle XAML exceptions quite easily by wrapping the call to InitializeComponents within the constructor of your Windows class. You can then either catch all exceptions or specifically XamlParseException, whichever you find appropiate.

这样说,您可以通过在Windows类的构造函数中包含对InitializeComponents的调用来轻松处理XAML异常。然后,您可以捕获所有异常或特别是XamlParseException,无论您找到合适的。

Example from this blog post:

此博客文章的示例:

public partial class Window1 : System.Windows.Window 
{
    public Window1()
    {
        try
        {
            InitializeComponent();
        }
        catch (Exception ex)
        {
            // Log error (including InnerExceptions!)
            // Handle exception
        }
    }
}

#2


2  

You can use the FallBackValue to provide a value to use in the binding if an error occurs. Other than using FallBackValue you can't handle exceptions in xaml.

如果发生错误,您可以使用FallBackValue提供在绑定中使用的值。除了使用FallBackValue之外,您无法处理xaml中的异常。

You might also want to look into TargetNullValue which make a certain value equivalent to Null (e.g. if you set TargetNullValue=5 and your user enters 5 your setter will get Null and if your getter supplies Null the binding will display 5).

您可能还想查看TargetNullValue,它使某个值等于Null(例如,如果您设置TargetNullValue = 5并且您的用户输入5,则您的setter将获得Null,如果您的getter提供Null,则绑定将显示5)。

#3


1  

My first thought is that you could use a ValueConverter, check for value==null and then set some arbitrary property of the view model, through a converter parameter. You could then use a regular property trigger in your xaml to show the error.

我的第一个想法是你可以使用ValueConverter,检查value == null然后通过converter参数设置视图模型的一些任意属性。然后,您可以在xaml中使用常规属性触发器来显示错误。

I'm pretty sure you could do this with a binding validator too though so I'd look there first.

我很确定你也可以用绑定验证器做到这一点,所以我先看看那里。

EDIT: Yeah, take a look at http://msdn.microsoft.com/en-us/library/ms753962.aspx for an intro to binding validation rules,..that'll put you on the right path

编辑:是的,请查看http://msdn.microsoft.com/en-us/library/ms753962.aspx以获取绑定验证规则的介绍,...这将使您走上正确的道路

#4


0  

Yes, you may handle exception using XAML. It exists for unhandled exceptions. This link might help.. http://www.wpf-tutorial.com/wpf-application/handling-exceptions/

是的,您可以使用XAML处理异常。它存在未处理的异常。这个链接可能会有所帮助.. http://www.wpf-tutorial.com/wpf-application/handling-exceptions/