Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement a preprocessor for C# like JSX. CSX? #10316

Open
ApplePieCodes opened this issue Jan 19, 2025 · 4 comments
Open

Implement a preprocessor for C# like JSX. CSX? #10316

ApplePieCodes opened this issue Jan 19, 2025 · 4 comments
Labels
External Non-WPF Issue External issue, not caused by WPF 📭 waiting-author-feedback To request more information from author.

Comments

@ApplePieCodes
Copy link

ApplePieCodes commented Jan 19, 2025

I don't like databindings and tend to handle repetitive instances of a component in code. For example, I have code like this

foreach (ProjectReference project in projects.Projects)
{
    ListBoxItem item = new ListBoxItem();
    StackPanel proj = new StackPanel();
    proj.Orientation = Orientation.Horizontal;
    proj.VerticalAlignment = VerticalAlignment.Center;
    proj.HorizontalAlignment = HorizontalAlignment.Left;
    Image templateImg = new Image();
    templateImg.Width = 50;
    templateImg.Height = 50;
    templateImg.Margin = new Thickness(5, 5, 5, 5);
    switch (project.Template)
    {
        case "3D":
            templateImg.Source = new BitmapImage(new Uri("pack://application:,,,/Resources\\Images\\TemplateIcons\\3D.png"));
            break;
        case "3D+":
            templateImg.Source = new BitmapImage(new Uri("pack://application:,,,/Resources\\Images\\TemplateIcons\\3D_Plus.png"));
            break;
    }
    proj.Children.Add(templateImg);
    item.Content = proj;
    ListBox.Children.Add(item);
}

What if I could simplify the code to something like this?

foreach (ProjectReference project in projects.Projects)
{
    BitmapImage imgSource;
    switch (project.Template)
    {
        case "3D":
            imgSource = new BitmapImage(new Uri("pack://application:,,,/Resources\\Images\\TemplateIcons\\3D.png"));
            break;
        case "3D+":
            imgSource = new BitmapImage(new Uri("pack://application:,,,/Resources\\Images\\TemplateIcons\\3D_Plus.png"));
            break;
    }
    ListBox.Children.Add(
    <ListBoxItem>
       <StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Left">
            <Image Source=imgSource/>
        </StackPanel>
    </ListBoxItem>);

I feel like this would be a really helpful feature.

@miloush
Copy link
Contributor

miloush commented Jan 19, 2025

tip: use ```C# line to start a code block and ``` line to end it

It's runtime parsing rather than compiling, but would XamlReader.Parse(string) help?

@ApplePieCodes
Copy link
Author

tip: use C# line to start a code block and line to end it

It's runtime parsing rather than compiling, but you would XamlReader.Parse(string) help?

That sounds good, but i was suggesting just typing the XAML. i will start using that though

@miloush
Copy link
Contributor

miloush commented Jan 19, 2025

The issue is this request is more of a C# compiler request rather than WPF request.

Note that VB.NET does have XML literals, but the C# team decided not to do that, so that is an indication of their position.

Technically, WPF allows you to do that by means of XAML files. If you put your snippet into a XAML file, the compiler will sort of pre-compile it into BAML which you can access from the application. The advantage of this is you get full intellisense.

I think the best compromise you could get nowadays is to write a Roslyn source generator. Have your XAML stored e.g. in a string constant and the source generator will create a corresponding method or property that would return the object.

@lindexi
Copy link
Member

lindexi commented Jan 20, 2025

We can write the C# code in XAML, such as:

    <Grid>
        <Button Name="Button" HorizontalAlignment="Center" VerticalAlignment="Center"
                Click="Button_OnClick">The Button</Button>
    @code
    {
        void Button_OnClick(object sender, RoutedEventArgs e)
        {
            Button.Content = "The new content";
        }
    }
    </Grid>

@siagupta0202 siagupta0202 added 📭 waiting-author-feedback To request more information from author. External Non-WPF Issue External issue, not caused by WPF and removed Untriaged labels Jan 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
External Non-WPF Issue External issue, not caused by WPF 📭 waiting-author-feedback To request more information from author.
Projects
None yet
Development

No branches or pull requests

4 participants