public MainWindow()
{
public static IHighlightingDefinition Load(XshdSyntaxDefinition syntaxDefinition,IHighlightingDefinitionReferenceResolver resolver)
Load('[lua.xshd]', IHighlightingDefinitionReferenceResolver);
}
How do i fix this?
Related
If I pull a class A and class B onto a Code Map, VSE (Visual Studio Enterprise) will map the direct calls of class A calling methods in class B.
So,
public class A
{
public void DoSomething()
{
b.DoSomethingElse();
}
}
This will map. But if it's something like:
public class A
{
public void DoSomething()
{
d.DoManyThings();
}
}
public class D
{
public void DoManyThings()
{
c.DoThings();
}
}
public class C
{
public void DoThings()
{
b.DoSomethingElse();
}
}
public class B
{
public void DoSomethingElse()
{
// imagine code here
}
}
Then the Code Map won't map between class A and class B automatically. The only way I've found to show those dependencies is to go to each method and click "Show Methods This Calls".
Is there a way to get VSE make the Code Map all those dependencies initially without having to investigate every method?
I would create a controller that response with the third-party IHtmlContent created through IHtmlHelper interface without using View() method. Actually I'm using a workaround:
I have created an extension function of IHtmlHelper that behaves like a "master" and other static function that behaves like "slaves". The master function call slaves based on data structure that It has received as a parameter. Slaves create the IHtmlContent based on third-party library. When all slave functions are completed, the controller send back the response.
This behaviour is inside cshtml view called by controller.
Is possible to have IHtmlHelper as controller constructor parameter using dependency Injection?
Something like
public class MyTestController
{
private readonly IHtmlHelper _html;
public MyTestController(IHtmlHelper html) {
_html = html;
}
}
If it were possible I would simplify many operations because I'm using this Master/Slave functions to replicate the behaviours of a class
Here you can find a pure example of how my code works now and how I would like to change it
https://gist.github.com/Blackleones/eb0d02b9dd99164271af88e22143d72b#file-example-cs
I need this component because the third-party library is an extension of IHtmlHelper
I thank Itminus for answering me.
However call IHtmlHelper on controller constructor it is not enough, you must contextualize IHtmlHelper before using it.
I want to share another solution that I found and solve my problem, maybe It can be useful for other users.
Please see https://gist.github.com/Blackleones/eb0d02b9dd99164271af88e22143d72b#file-example-cs to remember what my goal is.
To solve my problem I have created a class called HtmlHelperAdapter that contains some instances I need.
public class MyHtmlHelperAdapter
{
public IHtmlHelper _html;
public NubessHtmlHelperAdapter(IHtmlHelper html)
{
_html = html;
}
public IHtmlHelper Html => _html;
public ViewContext ViewContext => _html.ViewContext;
public HttpContext HttpContext => _html.ViewContext.HttpContext;
public ViewDataDictionary ViewData => _html.ViewData;
public IServiceProvider provider => _html.ViewContext.HttpContext.RequestServices;
}
after that I have created an extension method for IHtmlHelper
public static class MyBuilderExtension
{
public static MyBuilder Nubess(this IHtmlHelper html)
{
return new MyBuilder(new MyHtmlHelperAdapter(html));
}
}
where MyBuilder is
public class MyBuilder
{
private readonly MyHtmlHelperAdapter _htmlHelper;
public MyBuilder(MyHtmlHelperAdapter htmlHelper)
{
_htmlHelper = htmlHelper;
}
public FormBuilder<object> Form<T>(IDictionary<string, string> customizeArgs = null, FormBuilder<object> externalForm = null)
{
return new FormBuilder(_htmlHelper, typeof(T), customizeArgs, externalForm).Build();
}
}
now I can create my custom contents for views using class thanks to MyHtmlHelperAdapter
public class FormBuilder
{
private MyHtmlHelperAdapter _htmlHelper;
private readonly IStringLocalizer<SharedResources> _localizer;
private readonly LinkGenerator _linkGenerator;
private readonly IEngine _engine;
private readonly IEngineElement _element;
private readonly Type _typeDescriptor;
private readonly IDictionary<string, string> _descriptorArgs;
/* variabili per semplificare la gestione del builder */
private readonly FormBuilder<object> Form;
private readonly FormConfig Config;
private readonly IList<FormGroupConfig> Groups;
private readonly IList<FormItemConfig> Items;
private readonly IDictionary<string, string> FormOptions;
private readonly string _clientPrefix = "smtForm_{0}_";
public FormBuilder(MyHtmlHelperAdapter htmlHelper, Type typeDescriptor, IDictionary<string, string> ModelCustomizeArgs = null, FormBuilder<object> externalForm = null)
{
_htmlHelper = htmlHelper;
_localizer = _htmlHelper.provider.GetRequiredService<IStringLocalizer<SharedResources>>() ?? throw new ArgumentNullException();
_linkGenerator = _htmlHelper.provider.GetRequiredService<LinkGenerator>() ?? throw new ArgumentNullException();
_engine = _htmlHelper.provider.GetRequiredService<IEngine>() ?? throw new ArgumentNullException();
//code..
}
public FormBuilder<object> Build()
{
//code..
return Form;
}
//methods..
}
Short answer : Yes .
If you look into the source code of MVC, you will find that the IHtmlHelper is registered as a transient service :
internal static void AddViewServices(IServiceCollection services)
{
...
//
// HTML Helper
//
services.TryAddTransient<IHtmlHelper, HtmlHelper>();
services.TryAddTransient(typeof(IHtmlHelper<>), typeof(HtmlHelper<>));
services.TryAddSingleton<IHtmlGenerator, DefaultHtmlGenerator>();
services.TryAddSingleton<ModelExpressionProvider>();
// ModelExpressionProvider caches results. Ensure that it's re-used when the requested type is IModelExpressionProvider.
services.TryAddSingleton<IModelExpressionProvider>(s => s.GetRequiredService<ModelExpressionProvider>());
services.TryAddSingleton<ValidationHtmlAttributeProvider, DefaultValidationHtmlAttributeProvider>();
services.TryAddSingleton<IJsonHelper, DefaultJsonHelper>();
...
}
In other words, you could inject the IHtmlHelper service as you like.
Just as a reminder, we should always prefer to using IHtmlHelper within View Layer.
I have created a Windows8 blank xaml application.And now i want to make this application as a Share Target.I have followed the instructions at below link and able to make it as a Target app.
http://msdn.microsoft.com/en-us/library/windows/apps/xaml/Hh973053
But where do i add the following method (OnShareTargetActivated) in my blank xaml application ?When i manually add this method in mainpage.xaml.cs , it shows the errors"
"Project.MainPage.OnShareTargetActivated(Windows.ApplicationModel.Activation.ShareTargetActivatedEventArgs)' is a new virtual member in sealed class "
"Project.MainPage.OnShareTargetActivated(Windows.ApplicationModel.Activation.ShareTargetActivatedEventArgs)': no suitable method found to override "
protected override async void OnShareTargetActivated(ShareTargetActivatedEventArgs args)
{
// Code to handle activation goes here.
}
It goes in the class that inherits from Windows.UI.Xaml.Application. Typically App.cs.
E.g.
sealed partial class App : Application
{
public App()
{
this.InitializeComponent();
}
//...
protected override void OnShareTargetActivated(ShareTargetActivatedEventArgs args)
{
var rootFrame = new Frame();
rootFrame.Navigate(typeof(MainPage), args.ShareOperation);
Window.Current.Content = rootFrame;
Window.Current.Activate();
}
}
Abstracting commands into the View Model is a valuable practice with XAML/MVVM projects. I get that. And, I see ICommand in in WinRT; but, how do we implement it? I haven't found a sample that actually works. Anyone know?
My all time favorite has to be the DelegateCommand provided by the Microsoft Patterns and Practices team. It allows you to create a typed command:
MyCommand = new DelegateCommand<MyEntity>(OnExecute);
...
private void OnExecute(MyEntity entity)
{...}
It also provides a way to raise the CanExecuteChanged event (to disable/enable the command)
MyCommand.RaiseCanExecuteChanged();
Here's the code:
public class DelegateCommand<T> : ICommand
{
private readonly Func<T, bool> _canExecuteMethod;
private readonly Action<T> _executeMethod;
#region Constructors
public DelegateCommand(Action<T> executeMethod)
: this(executeMethod, null)
{
}
public DelegateCommand(Action<T> executeMethod, Func<T, bool> canExecuteMethod)
{
_executeMethod = executeMethod;
_canExecuteMethod = canExecuteMethod;
}
#endregion Constructors
#region ICommand Members
public event EventHandler CanExecuteChanged;
bool ICommand.CanExecute(object parameter)
{
try
{
return CanExecute((T)parameter);
}
catch { return false; }
}
void ICommand.Execute(object parameter)
{
Execute((T)parameter);
}
#endregion ICommand Members
#region Public Methods
public bool CanExecute(T parameter)
{
return ((_canExecuteMethod == null) || _canExecuteMethod(parameter));
}
public void Execute(T parameter)
{
if (_executeMethod != null)
{
_executeMethod(parameter);
}
}
public void RaiseCanExecuteChanged()
{
OnCanExecuteChanged(EventArgs.Empty);
}
#endregion Public Methods
#region Protected Methods
protected virtual void OnCanExecuteChanged(EventArgs e)
{
var handler = CanExecuteChanged;
if (handler != null)
{
handler(this, e);
}
}
#endregion Protected Methods
}
Check out RelayCommand class (only METRO code). The NotifyPropertyChanged class can be found here. The NotifyPropertyChanged class is only used to allow bindings on CanExecute and update it with RaiseCanExecuteChanged.
The original relay command class can be found here
Unfortunately there does not seem to be a native class that implements it for you. The interface is not overly complicated if you want to implement it yourself, and the popular MVVM Lite toolkit includes its own version of RelayCommand. You can add MVVM Lite to your project by right-clicking on References and choosing "Manage NuGet Packages". If you don't have this option, enable Nuget under Tools -> Extensions and Updates.
I've been looking for a minimal end-to-end implementation of a XAML-MVVM command, and not found it yet.
So, following #Rico's answer I ended up with the following as a minimal RelayCommand which works. I use a similar minimal version in a large project.
public class RelayCommand : System.Windows.Input.ICommand {
readonly Action<object> execute;
public RelayCommand(Action<object> execute) {
this.execute = execute;
}
public bool CanExecute(object parameter) {
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter) {
this.execute(parameter);
}
}
The larger RelayCommand class seems to provide more control over CanExecute and CanExecuteChanged, but you don't need that to get started - and you may not need it at all.
To use it in a view model:
class ViewModel : INotifyPropertyChanged {
<< ... snip VM properties and notifications ...>>
public RelayCommand DoSomethingCommand {
get {
return new RelayCommand(param => {
this.DoSomething(param as AType);
Debug.WriteLine("Command Executed");
});
}
}
}
(We don't need the INotifyPropertyChanged for the Command, but any view model typically implements it.)
Finally, the XAML...
<Grid>
<!-- Set the data context here, for illustration. -->
<Grid.DataContext>
<local:ViewModel/>
</Grid.DataContext>
<!-- A sample control bind to a property -->
<TextBlock
Text="{Binding AProp}"/>
<!-- Bind a command -->
<Button Command="{Binding DoSomethingCommand}" CommandParameter="foo">Change!</Button>
</Grid>
I found this really good example at
https://code.msdn.microsoft.com/windowsapps/Working-with-ICommand-690ba1d4
<Page.Resources>
<local:MyCommandsCollection x:Key="MyCommands" />
</Page.Resources>
<Button Width="280"
Height="59"
Margin="513,280,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Command="{Binding MyFirstCommand}"
CommandParameter="{Binding Text,
ElementName=myTextBox}"
Content="Execute Command" />
public class MyCommandsCollection
{
public MyCommand MyFirstCommand
{
get { return new MyCommand(); }
}
}
public class MyCommand : ICommand
{
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
public async void Execute(object parameter)
{
MessageDialog message = new MessageDialog(
"The command is executing, the value of the TextBox is " + parameter as String);
await message.ShowAsync();
}
}
I tried this out with x:Bind and it works nicely. All I need is to expose a property in my ViewModel that returns a new Instance of the "MyCommand" class and it's all good.
Since I'm setting the DataContext in my XAML, I didn't need to mess with any of the "MyCommandCollection" stuff. Yay compiled binding.
Here is the set up that is not working
Using Ninject V3.0
public class LoggerModule : NinjectModule{
public override void Load()
{
Bind<ILogger>.ToProvider(MyLoggerProvider);
}
}
public class MyLoggerProvider: IProvider<ILogger>
{
public object Create(IContext context){
return new OneOfMyLoggers();
}
}
In my application wherever I inject instance of ILogger (using constructor or property injection, just does matter) I never get instance of ILogger resolved.
But If do not use module and/or povider, and bind when kernel is created, everything works like a charm. The following works
public class MyDiResolver()
{
public MyDiResolver()
{
MyKernel = new StandardKernel();
MyKernel.Bind<ILogger>().To<OneOfMyLoggers>();
}
}
The same arrangement of modules and providers works fine in Ninject2.x version. Is there something different about Ninject V3.0 that I am missing?
Thanks
Try passing the module into the StandardKernel so it knows to use it:
using (IKernel kernel = new StandardKernel(new LoggerModule()))
{
ILogger logger = kernel.Get<OneOfMyLoggers>();
}