Why is the event binding to a method, why does it work in UWP?

UWPa new way DataBinding, Compiled Bindingusing the markup extension {x:Bind}when I found this new feature, I have found that we can bind an event to a method!

Example:

Xaml:

<Grid>
    <Button Click="{x:Bind Run}" Content="{x:Bind ButtonText}"></Button>
</Grid>

Code for:

private string _buttonText;

public string ButtonText
{
     get { return _buttonText; }
     set
         {
             _buttonText = value;
             OnPropertyChanged();
         }
}

public MainPage()
{
    this.InitializeComponent();
    ButtonText = "Click !";
}

public async void Run()
{
    await new MessageDialog("Yeah the binding worked !!").ShowAsync();
}

Result:

enter image description here

And since the {x: Bind} bindings are evaluated at runtime, and the compiler generates some files that represent this binding, so I went there to find out what is happening, therefore, in the MainPage.g.cs file (MainPage is the xaml file) . I found this:

 // IComponentConnector

 public void Connect(int connectionId, global::System.Object target)
 {
      switch(connectionId)
      {
           case 2:
           this.obj2 = (global::Windows.UI.Xaml.Controls.Button)target;
                        ((global::Windows.UI.Xaml.Controls.Button)target).Click += (global::System.Object param0, global::Windows.UI.Xaml.RoutedEventArgs param1) =>
           {
               this.dataRoot.Run();
           };
                break;
           default:
                break;
      }
}

The compiler seems to know that this is a valid binding, in addition, it creates an appropriate event handler and calls the corresponding method inside.

! ?? , . {x: Bind} , , non dependency , , ?

+4
2

, {x:Bind}.

, , ,

public sealed partial class MyUserControl : UserControl
{
    public Color BackgroundColor
    {
        get { return ((SolidColorBrush)Background).Color; }
        set { Background = new SolidColorBrush(value); }
    }
}

{x:Bind}, :

<local:MyUserControl BackgroundColor="{x:Bind ViewModel.BgColor}" />

while

<local:MyUserControl BackgroundColor="{Binding ViewModel.BgColor}" />

.

+1

, "x: Bind".

https://msdn.microsoft.com/en-us/library/windows/apps/mt204783.aspx

- . , . : = "{x: Bind rootFrame.GoForward}".

:

  • .
  • .
  • , .

, 2.

+2

Source: https://habr.com/ru/post/1627215/


All Articles