简体   繁体   English

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

[英]Can I handle thrown exceptions in XAML?

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. 第一个问题:我很惊讶XAML似乎没有对异常做任何事情,只是继续并且什么都不显示。 Is this by design? 这是设计的吗? Is this part of the "decoupling approach"? 这是“脱钩方法”的一部分吗?

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? 是否可能像上面的代码?

Firstly, I would imagine that it is not intended that XAML exceptions should be caught. 首先,我认为不应该捕获XAML异常。 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标记的动态特性,它们当然必须在运行时(初始化)发生。

Saying that, you can handle XAML exceptions quite easily by wrapping the call to InitializeComponents within the constructor of your Windows class. 这样说,您可以通过在Windows类的构造函数中包含对InitializeComponents的调用来轻松处理XAML异常。 You can then either catch all exceptions or specifically XamlParseException , whichever you find appropiate. 然后,您可以捕获所有异常或特别是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
        }
    }
}

You can use the FallBackValue to provide a value to use in the binding if an error occurs. 如果发生错误,您可以使用FallBackValue提供在绑定中使用的值。 Other than using FallBackValue you can't handle exceptions in xaml. 除了使用FallBackValue之外,您无法处理xaml中的异常。

You might also want to look into TargetNullValue which make a certain value equivalent to Null (eg 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)。

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. 我的第一个想法是你可以使用ValueConverter ,检查value==null然后通过converter参数设置视图模型的一些任意属性。 You could then use a regular property trigger in your xaml to show the error. 然后,您可以在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获取绑定验证规则的介绍,...这将使您走上正确的道路

Yes, you may handle exception using XAML. 是的,您可以使用XAML处理异常。 It exists for unhandled exceptions. 它存在未处理的异常。 This link might help.. http://www.wpf-tutorial.com/wpf-application/handling-exceptions/ 这个链接可能会有所帮助.. http://www.wpf-tutorial.com/wpf-application/handling-exceptions/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM