I'm trying to subscribe to the Activate event of an NSStatusBarButton object in AppDelegate's DidFinishLaunching() but the event never gets invoked.
The purpose is to get notified when the top menu bar icon of the application is clicked so its contents can get populated dynamically.
using AppKit;
using Foundation;
[Register("AppDelegate")]
public class AppDelegate : NSApplicationDelegate
{
private NSStatusItem _statusBar;
public override void DidFinishLaunching(NSNotification notification)
{
this._statusBar = NSStatusBar.SystemStatusBar.CreateStatusItem(NSStatusItemLength.Variable);
this._statusBar.Title = "MyApp";
this._statusBar.HighlightMode = true;
this._statusBar.Menu = new NSMenu("MyApp");
// Working example on NSMenuItem object
var someItem = new NSMenuItem("Some Item");
someItem.Activated += (sender, e) =>
{
System.Diagnostics.Debug.WriteLine("This one does fire.");
};
this._statusBar.Menu.AddItem(someItem);
// Problem
this._statusBar.Button.Activated += (sender, e) =>
{
System.Diagnostics.Debug.WriteLine("This one does not fire.");
};
}
}
It does not fire because you attached a menu. The button action is popping up the menu and the activated event of the button is never fired. If you remove the menu the button event will run.
Either remove the menu and use it as a button. Then your event will fire. Or just use the menu.
If you want to run custom code when the menu is shows set a delegate of the NSMenu:
using AppKit;
using Foundation;
public class MyMenuDelegate : NSObject, INSMenuDelegate
{
public void MenuWillHighlightItem(NSMenu menu, NSMenuItem item)
{
}
[Export("menuWillOpen:")]
public void MenuWillOpen(NSMenu menu)
{
// your code here
}
}
[Register("AppDelegate")]
public class AppDelegate : NSApplicationDelegate
{
private NSStatusItem _statusBar;
MyMenuDelegate _menuDel;
public override void DidFinishLaunching(NSNotification notification)
{
_statusBar = NSStatusBar.SystemStatusBar.CreateStatusItem(NSStatusItemLength.Variable);
_statusBar.Title = "MyApp";
_statusBar.HighlightMode = true;
_statusBar.Menu = new NSMenu("MyApp");
_menuDel = new MyMenuDelegate();
_statusBar.Menu.Delegate = _menuDel;
// Working example on NSMenuItem object
var someItem = new NSMenuItem("Some Item");
someItem.Activated += (sender, e) =>
{
System.Diagnostics.Debug.WriteLine("This one does fire.");
};
_statusBar.Menu.AddItem(someItem);
}
}
Related
I have a custom UserControl called ClosableTabItem which inherits from the TabItem control. I simply added a save button and a close button and I'm trying to wire in some event handlers. When the user clicks on the X (close), I want to invoke a "OnClosing" event with cancellation event arguments so that the user can put in some logic in the OnClosing event and if needed, cancel the close operation just like you can on a windows FormClosing event.
I'm not sure how I can fire the event and wait for a response before removing the tabitem from the collection.
Any ideas?
Thanks.
public class ClosableButtonTabItem : TabItem
{
private readonly cTabButtonHeader _closableTabHeader;
public event EventHandler<TabButtonClickEventArgs> OnTabButtonClick;
public event EventHandler<System.ComponentModel.CancelEventArgs> OnTabClosing;
public event EventHandler OnTabClosed;
public UserControl AttachedForm { get; set; }
public string Title
{
get => ((cTabButtonHeader) this.Header).label_TabTitle.Content.ToString();
set => ((cTabButtonHeader)this.Header).label_TabTitle.Content = value;
}
public ClosableButtonTabItem()
{
_closableTabHeader = new cTabButtonHeader();
Header = _closableTabHeader;
_closableTabHeader.button_close.Source =
ImageHelper.LocalPathToImageSource(ImageHelper.ImageSizes.Size_32x32, "x_off.png");
_closableTabHeader.button_close.MouseEnter += button_close_MouseEnter;
_closableTabHeader.button_close.MouseLeave += button_close_MouseLeave;
_closableTabHeader.button_close.MouseLeftButtonDown += button_close_MouseLeftButtonDown;
_closableTabHeader.label_TabTitle.SizeChanged += label_TabTitle_SizeChanged;
//closableTabHeader.button_group.MouseEnter += button_save_MouseEnter;
//closableTabHeader.button_group.MouseLeave += button_save_MouseLeave;
_closableTabHeader.button_save.MouseLeftButtonDown += button_save_MouseLeftButtonDown;
}
void button_close_MouseLeftButtonDown(object sender, MouseButtonEventArgs mouseButtonEventArgs)
{
OnTabClosing?.Invoke(this, new CancelEventArgs());
//Code somewhere that if they don't cancel the OnClosing event the run:
((TabControl)Parent).Items.Remove(this); }
}
I found it.
void button_close_MouseLeftButtonDown(object sender, MouseButtonEventArgs mouseButtonEventArgs)
{
if (OnTabClosing == null)
{
((TabControl)Parent).Items.Remove(this);
OnTabClosed?.Invoke(this, EventArgs.Empty);
return;
}
foreach (var subHandler in OnTabClosing.GetInvocationList())
{
var cea = new TabButtonClosingEventArgs(AttachedForm);
OnTabClosing?.Invoke(this, cea);
if (cea.Cancel) continue;
((TabControl)Parent).Items.Remove(this);
OnTabClosed?.Invoke(this, EventArgs.Empty);
}
}
I'm working with RecyclerView (Xamarin.Android) on Visual Studio 2017, this is my first Xamarin project since I'm a native Android developer, and I'm kinda confused respect to implementing the On Items Clicks events on the Recycler View. I just created the RecyclerView.Adapter using the default template that the IDE provides (RecyclerAdapter class), it looks a lot like the native implementation:
Here my Code:
using System;
using Android.Views;
using Android.Widget;
using Android.Support.V7.Widget;
namespace Airlink
{
class PdfAdapter : RecyclerView.Adapter
{
public event EventHandler<PdfAdapterClickEventArgs> ItemClick;
public event EventHandler<PdfAdapterClickEventArgs> ItemLongClick;
Pdf[] items;
public PdfAdapter(Pdf[] data)
{
items = data;
}
// Create new views (invoked by the layout manager)
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup
parent, int viewType)
{
//Setup your layout here
View itemView = null;
//var id = Resource.Layout.__YOUR_ITEM_HERE;
itemView=LayoutInflater.From(parent.Context).
Inflate(Resource.Layout.pdf_item, parent, false);
var vh = new PdfAdapterViewHolder(itemView, OnClick,
OnLongClick);
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
public override void OnBindViewHolder(RecyclerView.ViewHolder
viewHolder, int position)
{
var item = items[position];
// Replace the contents of the view with that element
PdfAdapterViewHolder holder = viewHolder as
PdfAdapterViewHolder;
holder.pdf_name.Text = items[position].Name;
}
public override int ItemCount => items.Length;
void OnClick(PdfAdapterClickEventArgs args) =>
ItemClick?.Invoke(this, args);
void OnLongClick(PdfAdapterClickEventArgs args) =>
ItemLongClick?.Invoke(this, args);
}
public class PdfAdapterViewHolder : RecyclerView.ViewHolder
{
public TextView pdf_name { get; set; }
public PdfAdapterViewHolder(View itemView,
Action<PdfAdapterClickEventArgs> clickListener,
Action<PdfAdapterClickEventArgs> longClickListener) :
base(itemView)
{
pdf_name = itemView.FindViewById<TextView>
(Resource.Id.pdf_name);
itemView.Click += (sender, e) => clickListener(new
PdfAdapterClickEventArgs { View = itemView, Position =
AdapterPosition });
itemView.LongClick += (sender, e) => longClickListener(new
PdfAdapterClickEventArgs { View = itemView, Position =
AdapterPosition });
}
}
public class PdfAdapterClickEventArgs : EventArgs
{
public View View { get; set; }
public int Position { get; set; }
}
}
So, given my code, I just want to know where I can handle the clicked View, I'm not sure if I have to implement some code on the PdfAdapterClickEventArgs Class or on the OnClick and OnLongClick voids that receive the PdfAdapterClickEventArgs object, let's say I want to show a Toast showing the name of the TextView inside the View clicked. I use to handle this action on native Android using:
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//my code here
}
}
inside the ViewHolder constructor method.
I mean, I can change the default C# implementation in order to get something similar to the snippet above, but I would like to keep the original C# code.
You will implement your OnClick event handler in the Activity/Fragment where instance of your Adapter is created.
This line means you are making the ItemClick event public in your adapter class.
public event EventHandler<PdfAdapterClickEventArgs> ItemClick;
So you are now able to do this:
var adapter = new PdfAdapter(data);
adapter.ItemClick += OnItemClick;
....
....
myRecyclerView. SetAdapter(adapter);
And you will have a method:
public void OnItemClick(object sender, PdfAdapterClickEventArgs e)
{
var view = args.View; //this is your view
Toast.MakeText(this, $"Item Position: {args?.Position}", ToastLength.Short).Show();
}
Hope this helps.
I have a page and on clicking a plus button on toolbar i am calling a popup page
from popup page user can add a new entry or cancel / close window without doing anything
Everything is working fine and code is like this
public partial class SelectSchool : ContentPage
{
public SelectSchool()
{
InitializeComponent();
#region toolbar
ToolbarItem tbi = null;
if (Device.OS == TargetPlatform.Android)
{
tbi = new ToolbarItem("+", "plus", async () =>
{
var target_page = new AddSchool();
Navigation.PushModalAsync(target_page);
}, 0,0);
}
ToolbarItems.Add(tbi);
#endregion
this.Title = "Select School";
}
}
And my popup page is like
public partial class AddSchool : ContentPage
{
public AddSchool()
{
InitializeComponent();
}
private async void Button_OK_Clicked(object sender, EventArgs e)
{
//doing some operations like entry to db etc and close page
Navigation.PopModalAsync();
}
private void cancelClicked(object sender, EventArgs e)
{
Navigation.PopModalAsync();
}
}
But now i want to wait for the Popup to get closed to do some additional coding and i tried below code
if (Device.OS == TargetPlatform.Android)
{
tbi = new ToolbarItem("+", "plus", async () =>
{
var target_page = new AddSchool();
await Navigation.PushModalAsync(target_page);
//await till target_page is closed and once its closed call my next function here
}, 0,0);
}
But await is not working . How can i await on this area till the popup getting closed ? Any idea??
Use the Disappearing event on your modal page.
Example:
var modalPage = new ContentPage();
modalPage.Disappearing += (sender2, e2) =>
{
System.Diagnostics.Debug.WriteLine("The modal page is dismissed, do something now");
};
await content.Navigation.PushModalAsync(modalPage);
System.Diagnostics.Debug.WriteLine("The modal page is now on screen, hit back button");
Or use a EventWaitHandle:
var waitHandle = new EventWaitHandle(false, EventResetMode.AutoReset);
var modalPage = new ContentPage();
modalPage.Disappearing += (sender2, e2) =>
{
waitHandle.Set();
};
await content.Navigation.PushModalAsync(modalPage);
System.Diagnostics.Debug.WriteLine("The modal page is now on screen, hit back button");
await Task.Run(() => waitHandle.WaitOne());
System.Diagnostics.Debug.WriteLine("The modal page is dismissed, do something now");
A bit late on the answer here, but it might be best to listen to the Application's OnModalPagePopping event handler.
First, create the modal page. Give it a property to store the data you want to later retrieve:
public class MyModalPage : ContentPage
{
public string Data { get; set; }
public MyModalPage()
{
InitializeComponent();
// ... set up the page ...
}
private async void PopThisPage()
{
// When you want to pop the page, just call this method
// Perhaps you have a text view with x:Name="PhoneNumber", for example
Data = PhoneNumber.Text; // store the "return value" before popping
await MyProject.App.Current.MainPage.Navigation.PopModalAsync();
}
}
In the parent page, you can create the modal page, and set up the event handler to listen for when the modal page pops:
public class MyPage : ContentPage
{
MyModalPage _myModalPage;
public MyPage()
{
InitializeComponent();
// ... set up the page ...
}
private async void ShowModalPage()
{
// When you want to show the modal page, just call this method
// add the event handler for to listen for the modal popping event:
MyProject.App.Current.ModalPopping += HandleModalPopping;
_myModalPage = new MyModalPage();
await MyProject.App.Current.MainPage.Navigation.PushModalAsync(_myModalPage());
}
private void HandleModalPopping(object sender, ModalPoppingEventArgs e)
{
if (e.Modal == _myModalPage)
{
// now we can retrieve that phone number:
var phoneNumber = _myModalPage.Data;
_myModalPage = null;
// remember to remove the event handler:
MyProject.App.Current.ModalPopping -= HandleModalPopping;
}
}
}
This is better than using the OnDisappearing method, as others have already stated that can be called when the app is backgrounded, etc. And its behavior is not consistent across platforms.
There is also another event OnModalPopped, which is called after the modal is completely popped from the navigation stack. If using that, it should work similarly.
You can try to create an event, call when pop close.
public partial class AddSchool : ContentPage
{
public delegate void PopupClosedDelegate();
public event PopupClosedDelegate PopupClosed;
public AddSchool()
{
InitializeComponent();
}
private async void Button_OK_Clicked(object sender, EventArgs e)
{
//doing some operations like entry to db etc and close page
await Navigation.PopModalAsync();
if (PopupClosed!=null)
{
PopupClosed();
}
}
private async void cancelClicked(object sender, EventArgs e)
{
await Navigation.PopModalAsync();
if (PopupClosed != null)
{
PopupClosed();
}
}
}
I put it on the button click event, maybe you can put on close or dispose event. Then here is implement
public partial class SelectSchool : ContentPage
{
public SelectSchool()
{
InitializeComponent();
#region toolbar
ToolbarItem tbi = null;
if (Device.OS == TargetPlatform.Android)
{
tbi = new ToolbarItem("+", "plus", async () =>
{
var target_page = new AddSchool();
target_page.PopupClosed += () => { /*Do something here*/ };
Navigation.PushModalAsync(target_page);
}, 0, 0);
}
ToolbarItems.Add(tbi);
#endregion
this.Title = "Select School";
}
}
Hope this help.
I create an extension method for this:
public static class DialogUtils
{
public static async Task ShowPageAsDialog(this INavigation navigation, Page page)
{
int pagesOnStack = navigation.NavigationStack.Count + 1;
var waitHandle = new EventWaitHandle(false, EventResetMode.AutoReset);
page.Disappearing += (s, e) =>
{
if (navigation.NavigationStack.Count <= pagesOnStack)
waitHandle.Set();
};
await navigation.PushAsync(page);
await Task.Run(() => waitHandle.WaitOne());
}
}
And I can use it:
private async void bShowDialogPage_Clicked(object sender, EventArgs e)
{
var page = new DialogPage();
await page.LoadData();
await Navigation.ShowPageAsDialog(page);
var result = page.PageResult;
}
It supports situations when dialog page show another page. I prefer NavigationStack instead ModalStack due to NavigationPage and BackButton.
Another way of accomplishing this is by calling an event from the page's OnDisapearing method, this event can then be subscribed by the navigation service which you create, and you can then use the "TaskCompletionSource" to wati until your page finishes its work and then complete the task.
For more details about accomplishing this, you can check this blog post.
Here is the base page's implementation, every page in this demo app inherit this page:
public class BasePage<T> : ContentPage
{
public event Action<T> PageDisapearing;
protected T _navigationResut;
public BasePage()
{
}
protected override void OnDisappearing()
{
PageDisapearing?.Invoke(_navigationResut);
if (PageDisapearing != null)
{
foreach (var #delegate in PageDisapearing.GetInvocationList())
{
PageDisapearing -= #delegate as Action<T>;
}
}
base.OnDisappearing();
}
}
Here is an overview of the navigation service you should use:
public async Task<T> NavigateToModal<T>(string modalName)
{
var source = new TaskCompletionSource<T>();
if (modalName == nameof(NewItemPage))
{
var page = new NewItemPage();
page.PageDisapearing += (result) =>
{
var res = (T)Convert.ChangeType(result, typeof(T));
source.SetResult(res);
};
await App.Current.MainPage.Navigation.PushModalAsync(new NavigationPage(page));
}
return await source.Task;
}
To call this page with the navigation service, you can use the following code:
var item = await new SimpleNavigationService().NavigateToModal<Item>(nameof(NewItemPage));
Items.Add(item);
I have many controls in a window. Requirement is to know which control gets the focus from the lost focus event of a control.
Say, A Text box and it has the focus. Now I am clicking a button. while doing this, need to know that i am moving the focus to button from the Text box lost focus event.
So how could i achieve this..
This is what I did and its working for me
protected override void OnPreviewLostKeyboardFocus(KeyboardFocusChangedEventArgs e)
{
lostFocusControl = e.OldFocus;
}
private void PauseBttn_PreviewKeyDown(object sender, KeyEventArgs e)
{
/**invoke OnPreviewLostKeyboardFocus handller**/
}
Hope it will help
You can use FocusManager to handle this,
In your LostFocusEvent, Use FocusManager.GetFocusedElement()
uiElement.LostFocus+=(o,e)=>
{
var foo=FocusManager.GetFocusedElement();
}
The following class watches the FocusManager for changes in focus, it's a looped thread so you have to put up with the fact that it's running but when focus changes it will just raise an event letting you know what changed.
Just add these two classes to your project.
public class FocusNotifierEventArgs : EventArgs
{
public object OldObject { get; set; }
public object NewObject { get; set; }
}
public class FocusNotifier : IDisposable
{
public event EventHandler<FocusNotifierEventArgs> OnFocusChanged;
bool isDisposed;
Thread focusWatcher;
Dispatcher dispatcher;
DependencyObject inputScope;
int tickInterval;
public FocusNotifier(DependencyObject inputScope, int tickInterval = 10)
{
this.dispatcher = inputScope.Dispatcher;
this.inputScope = inputScope;
this.tickInterval = tickInterval;
focusWatcher = new Thread(new ThreadStart(FocusWatcherLoop))
{
Priority = ThreadPriority.BelowNormal,
Name = "FocusWatcher"
};
focusWatcher.Start();
}
IInputElement getCurrentFocus()
{
IInputElement results = null;
Monitor.Enter(focusWatcher);
dispatcher.BeginInvoke(new Action(() =>
{
Monitor.Enter(focusWatcher);
results = FocusManager.GetFocusedElement(inputScope);
Monitor.Pulse(focusWatcher);
Monitor.Exit(focusWatcher);
}));
Monitor.Wait(focusWatcher);
Monitor.Exit(focusWatcher);
return results;
}
void FocusWatcherLoop()
{
object oldObject = null;
while (!isDisposed)
{
var currentFocus = getCurrentFocus();
if (currentFocus != null)
{
if (OnFocusChanged != null)
dispatcher.BeginInvoke(OnFocusChanged, new object[]{ this, new FocusNotifierEventArgs()
{
OldObject = oldObject,
NewObject = currentFocus
}});
oldObject = currentFocus;
}
}
Thread.Sleep(tickInterval);
}
}
public void Dispose()
{
if (!isDisposed)
{
isDisposed = true;
}
}
}
Then in your code behind, create a new instance of the Focus Notifier class and hook on to it's OnFocusChanged event, remember to dispose it at the end or the thread will keep your app open.
public partial class MainWindow : Window
{
FocusNotifier focusNotifier;
public MainWindow()
{
InitializeComponent();
focusNotifier = new FocusNotifier(this);
focusNotifier.OnFocusChanged += focusNotifier_OnFocusChanged;
}
void focusNotifier_OnFocusChanged(object sender, FocusNotifierEventArgs e)
{
System.Diagnostics.Debug.WriteLine(e.OldObject);
System.Diagnostics.Debug.WriteLine(e.NewObject);
}
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
focusNotifier.Dispose();
base.OnClosing(e);
}
}
have you tried to register your controls to Control.LostFocus event and there you can check for Form.ActiveControl, to determine which control currently has the focus
I am using ListViewCollection class with my dataGrid. The underlying collection is an observable collection.
Whenever i call Move methods in the collection ( which is in ViewModel), the CurrentChanged Event doesnt fire.
However when UI calls the same method on it ( i can see it in the call stack), the event does fire.
this.EmailTemplates = new ListCollectionView(templateVmList);
this.EmailTemplates.CurrentChanging += (o, e) => EmailTemplates_CurrentChanging(o, e);
this.EmailTemplates.CurrentChanged += (o, e) => { this.SelectedEmailTemplate = (EmailTemplateViewModel)this.EmailTemplates.CurrentItem; };
if (this.EmailTemplates.Count > 0)
{
if (!this.EmailTemplates.MoveCurrentToFirst())
throw new ArgumentException("Element not found in collection");
}
What should i do in code to make sure the events fire no matter who is changing the collection.
Try using CollectionViewSource.GetDefaultView instead of creating a new ListCollectionView.
This test code worked fine for me
public class LcViewModel : BaseItemsViewModel
{
public LcViewModel()
{
MoveCommand = new RelayCommand(Move);
var view = CollectionViewSource.GetDefaultView(Items);
view.CurrentChanged += (sender, args) => Debug.WriteLine("CurrentChanged");
view.CurrentChanging += (sender, args) => Debug.WriteLine("CurrentChanging");
}
public ICommand MoveCommand { get; private set; }
private void Move()
{
var view = CollectionViewSource.GetDefaultView(Items);
view.MoveCurrentToFirst();
}
}