oleg-shilo / cs-script

C# scripting platform
http://www.cs-script.net
MIT License
1.63k stars 236 forks source link

[Question] Loading XAML UserControl #391

Open td3466 opened 4 days ago

td3466 commented 4 days ago

I'm not having much luck loading a UserControl for WPF, and wondering if you could advise what I might be doing wrong. I have been using this as a reference https://www.cs-script.net/cs-script/help-legacy/wpf.html.

I'm trying to load the control via LoadFile: UserControl c = CSScript.Evaluator.LoadFile("ScriptView.xaml.cs");

This is my UserControl XAML:

<UserControl x:Class="ScriptView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid>
        <TextBlock Text="View loaded from script" />
    </Grid>
</UserControl>

This is the Code behind: //css_winapp; //css_ref PresentationCore; //css_ref PresentationFramework; //css_inc ScriptView.xaml; using System; using System.Windows.Controls; public partial class ScriptView : UserControl { public ScriptView() { InitializeComponent(); } }

If I try to load without including the XAML, it loads the class, just without any UI component to it. When I try to load with the XAML it I get the following error: C:\Users\cclasmgr\Source\Repos\WpfApp2\Views\ScriptView.xaml.cs(18,2): error CS1525: Invalid expression term '<' C:\Users\cclasmgr\Source\Repos\WpfApp2\Views\ScriptView.xaml(2,14): error CS1002: ; expected C:\Users\cclasmgr\Source\Repos\WpfApp2\Views\ScriptView.xaml(2,34): error CS1002: ; expected C:\Users\cclasmgr\Source\Repos\WpfApp2\Views\ScriptView.xaml(3,79): error CS1002: ; expected C:\Users\cclasmgr\Source\Repos\WpfApp2\Views\ScriptView.xaml(4,69): error CS1525: Invalid expression term '<' C:\Users\cclasmgr\Source\Repos\WpfApp2\Views\ScriptView.xaml(5,14): error CS1002: ; expected C:\Users\cclasmgr\Source\Repos\WpfApp2\Views\ScriptView.xaml(5,14): error CS7017: Member definition, statement, or end-of-file expected C:\Users\cclasmgr\Source\Repos\WpfApp2\Views\ScriptView.xaml(5,79): error CS1002: ; expected C:\Users\cclasmgr\Source\Repos\WpfApp2\Views\ScriptView.xaml(6,74): error CS1002: ; expected C:\Users\cclasmgr\Source\Repos\WpfApp2\Views\ScriptView.xaml(7,30): error CS1002: ; expected C:\Users\cclasmgr\Source\Repos\WpfApp2\Views\ScriptView.xaml(8,35): error CS1002: ; expected C:\Users\cclasmgr\Source\Repos\WpfApp2\Views\ScriptView.xaml(8,57): error CS1525: Invalid expression term '>' C:\Users\cclasmgr\Source\Repos\WpfApp2\Views\ScriptView.xaml(8,58): error CS1525: Invalid expression term '<' C:\Users\cclasmgr\Source\Repos\WpfApp2\Views\ScriptView.xaml(9,11): error CS1525: Invalid expression term '<' C:\Users\cclasmgr\Source\Repos\WpfApp2\Views\ScriptView.xaml(10,20): error CS1002: ; expected C:\Users\cclasmgr\Source\Repos\WpfApp2\Views\ScriptView.xaml(10,52): error CS1525: Invalid expression term '>' C:\Users\cclasmgr\Source\Repos\WpfApp2\Views\ScriptView.xaml(10,53): error CS1525: Invalid expression term '<' C:\Users\cclasmgr\Source\Repos\WpfApp2\Views\ScriptView.xaml(11,6): error CS1525: Invalid expression term '/' C:\Users\cclasmgr\Source\Repos\WpfApp2\Views\ScriptView.xaml(11,12): error CS1525: Invalid expression term '<' C:\Users\cclasmgr\Source\Repos\WpfApp2\Views\ScriptView.xaml(12,2): error CS1525: Invalid expression term '/' C:\Users\cclasmgr\Source\Repos\WpfApp2\Views\ScriptView.xaml(12,15): error CS1733: Expected expression C:\Users\cclasmgr\Source\Repos\WpfApp2\Views\ScriptView.xaml(2,2): error CS0119: 'UserControl' is a type, which is not valid in the given context C:\Users\cclasmgr\Source\Repos\WpfApp2\Views\ScriptView.xaml(2,16): error CS0103: The name 'Class' does not exist in the current context C:\Users\cclasmgr\Source\Repos\WpfApp2\Views\ScriptView.xaml(3,14): error CS0103: The name 'xmlns' does not exist in the current context C:\Users\cclasmgr\Source\Repos\WpfApp2\Views\ScriptView.xaml(4,20): error CS0103: The name 'x' does not exist in the current context C:\Users\cclasmgr\Source\Repos\WpfApp2\Views\ScriptView.xaml(5,9): error CS0103: The name 'xmlns' does not exist in the current context C:\Users\cclasmgr\Source\Repos\WpfApp2\Views\ScriptView.xaml(5,15): error CS0103: The name 'mc' does not exist in the current context C:\Users\cclasmgr\Source\Repos\WpfApp2\Views\ScriptView.xaml(6,14): error CS0140: The label 'xmlns' is a duplicate C:\Users\cclasmgr\Source\Repos\WpfApp2\Views\ScriptView.xaml(6,20): error CS0103: The name 'd' does not exist in the current context C:\Users\cclasmgr\Source\Repos\WpfApp2\Views\ScriptView.xaml(7,17): error CS0103: The name 'Ignorable' does not exist in the current context C:\Users\cclasmgr\Source\Repos\WpfApp2\Views\ScriptView.xaml(8,16): error CS0103: The name 'DesignHeight' does not exist in the current context C:\Users\cclasmgr\Source\Repos\WpfApp2\Views\ScriptView.xaml(8,35): error CS0140: The label 'd' is a duplicate C:\Users\cclasmgr\Source\Repos\WpfApp2\Views\ScriptView.xaml(8,37): error CS0103: The name 'DesignWidth' does not exist in the current context C:\Users\cclasmgr\Source\Repos\WpfApp2\Views\ScriptView.xaml(9,6): error CS0119: 'Grid' is a type, which is not valid in the given context C:\Users\cclasmgr\Source\Repos\WpfApp2\Views\ScriptView.xaml(10,10): error CS0119: 'TextBlock' is a type, which is not valid in the given context C:\Users\cclasmgr\Source\Repos\WpfApp2\Views\ScriptView.xaml(10,20): error CS0103: The name 'Text' does not exist in the current context C:\Users\cclasmgr\Source\Repos\WpfApp2\Views\ScriptView.xaml(11,7): error CS0119: 'Grid' is a type, which is not valid in the given context C:\Users\cclasmgr\Source\Repos\WpfApp2\Views\ScriptView.xaml(12,3): error CS0119: 'UserControl' is a type, which is not valid in the given context C:\Users\cclasmgr\Source\Repos\WpfApp2\Views\ScriptView.xaml.cs(16,9): error CS0103: The name 'InitializeComponent' does not exist in the current context

oleg-shilo commented 4 days ago

Unfortunately what you are trying to do is problematic. That was a short version.

The longer one is more interesting :)

Before XAML based application can be executed a few things need to happen:

  1. *.cs file needs to be compiled into an assembly
  2. .xaml needs to be compiled into .balm
  3. the executable needs to be assembled with *.balm embedded as a resource

Out of all .NET compilation options available today only dotnet.exe is capable of compiling xaml. The rest (Roslyn and csc.exe) can only compile C#.

CS-Script is able to engage dotnet.exe compiling engine from the CLI launcher (e.g. csws.exe):

# create the sample WPF script
cscs -new:wpf hello_wpf.cs

# execute the sample WPF script
csws hello_wpf.cs

However, in your case, you are using CS-Script engine as an assembly hosted in your exe. And in the hosted scenarios like yours, only Roslyn and csc.exe are available. Thus your XAML is never compiled. That's why you have that runtime error.

This means that if you are indeed keen to load your WPF control you have only two options:

td3466 commented 4 days ago

Thank you kindly for the response and explanation. Most helpful.