Write lambda function into XAML

Jesse_JiangJesse_Jiang USMember ✭✭✭
edited October 10 in Xamarin.Forms Evolution

Summary

I'm looking used flux in Xamarin, in their Github sample, https://github.com/facebook/flux/tree/master/examples/flux-todomvc I found an interesting syntax in HTML and JavaScript

          <button
            className="destroy"
            onClick={
              // Empty function for now, we will implement this later.
              () => {}
            }
          />

So I'm thinking, if it is possbile in XAML, like in Button click function, we can access all XAML element in that lambda function.

API Changes

With the help with roslyn, we can load lambda from Xaml directly and run it in runtime.

Intended Use Case

Maybe we can build some cool feature from flux pattern or reactive

0
0 votes

Open · Last Updated

Posts

  • StephaneDelcroixStephaneDelcroix USInsider, Beta ✭✭✭✭

    A more valid way to write this in xaml would be using x:Code and CDATA, but we do not support this right now.

  • RyanDixonRyanDixon USMember ✭✭✭
    edited October 10

    @StephaneDelcroix said:
    A more valid way to write this in xaml would be using x:Code and CDATA, but we do not support this right now.

    Hey Stephane, thanks for some of the great work going into forms!

    I know this sort of design goes against the point of MVVM in a way, but is there any reason as to why this hasn't been done yet? Or is it just a backlog thing? I expect it would require quite significant changes to XAMLC as well?
    For example, maybe something like the following? :smile:

    <?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="App.Views.WelcomePage">
    
                <ContentPage.Resources>
                    <ResourceDictionary>
                        <x:Code x:Key="ButtonHandler">
                        <![CDATA[
                            void ButtonOnClick(object sender, RoutedEventArgs args) 
                            {
                                System.Diagnostics.Debug.WriteLine("Yay!");
                            }
                        ]]>
                        </x:Code>
                    </ResourceDictionary>
                </ContentPage.Resources>
    
                <ContentPage.Content>
                    <Button x:Name="MyButton" Tapped="{StaticResource Key=ButtonHandler}" Text="Click Me" />
                </ContentPage.Content>
    
    </ContentPage>
    
  • StephaneDelcroixStephaneDelcroix USInsider, Beta ✭✭✭✭

    The fact that it goes against my principle of clean code is the main reason why this isn't implemented yet (and why it might never be).
    To me, it brings no added value as all our xaml controls have xaml.cs code behind code files.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    The one legitimate reason I can so for it is because then it could be part of a XAML template.
    If its only supported in the .xaml.cs then you have to inherit the code-behind of your UI and that clean code starts becoming less clean.

    public class CustomPage : ContentPage
    
    <local:Custom page xmlns:blahblahblah>
    
  • Jesse_JiangJesse_Jiang USMember ✭✭✭

    My idea is with this feature we can build more reactive code, like

         <button
            className="destroy"
            onClick={          
              () => {
                var frame = new Frame{ BackgroundColor = Color.Red};
                this.Page.Add(frame)
              }
            }
          />
    
  • Gigex42Gigex42 USMember ✭✭✭

    @StephaneDelcroix said:
    The fact that it goes against my principle of clean code is the main reason why this isn't implemented yet (and why it might never be).
    To me, it brings no added value as all our xaml controls have xaml.cs code behind code files.

    +1 on that.

    -1 on the suggestion.
    For me XAML is just a markup language which should not contain code. Having 2 files containing code for 1 page does make it a little messy and not good to read the code.

  • RyanDixonRyanDixon USMember ✭✭✭

    I was just more curious as if there was a technical reason behind it!
    However I have to agree that it makes more sense just to use the .cs behind it. (And in other implementations, use an MVVM framework which handles navigation and event -> commanding)

    Seems like just another mechanism to make picking Xamarin Forms up, even more complicated for newbies :smile:

Sign In or Register to comment.