Can you create an event in a user control, if so, how? - silverlight-4.0

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!

Related

How to access code behind from Tablet/Phone template?

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...

Registering RoutedEventHandler in UWP template control

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.

Event for cell add(jgraphx)

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,

XAML navigation in Windows Store Grid App

I trying to navigate between XAML pages in grid app. for this i have added a added a new XAML and a button in XAML and cs code for button event is
private void OnClick(object sender, RoutedEventArgs e)
{
Frame.Navigate(typeof (GroupedItemsPage));
}
on clicking this button my app should navigate to GroupedItemsPage, but instead it raises an null exception
try this :
private void OnClick(object sender, RoutedEventArgs e)
{
this.Frame.Navigate(typeof (GroupedItemsPage));
}
Please have a look there, too.

How to fire a button Click event in code behind in user control?

is it possible to fire a button Click event in code behind in user control?
In C# events can only be invoked from the class that declares them. In case of the Button there is a method called OnClick which raises the ClickEvent but it is protected. So you need to declare class that inherits from Button and change the visibility of OnClick method (or declare some over method that calls base.OnClick)
public class MyButton : Button
{
public new void OnClick()
{
base.OnClick();
}
}
Example of XAML
<StackPanel Background="White" >
<my:MyButton x:Name="TestButton" Click="HandleClick" Content="Test" />
<TextBlock x:Name="Result" />
</StackPanel>
And code behind:
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
new Timer(TimerCall,null,0,1000);
}
private void TimerCall(object state)
{
Dispatcher.BeginInvoke(()=>TestButton.OnClick());
}
private void HandleClick(object sender, RoutedEventArgs e)
{
Result.Text = String.Format("Clicked on {0:HH:mm:ss}",DateTime.Now);
}
}
Though it is always easier to call event handler directly.
HandleClick(this,null)
Then there will be no need for extra plumbing.