Command line equivalent of Visual Studio Add New Page (WPF) - wpf

In the WPF first desktop app walkthrough, it uses Visual Studio to explain how to create a new page in a WPF project. I don't use Visual Studio, but I want to make an XAML Page file using the terminal. I can't just create a new file because it has prewritten code for a new Page similar to below:
<Page x:Class="ExpenseIt.ExpenseItHome"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="350" d:DesignWidth="500"
Title="ExpenseIt - Home">
<Grid>
</Grid>
</Page>
I checked the dotnet new documentation and it says nothing about creating a new file. I know I can just have the default contents in a separate file which I can copy and paste but this seems like an inelegant workaround. Is there a command line argument or another way to get the Page WPF Template without Visual Studio?
Edit
To be more specific on what I am looking for, I would like an XAML file and its corresponding .xaml.cs code-behind file with all the using statements. As suggested, I can use XamlReader, which I have tried, but I would preferably like a tool. XamlPadX is outdated and does not have the code-behind that I am looking for.
Edit 2
This is my current code in the console app I am making:
if (args[0].StartsWith("loadpage-"))
{
string className = args[0].Substring("loadpage-".Length);
string xamlOutput = String.Join(
Environment.NewLine,
$"<Page x:Class=\"{className}\"",
"xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"",
"xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\"",
"xmlns:mc=\"http://schemas.openxmlformats.org/markup-compatibility/2006\"",
"xmlns:d=\"http://schemas.microsoft.com/expression/blend/2008\"",
"mc:Ignorable=\"d\"",
"d:DesignHeight=\"350\" d:DesignWidth=\"500\"",
$"Title=\"\">",
"<Grid>", "", "</Grid>",
"</Page>");
Console.WriteLine(xamlOutput);
}
else
{
Console.WriteLine("You must enter the argument in the following format: ");
Console.Write("loadpage-CLASSNAME");
}

You could write a console application which uses command line arguments to receive the xaml content via pure text, or text file containing the content of the xaml page.
Load it using XamlReader in Main method of your program.cs:
https://learn.microsoft.com/en-us/dotnet/api/system.windows.markup.xamlreader?view=netcore-3.1

Related

windowsformhost cant load a usercontrol from another dll

So I have a dll from another project which contains many useful classes and controls for me (lets call it foo.dll). I'm making an WPF app. I need to use some of them in my app. I created my usercontrol for windows forms and referenced UserControlForMe from foo.dll. It's shown, all good. Now I want to insert my usercontrol into a wpf form. It looks like this:
<UserControl x:Class="FlatRectangular_Profile.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
xmlns:uc="clr-namespace:FlatRectangular_Profile.UC"
Height="2093" Width="717">
<Grid Name="grid">
<WindowsFormsHost>
<uc:WindowsFormsProfManual ></uc:WindowsFormsProfManual>
</WindowsFormsHost>
</Grid>
</UserControl>
But here I get an error "cant load type UserControlForMe from foo.dll". No info on that error. Again, UserControlForMe loads in WindowsFormsProfManual. All these is going on in one class library. I referenced everything that foo.dll needed.
No idea how what to do next. I also tried to load it in code in usercontrol.loaded event, but it fails too, and shows stacktrace which leads to the constructor of the UserControlForMe.
I guess you'll have to add the assembly to your namespace import to point your application in the right direction:
xmlns:uc="clr-namespace:FlatRectangular_Profile.UC;Assembly=MyDLL"
I found a workaround since I cant get why it is not working. If I load a UserControlForMe from foo.dll directly to the windowsformhost, it works. But if there is a "buffer" dll, it works in this dll, but doesnt open in futher window. Also I add a UserControlForMe programmatically to a windowsformhost.

View in a module not displaying image but it will display text

