I need to make a action when the user make a new cell(drag and drop a cell from editorPallete).
graphComponent.addListener(mxEvent.ADD, new mxEventSource.mxIEventListener() {
#Override
public void invoke(Object sender, mxEventObject evt) {
System.out.println("event add");
}
} );
I do not receive any event for mxEvent.ADD, same result for mxEvent.ADD_CELLS.
You need to add the listener to the graph, not the graphComponent. You also need to use the CELLS_ADDED event instead of the ADD event. You can take a look at the api documentation for the mxGraph class to view a list of fired events for the class: http://jgraph.github.io/mxgraph/docs/js-api/files/view/mxGraph-js.html --> scroll down to the Events section
So your code should look something like this:
graphComponent.getGraph().addListener(mxEvent.CELLS_ADDED, new xEventSource.mxIEventListener() {
#Override
public void invoke(Object sender, mxEventObject evt) {
System.out.println("event add");
}
} );
Hope this helps,
Related
I am taking over a project with a DashboardPage and a DashboardPageViewModel that are linked through DI's ViewModelLocator. The Dashboard page have the following code that separates the Xaml/Code behinds into two separate templates.
private void SetContent()
{
Debug.WriteLine("Dashboardpage setContent");
switch(Device.Idiom)
{
case TargetIdiom.Phone:
Content = new PrimaryPhoneLayout
{
RegionContent = RegionContent
};
break;
case TargetIdiom.Tablet:
Content = new PrimaryTabletLayout(deviceDisplay)
{
RegionContent = RegionContent
};
break;
default:
throw new NotSupportedException($"{Device.Idiom} is not a supported idom");
}
}
I want to add a button in both Phone/Tablet xaml and handle the logic within DashboardPage. How can I make a reference to Dashboard page when these XAML files are linked to their individual code behind and not Dashboard Page?
To elaborate further, DashboardPage derives from MenuContainerPage that allows me to slide in/out of my slide menu. I want to handle this logic through a button that I implemented in both Tablet/Phone layout.
This is how I would do it.
I would start by creating an interface with the events I want to expose from my ContentView
For the sample, I will call this interface as IMenuOptionHandler and it would look like this
public interface IMenuOptionHandler
{
event EventHandler OnSlideIn;
event EventHandler OnSlideOut;
}
Here we have two events that will be invoked from our ContentViews. You can add as many as you wish.
Then we need to make our ContentViews to implement this interface:
public partial class PrimaryPhoneLayouts : ContentView, IMenuOptionHandler
{
//...
#region "IMenuOptionHandler implementation"
public event EventHandler OnSlideIn;
public event EventHandler OnSlideOut;
#endregion
void OnSlideInButtonClicked(object sender, EventArgs e)
{
OnSlideIn?.Invoke(this, EventArgs.Empty);
}
void OnSlideOutButtonClicked(object sender, EventArgs e)
{
OnSlideOut?.Invoke(this, EventArgs.Empty);
}
}
public partial class PrimaryTabletLayout : ContentView, IMenuOptionHandler
{
// ...
#region "IMenuOptionHandler implementation"
public event EventHandler OnSlideIn;
public event EventHandler OnSlideOut;
#endregion
void OnSlideInButtonClicked(object sender, EventArgs e)
{
OnSlideIn?.Invoke(this, EventArgs.Empty);
}
void OnSlideOutButtonClicked(object sender, EventArgs e)
{
OnSlideOut?.Invoke(this, EventArgs.Empty);
}
As you can see both classes are implementing our interface.
Also, I added two sets of methods which are the methods that you will hook to the Buttons on the XAML.
Let's imagine that your XAML looks like this:
<ContentView.Content>
<StackLayout Orientation="Vertical"
HorizontalOptions="FillAndExpand">
<Button Text="SlideIn"
VerticalOptions="CenterAndExpand"
HorizontalOptions="FillAndExpand"
Clicked="OnSlideInButtonClicked" />
<Button Text="SlideOut"
VerticalOptions="CenterAndExpand"
HorizontalOptions="FillAndExpand"
Clicked="OnSlideOutButtonClicked" />
</StackLayout>
</ContentView.Content>
Both XAML should have the buttons and the Clicked events wired to our methods in the Code behind classes.
These two methods the only purpose (as of now) is to invoke the events and notify anyone that it's subscribed to them that an event happened.
Now in your DashboardPage
you will add this global property for simplicity
IMenuOptionHandler MenuOptionHandler => Content as IMenuOptionHandler;
This will cast the Content of the Page, whatever it's the value, to IMenuOptionHandler. Any class that implements this interface will allow this cast to happen.
The last part to add on the same DashboardPage is the subscription to the events. These are gonna happen in the OnAppearing method and we will be unsubscribing on the OnDisappearing.
protected override void OnAppearing()
{
base.OnAppearing();
if (MenuOptionHandler != null)
{
MenuOptionHandler.OnSlideIn += MenuOptionHandler_OnSlideIn;
MenuOptionHandler.OnSlideOut += MenuOptionHandler_OnSlideOut;
}
}
protected override void OnDisappearing()
{
base.OnDisappearing();
if (MenuOptionHandler != null)
{
MenuOptionHandler.OnSlideIn -= MenuOptionHandler_OnSlideIn;
MenuOptionHandler.OnSlideOut -= MenuOptionHandler_OnSlideOut;
}
}
void MenuOptionHandler_OnSlideIn(object sender, EventArgs e)
{
//Logic to handle the SlideIn
Debug.WriteLine("MenuOptionHandler_OnSlideIn");
}
void MenuOptionHandler_OnSlideOut(object sender, EventArgs e)
{
//Logic to handle the SlideOut
Debug.WriteLine("MenuOptionHandler_OnSlideOut");
}
Now, whenever one of the Buttons on the ContentView (iPhone or Tablet) is clicked, the Dashboard ContentPage will be notified about this and you will be able to perform any task you wish.
Hope this helps.-
Assume you have a button in Page1, first give a name to the Button in Xaml:
<Button x:Name="btnInPage1" Text="Welcome to Xamarin.Forms!" />
In the code behind of Page1, create a public static property of button, and set the btnPageOne = btnInPage1:
public partial class Page1 : ContentPage
{
public static Button btnPageOne;
public Page1 ()
{
InitializeComponent ();
btnPageOne = btnInPage1;
}
}
Then in your DashboardPage, you can access the button by using Page1.btnPageOne, and handle the logic with:
Page1.btnPageOne.Clicked += delegate {
Console.WriteLine("Page1 btn clicked");
};
The same if you have Page2, Page3...
I'm having difficulty finding how to register a RoutedEventHandler in UWP. I'm attempting to code a template control that has event properties similar to ContentDialog's:
PrimaryButtonClick="ClickEvent"
Where ClickEvent is defined in the cs file. I'm only just getting the hang of templates, but I believe I want to do something that looks like this:
<Button Content="{TemplateBinding PrimaryButtonText}" Click="{TemplateBinding PrimaryButtonClick}"/>
Currently, all I can find is references to WPF versions of this type of code:
public static readonly RoutedEvent ValueChangedEvent =
EventManager.RegisterRoutedEvent("ValueChanged",
RoutingStrategy.Direct, typeof(RoutedPropertyChangedEventHandler<double>),
typeof(NumericBox));
public event RoutedPropertyChangedEventHandler<double> ValueChanged
{
add { AddHandler(ValueChangedEvent, value); }
remove { RemoveHandler(ValueChangedEvent, value); }
}
private void OnValueChanged(double oldValue, double newValue)
{
RoutedPropertyChangedEventArgs<double> args =
new RoutedPropertyChangedEventArgs<double>(oldValue, newValue);
args.RoutedEvent = NumericBox.ValueChangedEvent;
RaiseEvent(args);
}
But of course the types have changed. Can someone point me in the right direction?
Unfortunately, the concept of RoutedEvent (bubbling, tunneling) is not available in UWP currently. You can just create a classic event however instead:
public event EventHandler PrimaryButtonClick;
protected void InnerButton_Click(object sender, EventArgs e)
{
PrimaryButtonClick?.Invoke( sender, e );
}
Bubbling of events is possible for some predefined events, but it is not yet possible to allow bubbling for custom events in current version of UWP.
I am trying to create a user notification. Ideally a toast-like notification that shows up in the corner for about three seconds.
I am using MVVM-light and I think the notification could be done using its messenger-service.
I have this class:
public class NotificationSync
{
public string Messages { get; set; }
}
In one viewmodel i set up the Messenger like this:
Messenger.Default.Send(new NotificationSync()
{
Messages = "message"
});
And in my MainviewModel (which is the datacontext of the view) I listen for it like this:
Messenger.Default.Register<NotificationSync>(this, (action) =>
Mess = action.Messages );
Mess is a string property on the viewmodel:
private string mess;
public string Mess
{
get { return mess; }
set
{
mess = value;
RaisePropertyChanged("Mess");
}
}
What I would like to do with mess is to bind it to my view in a toast-like manner. I.E display it for some seconds in my view. Any tips on how to do this? Thank you.
What about a Visibility property for your toast plus a timer?
Messenger.Default.Register<NotificationSync>(this, (action) =>
Mess = action.Messages
ShowToast();
);
private void ShowToast()
{
IsToastVisible = true;
dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = TimeSpan.FromSeconds(3);
dispatcherTimer.Start();
}
void OnTimerTick(Object sender, EventArgs args)
{
IsToastVisible = false;
}
This assumes the textbox to which Mess is bound, is also bound to IsToastVisible and it's using a VisibilityConverter.
Iam developing a RCP application which consists of views and editors. I can change the values and edit the values of some parameters in editor. When a value has been changed, i need to make the editor dirty as well as would also like to enable the save button. Till now, i have not implemented my save button. Could anyone guide me how to make the save button enabled as well as how can i make an editor dirty when some modifications happen in editor.
Thanks in advance. Any help will be greatly appreciated.
Regards,
Girish
Here is an overview of the Form editor logic, hop it will help you.
public class TestEditor extends FormEditor {
#Override
protected void addPages() {
// this method is called when the editor is being created
// to add the necessary pages
// page classes should be like following
// class TestEditorPage extends FormPage
try {
TestEditorPage pageTest = new TestEditorPage(this);
addPage(pageTest);
} catch (PartInitException e) {
}
}
#Override
public void doSave(IProgressMonitor monitor) {
// this method will be called on save action
// (or Ctrl + s shortcut)
}
#Override
public void doSaveAs() {
// this method will be called on save-as
//call (or Ctrl + Shift + s shortcut)
}
#Override
public boolean isSaveAsAllowed() {
// put here the call to the logic that
// check if the save is allowed
return true;
}
#Override
public boolean isDirty() {
// Here the call for the logic that
// defines if the editor is dirty or not
return true;
}
}
I have created a User Control containing a DatePicker and I want create an event in the user control linking to the DatePicker DateChanged event. This custom user control will be used in an itemscontrol.
Yes. Add a public event to the control. And then add a method that looks for delegates attached to the event. If there are any delegates, raise the event. Here's an example:
In the User Control:
public partial class Controls_UserComments : System.Web.UI.UserControl
{
// the event delegates may listen for
public event EventHandler CommentEditing;
protected void Page_Load(object sender, EventArgs e)
{
// handle an event in the user control and bubble the event up to any delegates
GridView_Comments.RowCancelingEdit += new GridViewCancelEditEventHandler(GridView_Comments_RowCancelingEdit);
}
void GridView_Comments_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView_Comments.EditIndex = -1;
GridView_Comments.DataBind();
// raise the event for attached delegates
if (CommentEditing != null)
CommentEditing(this, EventArgs.Empty);
}
}
Now, in the web form utilizing the user control:
<ppc:UserComments ID="UserComments_ObservationComments" runat="server"
OnCommentEditing="RefreshComments"
/>
Good luck!