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

Q) Transform XAML into C# API? #95

Closed
zezba9000 opened this Issue Dec 5, 2018 · 12 comments

Comments

Projects
None yet
5 participants
@zezba9000

zezba9000 commented Dec 5, 2018

I know the code release is new and may not have everything yet but what API(s) would I use to transform XAML into a C# partial class for code behind as is done normally in WPF?

@dotMorten

This comment has been minimized.

Collaborator

dotMorten commented Dec 5, 2018

@zezba9000 Check out the benchmark in issue 94 - it parses a XAML string into an object.

@zezba9000

This comment has been minimized.

zezba9000 commented Dec 6, 2018

I'm looking to get the actual C# translated output a XAML file would produce not the API that uses reflection to generate an object instance. What API translates XAML into C# is the question.

I saw the API you pointed out but its not what I'm looking for unless there is another part to it.

@mikedn

This comment has been minimized.

mikedn commented Dec 6, 2018

but what API(s) would I use to transform XAML into a C# partial class for code behind as is done normally in WPF?

There's no such thing. Well, there is some code behind (likely generated by some WPF specific msbuild task and not by WPF proper) but that code doesn't do a lot. The bulk of loading an XAML page simply consists of reading the XAML (or BAML) into an object.

@zezba9000

This comment has been minimized.

zezba9000 commented Dec 6, 2018

There's no such thing.

Not sure what you mean as you can view the generated C# code behind in any WPF app. That logic is done somewhere. Its not magic. Am I asking the question with the wrong terminology?

Not sure why this question is being misunderstood.
What API generates the "MainWindows.g.i.cs" partial class for the "MainWindow.xaml / MainWindow.cs" parts that allow for compile time checks?

This XAML gets generated into C#

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid Name="myGrid">
		
    </Grid>
</Window>

C# output for the XAML

#pragma checksum "..\..\MainWindow.xaml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "0A6F47C5279FC5A22A0B177749CE6047474732F8"
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.42000
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Automation;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Effects;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Windows.Media.TextFormatting;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Shell;
using WpfApp1;


namespace WpfApp1
{


	/// <summary>
	/// MainWindow
	/// </summary>
	public partial class MainWindow : System.Windows.Window, System.Windows.Markup.IComponentConnector
	{

		private bool _contentLoaded;

		/// <summary>
		/// InitializeComponent
		/// </summary>
		[System.Diagnostics.DebuggerNonUserCodeAttribute()]
		[System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
		public void InitializeComponent()
		{
			if (_contentLoaded)
			{
				return;
			}
			_contentLoaded = true;
			System.Uri resourceLocater = new System.Uri("/WpfApp1;component/mainwindow.xaml", System.UriKind.Relative);

#line 1 "..\..\MainWindow.xaml"
			System.Windows.Application.LoadComponent(this, resourceLocater);

#line default
#line hidden
		}

		[System.Diagnostics.DebuggerNonUserCodeAttribute()]
		[System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
		[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
		[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Design", "CA1033:InterfaceMethodsShouldBeCallableByChildTypes")]
		[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]
		[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily")]
		void System.Windows.Markup.IComponentConnector.Connect(int connectionId, object target)
		{
			this._contentLoaded = true;
		}

		internal System.Windows.Controls.Grid myGrid;
	}
}
@mikedn

This comment has been minimized.

mikedn commented Dec 6, 2018

C# output for the XAML

Yeah, that's the code I said that it doesn't do a lot. Basically it doesn't do anything, it just calls LoadComponent. Sometimes there's more code in Connect, that sets variables and events. But that's about it, most of the work happens in LoadComponent.

This code is likely generated by presentationbuildtasks.dll so there's no actual API for that, it happens as part of the build process.

@zezba9000

This comment has been minimized.

zezba9000 commented Dec 6, 2018

ic, so just use the API @dotMorten suggested and manually output the reflected type info if I wanted to manually do it.

@mikedn

This comment has been minimized.

mikedn commented Dec 6, 2018

manually output the reflected type info if I wanted to manually do it

If that's what you are trying to do then I guess so. Though I'm not sure I understand what exactly you are trying to do. I'm afraid you're after something like WinForms designer generated code, that would be a ton of work and not very practical.

@zezba9000

This comment has been minimized.

zezba9000 commented Dec 6, 2018

Just trying to better understand WPF's build system APIs when it comes to linking XAML to the code behind. Hope the Presentation libs get released.

@zezba9000 zezba9000 closed this Dec 6, 2018

@lindexi

This comment has been minimized.

Contributor

lindexi commented Dec 6, 2018

@zezba9000 It is faster than using xaml when we write in C# code and removing the xaml file. And too many codes can only write in xaml that can not be changed to C# code.

@rladuca

This comment has been minimized.

Member

rladuca commented Dec 6, 2018

@zezba9000 If I understand correctly, you want to examine the temporary project output of WPF that comes from our markup compilation tasks. All of that lives in PresentationBuildTasks.dll, which isn't quite ready for open sourcing just yet (we're working on it!).

In the meantime, can you try this out in your build and see if this helps you get more insight into the files we generate?

@zezba9000

This comment has been minimized.

zezba9000 commented Dec 6, 2018

Ya I think thats the API I was looking for. Is there a list of libs that haven't been released that are planned to?

@rladuca

This comment has been minimized.

Member

rladuca commented Dec 6, 2018

@zezba9000 I do not have one offhand, but I think that's a great suggestion to add to our overall documentation.

@richlander What do you think about adding a more detailed list of what components are still waiting to be open sourced?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment