Create keyboard shortcut to regionate method - visual-studio

Can I in Visual Studio create keyboard shortcut to regionate method and auto-document it with GhostDoc?
From this:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
}
I want make this:
#region protected override void OnInit(EventArgs e)
/// <summary>
/// Raises the <see cref="E:Init"/> event.
/// </summary>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
}
#endregion

I am not sure about the GhostDoc auto documentation, but to regionate selected text you can use the "surround with" function in Visual Studio.
Select a portion of code and press Ctrl + K, S. This brings up the "surround with" context menu. Select "region" in the menu, type in your region name and you're all set.
This function can be used for a bunch of other stuff as well. If, for, while and try statements and so on.

Type three slashes to auto-generate the documentation

Related

VC++ Intellisense inherited members

I have the following scenario in my code:
class A {
/// <summary>
/// returns the total number of frames that the consumer can accept
/// </summary>
/// <returns></returns>
virtual int GetRenderBufferSizeInFrames() = 0;
}
class B : public A {
int GetRenderBufferSizeInFrames();
}
void F() {
B BInstance;
BInstance.GetRenderBufferSizeInFrames(); // VC++ does not show the information about
// the method
}
When I hover the mouse over the function call, VC++ does not show me the info on the function. Is there a way to fix that? Logically it should be doing that, no?
No, you need to be explicit about that.
Try adding <inheritdoc/> to the derived method.
class B : public A {
/// <inheritdoc />
int GetRenderBufferSizeInFrames();
}
As a side note, I suggest to use an override specifier, to ensure you don't have a typo in the method name and make it visibly clear that this is overriding a virtual method from the base class.

Why do I see this error when trying to open the control designer in visual studio and how do I fix it?

Why do I see this error when trying to open the control designer in visual studio 2019 and how do I fix it?
(Winforms)
The error switches sometimes when I try to open the designer:
I tried reinstalling Visual Studio but I am still getting this.
CustomContol1.cs
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace DBControls
{
public partial class CustomControl1 : Control
{
public CustomControl1()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
}
}
}
CustomContol1.Designer.cs
using System.Windows.Forms;
namespace DBControls
{
public partial class CustomControl1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion
}
}
you do not have a usercontrol, but you have inherited from Control.
This means there is nothing to design, and therefor the designer crashes
If you wish to build a usercontrol that you can edit in the designer, than you need to inherit from UserControl instead.
I am not sure if you can just edit this file and get it working, normally you have to use create new project and then choose customcontrol to do this.
here is an example on how to choose a project for this
this will create a dll that you can use in other project, in those projects you will have to click right mouse in the Toolbox and then choose items.
In the next screen you need to browse to the dll you created here and select it.
From then on all controls in that dll will be available in that project to drag onto forms

ASP.NET MVC 3.0 Razor, load View from anywhere out of the box?

Is it true that it is possible to load View from anywhere without
implementation of custom VirtualPathProvider in MVC 3.0?
If it is true how to do it?
Basically it is not a problem to implement custom VirtualPathProvider which would load the
View from anywhere but my implementation working only in MVC 2.0 and not working wih MVC 3.0, fore some reason method GetFile newer called for not existed views in MVC 3.0 and in that case I am getting "Server Error in '/' Application."
I followed the same code for my custom VirtualPathProvider from here: http://buildstarted.com/2010/09/28/mvc-3-razor-view-engine-without-a-controller/
UPDATE 1
OK i did fix my problem with my custom VirtualPathProvider after i put registration of my custom VirtualPathProvider provider first line in the Application_Start()
protected void Application_Start()
{
//Should be first line before routes and areas registration.
HostingEnvironment.RegisterVirtualPathProvider(new MyVirtualPathProvider());
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
When registration of custom VirtualPathProvider in Global.asax.cs folowing after AreaRegistration.RegisterAllAreas(); or RegisterRoutes(RouteTable.Routes); method method override VirtualFile GetFile(string virtualPath) wont work for "virtual Views".
UPDATE 2
does it means that the classes RazorView and RazorViewEngineRender is the answer?
UPDATE 3
If i have string representation of my razor view which does not exists in the file system (e.g i store razor views in database) how may i render it using this kind of approach http://buildstarted.com/2010/09/28/mvc-3-razor-view-engine-without-a-controller/
For example string representation of my View looks like this:
"#{
ViewBag.Title = ""About Us"";
}
<h2>About</h2>
<p>
Put content here.
</p>"
UPDATE 4
Now i see, to be able to use #Html.<something> custom TemplateBase should be implemented.
The sample of implementation of HtmlTemplateBase<T> could be fount here http://www.fidelitydesign.net/?p=239, but it won't work with RazorEngine v2, i am successfully getting template compiled, then after assembly loaded method public override void Execute() won't be executed i am getting an error: The method or operation is not implemented (stacktrace: http://tinypic.com/r/dcow4/7)
To make “public override T Model” happened i did change declaration of “public TModel Model” to “public virtual TModel Model” in “public abstract class TemplateBase : TemplateBase, ITemplate”. May be there is some another changes should be done? Or something in HtmlTemplateBase<T> should be done another way?
Don't be confused by Ben's (#BuildStarted) sample code in his article. He is detailing how to use an early version of the Razor ViewEngine to render templates without using a controller action. The intention was to be able to render templates in a generic fashion, rather than as specific page views. (This is what has evolved into our RazorEngine templating framework # http://razorengine.codeplex.com).
The VirtualPathProvider is still a core part of ASP.NET. There appears to be a general confusion about MVC 3's DependencyResolver being a replacement of a VirtualPathProvider but this is not the case, you still require a provider to be able to access content on a virtual path (which incidentally, all paths in ASP.NET are virtual).
Reverting my original answer, you should be able to achieve what you want purely through subclassing the RazorViewEngine and using that to create your views.
Have a look at this topic: http://coderjournal.com/2009/05/creating-your-first-mvc-viewengine/
No, loading a view from the database is not supported by default. You need to write your own VirtualPathProvider.
Note that Ben's blog post does not actually address directly the problem that you are trying to solve. The following blog post looks a lot closer to what you want: http://rebuildall.umbraworks.net/2009/11/17/ASP_NET_MVC_and_virtual_views. Note that it does not matter if you are trying to store razor or aspx views in the database. Virtual path providers in Asp.Net are simply about mapping a path to a stream of bytes that are the contents of the file represented by that path.
I ran into a similar issue implementing a VirtualPathProvider for embedded resource views. The solution was to implement GetFolder as well as GetFile. The view engine doesn't just call GetFile when you request that view. On the first request it looks through the views folder to find all available views. If that call doesn't include your database views in the list, they won't be found when you try to load them.
Everyone is correct. My post was not how to load Razor as a replacement but as a way to call razor without using MVC. Now...what you want is most likely related to my post here How to Download Razor View Engine Where I show how to create your own ViewEngine to host a razor page. It uses the same engine #Matthew Abbott and I use for the RazorEngine - which you can get from CodePlex. Unfortunately it's not complete but it should give you an idea on how to do it. (I'll post it here too)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Hosting;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml.Linq;
namespace RazorViewEngine {
/// <summary>
/// ViewEngine for the RazorView. Provides basic file handling to load views.
/// </summary>
public class RazorViewEngine : IViewEngine {
string[] SearchLocations { get; set; }
Tuple<string, string, RazorView> Cache { get; set; }
VirtualPathProvider VirtualPathProvider { get; set; }
public RazorViewEngine() {
//{1} == Controller name
//{0} == View name
SearchLocations = new string[] {
"~/Views/{1}/{0}.cshtml",
"~/Views/Shared/{0}.cshtml",
};
VirtualPathProvider = HostingEnvironment.VirtualPathProvider;
}
#region IViewEngine Members
public ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache) {
return CreateView(controllerContext, partialViewName, null, null, useCache);
}
public ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache) {
return CreateView(controllerContext, viewName, masterName, GetLayoutPath(controllerContext), useCache);
}
/// <summary>
/// Meat of the FindView methods.
/// </summary>
/// <param name="controllerContext">The current controller context for this request.</param>
/// <param name="viewName">The requested view name. </param>
/// <param name="masterName">The master page view name (currently unused)</param>
/// <param name="layoutPath">The layout path location (Replaces the masterpage in other view engines)</param>
/// <param name="useCache">Cache the viewpage?</param>
/// <remarks>The layout path is currently hardcoded to "Layout" and will look in the SearchLocations for that path</remarks>
/// <returns>Returns a ViewEngineResult with the requested view</returns>
public ViewEngineResult CreateView(ControllerContext controllerContext, string viewName, string masterName, string layoutPath, bool useCache) {
//grab the current controller from the route data
string controllerName = controllerContext.RouteData.GetRequiredString("controller");
//for proper error handling we need to return a list of locations we attempted to search for the view
string[] SearchedLocations;
//get the actual path of the view - returns null if none is found
string viewPath = GetViewPath(viewName, controllerName, out SearchedLocations);
if (viewPath != null) {
RazorView view = new RazorView(this, controllerContext, viewPath, layoutPath);
return new ViewEngineResult(view, this);
}
//we couldn't find the view - return an array of all locations we've looked in
return new ViewEngineResult(SearchedLocations);
}
/// <summary>
/// Look for the view in the current file system
/// </summary>
/// <param name="viewName">The name of the View you're looking for</param>
/// <param name="controllerName">Current controller name</param>
/// <param name="SearchedLocations">out a list of locations searched</param>
/// <returns>A string value of the relative path</returns>
public string GetViewPath(string viewName, string controllerName, out string[] SearchedLocations) {
return FindPath(viewName, controllerName, out SearchedLocations);
}
/// <summary>
/// Look for the view in the current file system
/// </summary>
/// <param name="viewName">The name of the View you're looking for</param>
/// <param name="controllerName">Current controller name</param>
/// <param name="SearchedLocations">out a list of locations searched</param>
/// <returns>A string value of the relative path</returns>
public string FindPath(string viewName, string controllerName, out string[] SearchedLocations) {
SearchedLocations = new string[SearchLocations.Length];
for (int i = 0; i < SearchLocations.Length; i++) {
string virtualPath = string.Format(SearchLocations[i], viewName, controllerName);
SearchedLocations[i] = virtualPath;
//check the active VirtualPathProvider if the file exists
if (VirtualPathProvider.FileExists(virtualPath)) {
//add it to cache - not currently implemented
return VirtualPathProvider.GetFile(virtualPath).VirtualPath;
}
}
return null;
}
/// <summary>
/// Get the layout virtual path
/// </summary>
/// <param name="controllerContext">The current Controller context for this request</param>
/// <returns>A string virtual path</returns>
public string GetLayoutPath(ControllerContext controllerContext) {
//This should probably be added to a list of locations - I'm not sure exactly
//what I need to do with this yet.
string[] locations;
return FindPath("Layout", controllerContext.RouteData.GetRequiredString("controller"), out locations);
}
/// <summary>
/// Current irrelevant
/// </summary>
/// <param name="controllerContext">The active controller context</param>
/// <param name="view">View to release</param>
public void ReleaseView(ControllerContext controllerContext, IView view) {
IDisposable disposable = view as IDisposable;
if (disposable != null) {
disposable.Dispose();
}
}
#endregion
}
/// <summary>
/// Implements IView and renders a Razor
/// </summary>
public class RazorView : IView {
ControllerContext ControllerContext;
string ViewPath;
string LayoutPath;
RazorViewEngine Engine;
public RazorView(RazorViewEngine engine, ControllerContext controllerContext, string viewPath, string layoutPath) {
//load the file
this.ControllerContext = controllerContext;
this.ViewPath = viewPath;
this.LayoutPath = layoutPath;
this.Engine = engine;
}
#region IView Members
/// <summary>
/// Converts Razor to html and writes it to the passed in writer
/// </summary>
/// <param name="viewContext"></param>
/// <param name="writer"></param>
public void Render(ViewContext viewContext, System.IO.TextWriter writer) {
//View contents
string contents = new StreamReader(VirtualPathProvider.OpenFile(ViewPath)).ReadToEnd();
string layoutContents = LayoutPath == null
? null
: new StreamReader(VirtualPathProvider.OpenFile(LayoutPath)).ReadToEnd();
contents = Parse(contents);
string output;
output = contents;
writer.Write(output);
}
/// <summary>
/// Converts Razor to html
/// </summary>
/// <param name="Razor">Razor text</param>
/// <returns>Html formatted Razor text</returns>
string Parse(string Razor) {
//Where do I get the model From
return RazorEngine.Razor.Parse(Razor);
}
#endregion
}
}

Handling the OnNavigatedFrom / OnNavigatedTo events in the ViewModel

I am trying to figure out a way for my ViewModel to handle saving or restore the page's state when the page is navigated From or To.
The first thing I tried was to add an EventToCommand behavior to the page, but the events (OnNavigatedFrom and OnNavigatedTo) are declared protected and the EventToCommand does not see the events to bind to.
Next I thought I would try using the Messenger class to pass a message to the ViewModel using code in the View's code behind:
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
Messenger.Default.Send<PhoneApplicationPage>(this);
base.OnNavigatedFrom(e);
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
Messenger.Default.Send<PhoneApplicationPage>(this);
base.OnNavigatedTo(e);
}
But this seems to have two issues, first is having this code in the code behind page. Second, the ViewModel cannot tell the difference between the OnNavigatedFrom and the OnNavigatedTo events without having to create a set a wrapper classes for the PhoneApplicationPage object (see UPDATE below).
What is the most MVVM-Light friendly way to handle these events?
UPDATE:
I was able to resolve the second issue by Sending the Messages like this:
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
Messenger.Default.Send<PhoneApplicationPage>(this,"NavigatedFrom");
base.OnNavigatedFrom(e);
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
Messenger.Default.Send<PhoneApplicationPage>(this, "NavigatedTo");
base.OnNavigatedTo(e);
}
and Registering them like this:
Messenger.Default.Register<PhoneApplicationPage>(this, "NavigatedFrom", false, (action) => SaveState(action));
Messenger.Default.Register<PhoneApplicationPage>(this, "NavigatedTo", false, (action) => RestoreState(action));
Executing a command from code behind is far cleaner than going through the whole messaging mess. After all there's nothing wrong with the view knowing about its DataContext.
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
viewModel.NavigatedToCommand.Execute(e.Uri);
}
ProfileViewModel viewModel
{
get
{
return this.DataContext as ProfileViewModel;
}
}
Update: Passing in NavigationContext.QueryString is probably more useful, since it already parses out the parameters and value.
Sorry for being three years late to this question. Yes, I'm still using Silverlight. Okay I want to write it in Page code-behind like this:
// Executes when the user navigates to this page.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
this.HandleOnNavigatedTo(e);
}
I am using an extension method like this:
public static void HandleOnNavigatedTo(this Page page, NavigationEventArgs e)
{
var vm = page.DataContext as IPageNavigationViewModel;
if (vm == null) return;
vm.HandleOnNavigatedTo(e);
}
The extension method implies that the Page must have a View Model that implements IPageNavigationViewModel in DataContext. For me, this is a separation-of-concerns compromise where the Page knows only about the most general-purpose data types in the Domain. This the interface:
using System.Windows.Navigation;
namespace Fox.Silverlight.ViewModels
{
/// <summary>
/// Defines View Model members for frame-navigation pages.
/// </summary>
public interface IPageNavigationViewModel
{
/// <summary>
/// Handles the <see cref="Page.OnNavigatedTo"/> method in the View Model.
/// </summary>
/// <param name="e">The <see cref="NavigationEventArgs"/> instance containing the event data.</param>
void HandleOnNavigatedTo(NavigationEventArgs e);
/// <summary>
/// Handles the <see cref="Page.OnNavigatedFrom"/> method in the View Model.
/// </summary>
/// <param name="e">The <see cref="NavigationEventArgs"/> instance containing the event data.</param>
void HandleOnNavigatedFrom(NavigationEventArgs e);
}
}
Looks like you have a solution to your problem already. I would also suggest the following:
Look at using one of the message values provided in the mvvm-toolkit, such as:
NotificationMessage<T>
Like this:
Messenger.Default.Send<NotificationMessage<PhoneApplicationPage>>(
new NotificationMessage<PhoneApplicationPage>(this, "Message"));
I think what Ryan was getting at, was the fact that you're using the PhoneApplicationPage as the message that is being sent, instead of an actual message.
You're doing this:
Messenger.Default.Send<PhoneApplicationPage>(this);
which is sending a message of type PhoneApplicationPage. You probably don't need to send the entire PhoneApplicationPage as the message.
You could make some messages for NavigatingTo / NavigatingFrom, ie.
Messenger.Default.Send<NavigatingToMessage>(new NavigatingToMessage());
etc.
I'm sure there are a million better ways to do this, I was just going along with how you had set things up. Personally, my ViewModelBase class has NavigatingTo/NavigatingFrom methods and I override the respective methods in the View and send them to my ViewModel.
I make a sample using the updated answer inside the question :
MainViewModel.xaml.cs :
public class MainViewModel : ViewModelBase
{
public MainViewModel()
{
Messenger.Default.Register<PhoneApplicationPage>(this, "NavigatedTo", false, ExecuteNavigatedTo);
}
// action contains everything you want.
private void ExecuteNavigatedTo(Page page)
{
// example
bool b = page.NavigationContext.QueryString.ContainsKey("id");
}
}
MainViewModel.xaml.cs :
protected override void OnNavigatedTo(NavigationEventArgs e)
{
Messenger.Default.Send<PhoneApplicationPage>(this, "NavigatedTo");
base.OnNavigatedTo(e);
}

Automatically adding .Net code comments

Where can I find a Visual Studio plug-in that automatically generates documentation header for methods and properties?
Example the comment to a property could look like this:
/// <summary>
/// Gets or sets the value of message
/// </summary>
public static string Message
{
get
{
return message;
}
set
{
message = value;
}
}
Ghostdoc from http://www.roland-weigelt.de/ghostdoc/
GhostDoc is the usual suspect.
As another poster mentioned, Visual Studio also does this to an extent by entering 3 '///' (forward slashes) on the line preceding a property/method/class definition.
Visual Studio does this automatically. Just position the cursor directly above the method and enter three '/'s
for example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcWidgets.Models
{
/// <summary>
/// This is a summary comment
/// </summary>
public class Comment
{
/// <summary>
///
/// </summary>
/// <param name="name"></param>
/// <param name="birthdate"></param>
/// <param name="website"></param>
/// <returns></returns>
public int SomeMethod(string name, DateTime birthdate, Uri website)
{
return 0;
}
}
}
You can then generate an XML comment file and then generate a Help file using SandCastle.
You may have to enable this feature in the Text Editor/C#/Advanced options dialog.

Resources