XAML navigation in Windows Store Grid App - xaml

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.

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.

Textview Widget Mono

I started to program with monodevelop (gtk#).
I have a very simple question:
protected void OnButton1Clicked (object sender, EventArgs e)
{
textview1.Buffer.Text="hallooo";
}
I just want the textview widget to show the text above. But when I run the program nothing happens.
You need more code?

How can I navigate a page in Xamarin?

I have this code in Xaml :
<Button Text="Click Me" Clicked="OnButtonClicked" />
And this code in code-behind :
void OnButtonClicked(object sender, EventArgs args)
{
}
How can I navigate a page in this method ?
First, make sure that your app is based on a NavigationPage to enable navigation in Xamarin.Forms. For this, set the MainPage property of your App.cs to a NaviationPage instance.
public App()
{
MainPage = new NavigationPage(new YourStartPage());
}
Now add a second Forms Xaml Page or Forms ContentPage to your project, that you can navigate to from your start page. Make sure, your OnButtonClicked handler is declared as async (like in the sample below) and navigate like this:
async void OnButtonClicked(object sender, EventArgs args)
{
await Navigation.PushAsync(new Page2());
}

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

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!