Is there a Preprocessed Razor Template for Visual Studio 2015 - visual-studio

I am going thru this article here which talks about using the Razor template for Xamarin forms. I am however unable to find such a template in Visual Studio. The closest I came to was a nuget package. I don't think I can use that if I am to follow the example in the article above. Has anyone found any such problem before or can this only be done on Xamarin studio?
Why would they not make a provision for Visual Studio as well!

Just tried to use a razor template inside a Xamarin.Forms PCL and stumbled over the missing template. But then I followed the advice in the comments and it worked without any problems.
Add a new class (or whatever you want) to the portable project and name the file RazorTest.cshtml.
Replace the content of the new file with a skeleton like this:
Adjust the namespace and bind to your own ViewModel:
#using RazorTestNamespace.ViewModels
#model RazorTestViewModel
<html>
<body>
<h1>#Model.Title</h1>
</body>
</html>
Go to the class properties, find the CustomTool property and type in RazorTemplatePreprocessor. Then VisualStudio magically creates the belonging RazorTest.cs file with the code-behind.
Build the HTML when needed:
Inside the ViewModel:
RazorTest razorTest = new RazorTest { Model = razorTestViewModel };
string html = razorTest.GenerateString();

Related

Blazor components - namespace move

I am experiencing problem in VS 2022 in Blazor complex project. After moving Blazor components to different namespace/folder the components are not functional any more. My component does not 'recognize' other Blazor components anymore. For example custom component MudTable from MudBlazor namespace:
I would like to be able move Blazor components easily as simple classes.
Sometimes it helps to specify the same namespace in razor file to match namespace in partial code behind class, but sometimes I end up creating component (razor and cs files) with different name (copying the same code). This is very time consuming. I am not able to reproduce the described bug in Simple Projects. I have tried to reproduce the issue but it works fine (with minor VS bug) as described below. I will keep trying to reproduce the issue on Sample project.
Please find below scenarios working fine:
Simple scenario working fine
Steps to reproduce the issue:
Create Blazor Server App
Create folders
Components
Components\Sub1
Components\Sub2
Add new Razor component e.g. MyComponent1.razor to folder Components\Sub1
Verify that <MyComponent1\> works on some page. Eg. add lines to Index.razor:
#using BlazorApp1.Components.Sub1
<MyComponent1 />
Move MyComponent1.razor to folder Components\Sub2
Update Index.razor:
#using BlazorApp1.Components.Sub2
<MyComponent1 />
It works fine
More complex scenario with minor bug
Create Blazor Server App
Create folders
Components
Components\Sub1
Components\Sub2
Add new Razor component e.g. MyComponent1.razor to folder Components\Sub1
Add behind the class to 'MyComponent1.razor.cs' to folder Components\Sub1:
using Microsoft.AspNetCore.Components;
namespace BlazorApp1.Components.Sub1
{
public partial class MyComponent1:ComponentBase
{
}
}
Verify that <MyComponent1\> works on some page. Eg. add lines to Index.razor:
#using BlazorApp1.Components.Sub1
<MyComponent1 />
Rename folder Components\Sub1 to Components\Sub1Renamed.
Visual studio does not display any error and project can be compiled/started. Code behind is even displayed 'linked' to razor file:
This seems as a VS bug to me.
When project is started Component1 is invisible on Index page, because component 'BlazorApp1.Components.Sub1.Component1has no visual code defined, because it has only non-visual definition 'behind the code'. The file 'Component1.razor' with visual is not used at all because it is in unused namespaceBlazorApp1.Components.Sub1Renamed`
Update Index.razor:
#using BlazorApp1.Components.Sub1Renamed
<MyComponent1 />
When project is started, Component1 is visible on Index page (but I assume that no code behind is processed)
Do you have any hint, how to make it work for blazor components ?

how to add custom code snippets for Razor pages (cshtml) in VS