I am using MEF, Prism and WPF. I have a simple module (for now, I'll do more with it when I get past this roadblock) that displays a single image sourced as static resource in that module. Here is the xaml for the view:
<UserControl x:Class="SplashScreenModule.SplashView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
Height="960" Width="1180">
<Grid>
<Label>
<Image Source="Images/My Image.png"/>
</Label>
</Grid>
The image shows up in the designer but when I run the code the window is blank. If I replace the image with simple text the text is display as expected.
<Label>
This shows up in the window.
</Label>
I've spent the last 3 hours trying to figure this out and am stumped. I would appreciate any help.
Thanks,
Bill
You have to know that there is a difference on how ressources are managed at DesignTime and at RunTime. At DesignTime there is no compilation required, thus it is able to display a ressource even if it is not include in the project.
At runtime this is normally possible, but it is better to include the ressources in your project (ie create a folder Images in your solution, and add the "My Image.png" in this folder), to be sure of the location of your ressource compared to your exe.
If you do it and the image still doesn't show up, try to remove the label and execute again.
By doing this it works fine for me. As soon as the image show up, you can add back the label.
Hope it helps.
The following code is working for me:
<Window x:Class="LabelImage.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Label>
<Image Source="Images/visual studio.png" />
</Label>
</Grid>
</Window>
I structured my solution like you did: I have the picture inside a folder named Images. The MainWindow is located directly under the project node (it's not in any subdirectory).
Make sure that the Build Action for your image is set to resource, otherwise it won't work with the WPF Resource Finding System (which internally uses PACK-Uris: http://msdn.microsoft.com/en-us/library/aa970069(v=vs.110).aspx)
I use .NET 4.5 with VS 2013. If you have any further questions, please feel free to ask.
Update after Bill's comment:
That's a whole different thing when your resource resides within another assembly. The syntax for referencing the image should be like this:
<Image Source="/ClassLibraryForResources;component/Images/visual studio.png" />
where you do the following steps:
Begin with a slash "/"
Specify the name of the assembly that holds your resource (this can also be a strong assembly name)
Continue with ";component/"
End with the path of the image (or other resource)
(Please note that you have to omit the quotation marks).
The solution that I used is structured like this:
This should resemble what you have described. Again: it's all about the PACK URIs that WPF uses (see link above).

Visual Studio can't resolve static resource in WPF window, even though it works at run time - why?

I have a WPF window and am using the MVVM pattern. I set the view model as a resource for the window as follows...
<Window
...other stuff removed for clarity...
xmlns:mvvm="clr-namespace:VisionRT.CRM.WPF.ViewModels.VRTSystems"
>
<Window.Resources>
<mvvm:DhrTemplatesViewModel x:Key="viewmodel" />
</Window.Resources>
I want to set the window's data context to use the resource, and found that the following XAML works...
<Window.DataContext>
<StaticResource ResourceKey="viewmodel"/>
<Window.DataContext>
The problem is that I can only do this by typing the XAML manually, Visual Studio doesn't show the resource anywhere. I can go to the window's properties and click the little icon next to the DataContext property, click the "Apply resource" option, but it doesn't show "viewmodel" as a resource, static or dynamic. If I enter the XAML manually and then open the "Apply resource" pop-up window, it has the "viewmodel" underlined as an error, and hovering over it shows a tooltip "cannot resolve resource reference"
However, when I run the application, it works fine, so the resource is being resolved at run time.
Anyone able to explain this? I would really like to be able to do this through the VS property editor, as I find that more convenient than typing the XAMl by hand. I'm also bothered by the fact that VS can't resolve it. This makes me think I'm doing something wrong.
Thanks for any explanation you can give.
The only (sad) explanation is that XAML is a second grade citizen in Visual Studio. Once you start pushing XAML a little more than basic and you end up with "unresolved" "cant display" "sorry I'm dumb", etc.
Refer to this WPF suggestion: http://dotnet.uservoice.com/forums/40583-wpf-feature-suggestions/suggestions/480899-make-xaml-a-first-class-citizen-of-visual-studio?ref=title to get them fixed.
It is possible to make Intellisense work 100% for a XAML {StaticResource} in Visual Studio.
Tested On
WPF/C#
Visual Studio 2015 Update 3
Step 1: Shift resources into shared project
The key is to shift all resources referenced by ResourceDictionary into a shared project:
Step 2: Design time intellisense
We're not quite there yet. The XAML runtime throws runtime if you include resources twice.
If we are including an .xaml file more than once in the entire project, we can include it at design time, but not at runtime:
public class DesignTimeResourceDictionary : ResourceDictionary
{
private Uri source;
public new Uri Source
{
get
{
if ((bool)DesignerProperties.IsInDesignModeProperty.GetMetadata(typeof(DependencyObject)).DefaultValue)
{
return null;
}
return this.source;
}
set { this.source = value; }
}
}
Then we can add design time resources:
<!--Design time resource dictionary, so intellisense will work with with {StaticResource}.-->
<ResourceDictionary.MergedDictionaries>
<dr:DesignTimeResourceDictionary Source="/Project.Name;component/Folder/SharedResourceA.xaml" />
<dr:DesignTimeResourceDictionary Source="/Project.Name;component/Folder/SharedResourceB.xaml" />
</ResourceDictionary.MergedDictionaries>
If we are using ReSharper, it will offer to automatically add the namespace prefix into the header:
xmlns:dr="clr-namespace:MyNamespace;assembly=Project.Name"
Appendix A: Extra for Experts: Why the need for Step 1?
As an aside, Intellisense in XAML is the same as Intellisense in C#: it only works with sub-projects that are referenced. This is why if projects A and B want to share some code, you must put it in a class library C that is referenced by projects A and B. This is in contrast to C or C++ where #including a file in the root makes it available to all subfiles.
With XAML, you can cheat and add static resources into Main Application Project (above), and the XAML runtime will still work properly - but Intellisense won't work at design time, presumably as its based on the same engine that is used for C# intellisense.
To make Intellisense work all the time in XAML, Visual Studio Intellisense would have to scan up through every parent project (instead of just down towards referenced projects).

xaml viewer replacement for vs 2008

I want to be able to edit xaml in vs 2008 and be able to view the results without going crazy waiting for the wpf designer to draw the xaml.
I downloaded xamlpadx (v. 4) and kaxaml. They both look great, but it seems that they can only edit xaml without any custom namespaces. This makes it impossible for me because I'm using mvvm and am importing several namespaces in my xaml.
When I upload a typical xaml doc in kaxaml I get
like this:
<UserControl x:Class="SkipPro.View.ContactView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:m="clr-namespace:MVVMLibrary;assembly=MVVMLibrary"
xmlns:tk="http://schemas.microsoft.com/wpf/2008/toolkit"
Height="Auto" Width="Auto">
<UserControl.Resources>
<m:NotConverter x:Key="NotConverter"/>
<m:VisibilityConverter x:Key="VisibilityConverter"/>
</UserControl.Resources>
'Class' attribute does not exist in xaml namespace...
What are my choices?
Expression Blend, I believe they have a "Free" or, at least, "Trial" version you can dl.
Well, you have two options within Visual Studio 2008:
Go to: Tools -> Options -> Text Editor -> XAML -> Miscellaneous and check Always open documents in full XAML view under Default View group. You can still switch to design view tab within editor.
Open XAML documents with XML editor by default. Right click on a XAML document in Solution Explorer and click on Open With... in context menu. Select XML Editor or XML Editor with Encoding and click Set as Default button. From now on, XAML will be loaded without the WPF designer.
Edit: Here are few links with the tricks above:
http://karlshifflett.wordpress.com/2008/02/04/visual-studio-2008-tip-open-xaml-files-faster/
http://weblogs.asp.net/fmarguerie/archive/2009/01/29/life-changer-xaml-tip-for-visual-studio.aspx

WPF: XAML Custom Namespace

Okay so I have a Window in WPF. I add the following line inside of it:
xmlns:controls="clr-namespace:mCubed.Controls"
This compiles and runs just fine, but the Visual Studio designer gives me this error:
Could not load file or assembly 'mCubed, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
When I remove this line from the Window, it compiles and runs just fine and the Visual Studio designer works like a charm!
I'm confused as to why that one line breaks the designer? This occurs REGARDLESS if I have put the following line in the body of the XAML document.
<controls:MyControl/>
All my .cs files are in the same VS project. I have a mCubed namespace which contains my cleverly named mCubedWindow class. I have all my controls classes defined in the mCubed.Controls namespace. Do NOT tell me this is an assembly problem, ALL MY FILES ARE IN THE SAME VS PROJECT!
Not an assembly problem, just a designer problem. The VS WPF designer in 2008 is primitive at best - completely useless IMHO. I turn it off completely and use the XML editor instead. Hopefully things will improve drastically in 2010.
Is MyControl in the same assembly as the window? If it isn't, you need to include the assembly name in the declaration:
xmlns:controls="clr-namespace:mCubed.Controls;assembly=mCubed"
That's a bit weird. I've developed several projects that do exactly that. Here's a quick dummy project, all in one .exe:
First, a UserControl with a couple of buttons:
<UserControl x:Class="WpfApplication1.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid Width="30">
<Button HorizontalAlignment="Left">A</Button>
<Button HorizontalAlignment="Right">B</Button>
</Grid>
</UserControl>
Now the main window, with my control added to it:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:p="clr-namespace:WpfApplication1"
Title="Window1" Height="300" Width="300">
<Grid>
<p:UserControl1/>
</Grid>
</Window>
No error messages anywhere.
Is the XAML loose (Build action: None, No code behind) or compiled (Build action: Page, Can have code behind)?
If the XAML is loose or if MyControl is in a different assembly you must specify which assembly MyControl is in, like Daniel Pratt stated:
xmlns:controls="clr-namespace:mCubed.Controls;assembly=mCubed"
Make sure the assembly mCubed and its dependencies (references) are copied to your output directory. If they are not, then add mCubed as a reference to the start-up project.

Resources