Check binding compilation time?

I'm moderately new to MVVM, and I understand some of the advantages of MVVM over simple xaml code (e.g. VM testability). We use Caliburn Micro to do some bindings, but this kind of irrelevance, I could use a specific binding {binding ...}, and my question will more or less remain the same.

I understand the logic that a virtual machine does not need to know anything about V if everything is done correctly, but it seems that since we use dynamic binding of runtime between V and VM, the compiler cannot check the validity of each binding expression. For example, if I have a user interface component called UserName, then Caliburn will try to associate it with a VM property called UserName at runtime (similar to doing something like Value = "{binding UserName}"). However, if I rename my VM property or skip the name of the user interface component or the binding expression, we do not get any signs of a problem until runtime.

It seems to me that it would be very nice to tell xaml that the component or page of the user interface will be bound to a specific interface, so that the compiler can do some work that would otherwise (and be redone) by the testing department (and this is what I often do).

Is there any way to tell xaml that we are binding to an interface or object type? Is there any tool that can connect to the xaml app to check for bindings?

+5
source share
4 answers

XAML, , , , , XAML XAMLReader, TraceListener .

, , , . - ()

Binding createBinding<TProperty>(Expression<Func<TProperty>> property, object source)
{
    MemberExpression me = property.Body as MemberExpression;
    if (me == null || me.Expression != property.Parameters[0]
          || me.Member.MemberType != MemberTypes.Property) {
        throw new InvalidOperationException(
            "Now tell me about the property");
    }
    Binding b = new Binding(me.Member.Name);
    b.Source = source;

    return b;
}

// sample code
Binding b = createBinding(()=>this.FontSize, this);
textBlock1.SetBinding(TextBlock.FontSizeProperty, b);
+4
+2

: " - , xaml ?".

, Snoop . , .

. Solution 8 at ReSharper WPF: " " MyVariable " - DataContext ".

0

You can easily check if any of your bindings failed to find binding errors in the output window at run time. You may need to set debugging information to bind to the verbose.

-1
source

All Articles