Is it possible to add custom code snippets for Razor pages using the Code Snippets Manager in Visual Studio?
I can add snippets for HTML, but I can't find CSHTML anywhere.
I'm using Visual Studio Community 2019 16.4.1
That is because there are no dedicated Cshtml snippets. The Razor code is either C# or VB, so those snippets will be valid in a code block.
To use those snippets you simply have to open a code-block:
Either type # or #( to open an explicit statement
type foreach
type tab twice to activate the snippet
To edit the snippets, press Ctrl+K, Ctrl+B
This worked for me in Visual Studio 2019
Create and save your XML .snippet file. Example template:
<CodeSnippet Format="1.1.0">
<Header>
<Title>NameOfSnippet</Title>
<Author>YourName</Author>
<Shortcut>ShortCutName</Shortcut>
<Description>Description</Description>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>Id</ID>
<ToolTip>Please add an ID</ToolTip>
<Default>REPLACE_ID</Default>
</Literal>
</Declarations>
<Code Language="html"><![CDATA[<div id="$Id$">Example HTML Snippet</div>$end$]]></Code>
</Snippet>
Tools > Code Snippet Manager > Language Dropdown: html > Import
Navigation to your .snippet file and open
Select both My HTML Snippets & HTML
Select Finish and OK
Open a cshtml file and type in your ShortCutName > hit tab
You should see your snippet in the cshtml, since language is html, and not CSharp, no need to open a code block
When I was trying to tackle this problem, the thing that was missing in my mental model was that a .cshtml file consists of multiple parts that are in their own language context. For example, you could have:
a <style></style> block that its contents would be css and VS would recognize CSS snippets in there,
a <script></script> block that has javascript content and VS would recognize javascript snippets
a # block where vs could recognize either CSharp or Basic snippets
If you aren't in one of the other spots, html snippets are recognized.
In my case, I wanted to add a javascript snippet but I thought that the available snippets were tied directly with my file type so I was trying to add them as HTML snippets (and then was disappointed when they didn't show up in my script tag).
ALSO, its important to note that at least with VS 2017, you may need to restart VS to get the editor to recognize any new snippets.

Can I view .html files using Razor Intellisense as if they were .cshtml files in Visual Studio 2010?

I've gotten .Net MVC3 to process .html (and other custom extension) just like a .cshtml file but VS2010 will not highlight the Razor syntax or show Intellisense for it. How do I get VS2010 to recognize .html file as .cshtml?
It's not so easy. If you see asp.net mvc 3 source, you can see in webpages folder next things:
File: RazorDebugHelpers.cs
// Trim the html part of cshtml or vbhtml
string outputExtension = extension.Substring(0, 3);
File: RazorCodeLanguage.cs
private static IDictionary<string, RazorCodeLanguage> _services = new Dictionary<string, RazorCodeLanguage>(StringComparer.OrdinalIgnoreCase) {
{ "cshtml", new CSharpRazorCodeLanguage() },
{ "vbhtml", new VBRazorCodeLanguage() }
};
File: PreApplicationStartCode.cs
WebPageHttpHandler.RegisterExtension("cshtml");
WebPageHttpHandler.RegisterExtension("vbhtml");
And so on.
What i want to say? Extension logic very deep in mvc. If you want to do something like that you prorably need to download sources, edit them and build custom library, but it's very time expensive. Maybe you can ask you question by other way, I hope exist better solution for your problem.

How do I add Jquery VS intellisense reference to my view in VS 2010

I am trying to get Jquery intellisense working in Visual Studio 2010.
I've looked around on StackOverflow and tried adding this to my view:
#{
/// <reference path="/Scripts/jquery-1.5.1-vsdoc.js"/>
}
That's not working.. I've got it working by adding the same script tag to each view but this is less than ideal since i want to keep all my scripts in one place at the bottom of my layout page.
Add the <script> tags to each view inside an #if (false) { ... } block.
The IDE will still see them, and provide IntelliSense, but they won't do anything at runtime.

Does Visual Web Part support ObjectDataSource in designer?

I'm trying to upgrade some Sharepoint 2007 webparts to SP2010 using the web part projects built into Visual Studio 2010. Namely, I'm using Visual Web Part to migrate our existing controls, which make extensive use of ObjectDataSource. However, when adding an ODS to the control in the Visual Web Part project, it will not pick up objects in referenced class library projects. I was able to duplicate the problem from a clean setup as follows:
Create a new Visual Web Part
Add a new class library to the solution.
Class code is as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WebPartODS
{
[System.ComponentModel.DataObject(true)]
public class TestUser
{
[System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select,false)]
public List<int> TestMethod()
{
return new List<int>();
}
}
}
Add the class library project as a reference in the Web Part project
In the VisualWebPart ascx file, add a objectdatasource in the Source view:
<asp:ObjectDataSource ID="TestOD" runat="server"></asp:ObjectDataSource>
Switch to Design view, bring up the "Configure data source" wizard. On the drop down, the class from the library project will not appear.
Is there a step that I am missing here, or is there an issue with trying to do it this way?
Ok, i got it to work. Here is where i got my answer: MSDN Forumn
I originally had a separate class for my business layer. I removed it and put my code in the ascx.cs file. Then I added the following line of code to my page load method.
ObjectDataSource1.TypeName = this.GetType().AssemblyQualifiedName;
I also removed the TypeName from the ascx page.

Resources