how to add new menu item with observer in magento2?
I have define event in config.xml in my module as below
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="page_block_html_topmenu_gethtml_after">
<observer name="namespacetest_page_block_html_topmenu_gethtml_after" instance="Namespace\Test\Observer\AddMenu"/>
</event>
</config>
and below observer file contain code
<?php
namespace Namespace\Test\Observer;
use Magento\Framework\Event\ObserverInterface;
class AddMenu implements ObserverInterface
{
public function execute(\Magento\Framework\Event\Observer $observer)
{
$event = $observer->getEvent();
// function not called when event occur
}
}
what I am doing wrong here? Please help me in figure out the issue.
Thanks
You have define event in wrong file name. i.e. config.xml
It should be events.xml in directory
[magentoroot]/app/code/Namespace/Test/etc/frontend/events.xml
Related
I started working with .NET MAUI. I ran into a problem just by starting my development.
I want to show a popup and I'm using the Community Toolkit.
All I did is:
I created a new .NET MAUI Application Project, installed the Community Toolkit NuGet Package (of course also the .UseMauiCommunityToolkit in the start up class) and added a XAML File for the Popup:
<?xml version="1.0" encoding="utf-8" ?>
<toolkit:Popup xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
x:Class="TestApp.ProfilePopup">
<VerticalStackLayout>
<Label Text="This is a very important message!" />
</VerticalStackLayout>
</toolkit:Popup>
I've no partial class for this popup
I just modified the button on MainPage to display the popup:
private void OnCounterClicked(object sender, EventArgs e)
{
var popup = new ProfilePopup();
this.ShowPopup(popup);
}
If I run this application and click on the button to display the popup I'll get the error message:
CommunityToolkit.Maui.Core.Handlers.PopupHandler found for TestApp.ProfilePopup is incompatible
If I create the popup in C#, it works:
private void OnCounterClicked(object sender, EventArgs e)
{
var popup = new Popup
{
Content = new VerticalStackLayout
{
Children =
{
new Label
{
Text = "This is a very important message!"
}
}
}
};
this.ShowPopup(popup);
}
Any idea what I'm doing wrong?
Thank you!
Markus
I reproduced the error message.
THE CAUSE: "I've no partial class for this popup".
That won't work. without that, there is no InitializeComponent call. The result is not a valid View.
To fix the problem,
First make sure you have got the toolkit registered in MauiProgram.cs:
using CommunityToolkit.Maui;
...
builder.UseMauiApp<App>().UseMauiCommunityToolkit();
then you must have
file ProfilePopup.xaml.cs containing:
public partial class ProfilePopup : CommunityToolkit.Maui.Views.Popup
{
public ProfilePopup()
{
InitializeComponent();
}
}
I generate custom views using these steps:
Project / Add New Item / .NET MAUI ContentView (XAML).
Give name "MyView". This adds TWO files to project: MyView.xaml and MyView.xaml.cs.
In MyView.xaml, add needed xmlns and change base class.
Was:
<ContentView xmlns=...
...
x:Class=...>
...
</ContentView>
change to:
<toolkit:Popup xmlns=
...
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
x:Class=...>
...
</toolkit:Popup>
In MyView.xaml.cs, change base class.
Was:
public partial class ProfilePopup : ContentView
change to:
public partial class ProfilePopup : CommunityToolkit.Maui.Views.Popup
The documentation on Microsoft page should be completed (I used as template):
https://learn.microsoft.com/en-us/dotnet/communitytoolkit/maui/views/popup
I was having the same problem. I forgot to add .UseMauiCommunityToolkit(); to MauiProgram.cs.
You can try this, as well:
var popup = new PopupPage();
await PopupExtensions.ShowPopupAsync<PopupPage>(this, popup);
Is there a way to create a file with specific extension. Currently im creating a html kind file. Is there a way to give specific extension to the file while creating? Maybe .css or .js etc?
<extension
point="org.eclipse.ui.newWizards">
<category id="com.ui.category" name="XXX Project">
</category>
<wizard
category="com.ui.category"
id="ui.wizard.NewFileWizard"
name="Create a new File"
icon="icons/new_project.png"
class="org.eclipse.ui.wizards.newresource.BasicNewFileResourceWizard"
project="true"
>
</wizard>
</extension>
You will have to create your own wizard to do this, extending BasicNewFileResourceWizard
The minimum code would be something like this:
public class FileExtNewFileWizard extends BasicNewFileResourceWizard
{
public FileExtNewFileWizard()
{
super();
}
#Override
public void addPages()
{
super.addPages();
// Get the page created by `super.addPages` and set the default file extension
WizardNewFileCreationPage page = (WizardNewFileCreationPage)getPage("newFilePage1");
page.setFileExtension("css");
}
}
In the app I'm working on, there's a multi-steps registration, 4 steps:
to accomplish it, I'm thinking to have a single page to host a content view of the registration step, when it passes the validation requirements I remove it and inject the next content view.
This is an example to simplify my need:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:XamApp"
x:Class="XamApp.MainPage">
<local:Register1/>
</ContentPage>
and the Register1 looks like this:
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:XamApp"
x:Class="XamApp.Register1">
<ContentView.Resources>
<ResourceDictionary>
<local:IntToBoolConverter x:Key="intToBool"/>
</ResourceDictionary>
</ContentView.Resources>
<ContentView.Content>
<StackLayout>
<Label Text="Page 1" FontSize="Large"/>
<Entry x:Name="txtName" Placeholder="Name"/>
<Button Text="Next" IsEnabled="{Binding Source={x:Reference txtName},
Path=Text.Length,
Converter={StaticResource intToBool}}"
Clicked="Button_Clicked"/>
</StackLayout>
</ContentView.Content>
</ContentView>
There are two problems:
1- I don't know how to handle the data (view model) between steps, to have only one object through all steps (Shall I use DI? if yes , then how in MVVMLight?)
2- How to to inject the content view into the main registration page dynamically in an MVVM fashion?
The solution I came up with, is creating an interface:
public interface INavigate
{
INavigate Next();
INavigate Previous();
}
all the ContentViews inherit from, for example the second ContentView implementation:
public INavigate Next()
=> new View3();
public INavigate Previous()
=> new View1();
the container page's Content property is bound to the view model's property CurrentView of type INavigate,
the command of the next button execute this:
CurrentView = CurrentView.Next();
and this is for the previous button:
CurrentView = CurrentView.Previous();
What is required to define a custom event for a UI plugin in NativeScript?
What I'm trying to achieve is to trigger a foo event that works similar to the tap event on a Button and can be hooked into as follows:
<Page xmlns="http://schemas.nativescript.org/tns.xsd"
xmlns:fooplugin="nativescript-foo-plugin">
<StackLayout>
<Button text="Tap Me!" tap="{{ onTap }}" />
<fooplugin:FooPlugin foo="{{ onFoo }}" />
</StackLayout>
</Page>
What I've done essentially boils down to calling the notify function with the eventName value of foo from within the plugin code (ignoring memory leak considerations):
import * as view from 'ui/core/view';
export class FooPlugin extends view.View {
constructor() {
super();
setTimeout(() => {
this.notify({
eventName: 'foo',
object: this,
});
// also tried this._emit('foo');
}, 1000);
}
}
Is there something else that I'm missing and that I need to do to make this work?
create a property public static fooEvent="foo"
the name of the property is important it should be eventname+ Event now it should work.
create a property of your Event public static fooEvent="foo". The name is important! It has to be eventname + "Event"
overload the on-function in your declaration file index.d.ts or FooPlugin.d.ts
// Needed when on method is overriden.
on(eventNames: string, callback: (data: EventData) => void, thisArg?: any);
I create a file creation page by extending class WizardNewFileCreationPage. What I am trying to do is to hide the help button on the left bottom corner. Any suggestions on how to do this?
Thank you.
Call this method: Wizard#setHelpAvailable(false)
Refer API here
I called this method but not working. See the following:
public class NewTDLDiagram extends Wizard implements INewWizard {
private NewDiagramFilePage page;
public NewTDLDiagram() {
// TODO Auto-generated constructor stub
setHelpAvailable(false);
}
...
}
This class is registered as an extension point of org.eclipse.ui.newWizards:
<extension
point="org.eclipse.ui.newWizards">
<wizard
class="com.abc.graphicview.ui.NewDiagram"
icon="icons/NewSmdWizard.gif"
id="com.abc.graphicview.ui.diagrawizard"
name="Diagram"
project="false">
<selection
class="org.eclipse.core.resources.IResource">
</selection>
</wizard>
</extension>