I'm trying to go to another page when clicking a button. How could i do this (WinUI 3) - xaml

I'm trying to go to another page when clicking a button. How could i do this.
I tried using a NavigationViewItem but it didn't work.

The NavigationViewItem you posted is used in UWP.
If it is a WinUI3 project, then you need to refer to this document, use Navigate.
the relevant code samples are as follows.
xaml
<Button x:Name="myButton" Click="myButton_Click"/>
xaml.cs
private void myButton_Click(object sender, RoutedEventArgs e)
{
Frame rootFrame = new Frame();
this.Content = rootFrame;
rootFrame.Navigate(typeof(BlankPage1), null);
}

Related

Button color change on click or hover

I added a button component in my application, and I am expecting it to change the color when user hovers on it or click it.
But looks like that's not the default behavior of the button.
Is there a way to get that behavior maybe by setting some attribute?
You can simply use a event handler to set the color of the button when clicking it.
Here's the code sample below for your reference:
<Button Clicked="Button_Clicked">
Code in backend:
private void Button_Clicked(object sender, EventArgs e)
{
var button = (Button)sender;
button.BackgroundColor = Color.FromArgb("#FFA07A");
}
Update:
I found a simialr issue for your scenerio in Github, you can follow it up here:https://github.com/dotnet/maui/issues/9552

How to add button to pivot header

I want to add a button to the pivot header so that it is shown in-line with pivot item titles, but doesn't update the content shown but just launches an event instead.
After some experimenting, I discovered how to do this. First place a button inside a PivotItem's header. Set it up to route tap events as well as click events.
<PivotItem>
<PivotItem.Header>
<Button
x:Name="MyButton"
Tapped="PivotItem_Tapped"
Click="MyButton_Click">MyButton</Button>
</PivotItem.Header>
</PivotItem>
Then inside the handler for the tap, consume the tap event. Clicking on the button won't update the pivot's content now:
private void MyButton_Click(object sender, RoutedEventArgs e)
{
//do whatever you need.
}
private void PivotItem_Tapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)
{
e.Handled = true;
}

UWP - Close my content dialog on click on the blank area

I have created a content dialog for my UWP app which involves a centralized UI Element and a surrounding blank area.But content dialog does not have a property like "IsLightDismissEnabled" to close the dialog on click on an area except the UIELEMENT area.How can I achieve it?
In the code behind your content dialog:
public sealed partial class CustomDialog : ContentDialog
{
public CustomDialog()
{
this.InitializeComponent();
Boolean isHide;
Window.Current.CoreWindow.PointerPressed += (s, e) =>
{
if (isHide)
Hide();
};
PointerExited += (s, e) => isHide = true;
PointerEntered += (s, e) => isHide = false;
}
}
There are few options that I can think of:
Use popup (like uruk suggested) and add your controls inside, create the popup at desired location (you could also use binding here if you want to show the popup at location depending on user input at runtime Popup has HorizontalOffset and VerticalOffset properties)
Create a parent view that is taking up the whole page but is transparent, then add your UI elements at the center and attach tap/click events to the transparent view. These events are going to just close remove/collapse the transparent view which contains the other elements inside (Either by binding of values or by setting the values to the UI elements).
Example or popup usage:
<Popup x:Name="MenuPopUp"
IsLightDismissEnabled="True"
HorizontalOffset="{Binding HorizontalOffset}"
VerticalOffset="{Binding VerticalOffset}"
IsOpen="{Binding IsOpen, Mode=TwoWay}">
<Grid>
YOUR ELEMENTS HERE
</Grid>
</Popup>
Content dialog is a modal dialog. Why don't you use a Popup or a child class of it? It's non-modal, and it already has the IsLightDismissEnabled property you just mentioned.
<Popup x:Name="MenuPopUp"
IsLightDismissEnabled="True"
LostFocus="MenuPopUp_LostFocus"/>
In CS
private void MenuPopUp_LostFocus(object sender, RoutedEventArgs e)
{
MenuPopup.IsOpen = false;
}

wp7 handling application bar button click dynamically in pivots

i have a pivot page with 3 pages and there are two application bar buttons. But i want that when pivot is changed, the first button of application bar should do different tasks on different pivot, and second button will do same task on all pivots. I am doing this as:
private void PivotControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ApplicationBarIconButton firstButton = (ApplicationBarIconButton)ApplicationBar.Buttons[0];
if (PivotControl.SelectedIndex == 0)
{
firstButton.IsEnabled = true;
firstButton.Click += new EventHandler(FirstPivotButton_Click);
}
else if (PivotControl.SelectedIndex == 1)
{
firstButton.IsEnabled = true;
firstButton.Click += new EventHandler(SecondPivotButton_Click);
}
else if (PivotControl.SelectedIndex == 2)
{
firstButton.IsEnabled = false;
}
}
void FirstPivotButton_Click(object sender, EventArgs e)
{
NavigationService.Navigate(new Uri("/PageA.xaml", UriKind.Relative));
}
void SecondPivotButton_Click(object sender, EventArgs e)
{
NavigationService.Navigate(new Uri("/PageB.xaml", UriKind.Relative));
}
But the problem is that PageA is being navigated well but there is problem in going to PageB from secondpivotbutton click event. Please help me
Use this great library for simplifying usage of ApplicationBar on Windows Phone - AppBarUtils.
You can find them on NuGet as well.
There is also nice tutorial, how ti display different buttons for each panorama/pivot item using this library:
http://allenlooplee.wordpress.com/2012/09/17/how-to-show-different-app-bar-for-different-pivotpano-item/
Here's how I did this in one of my apps. Button 1 is different for pivot pages and button 2 is always the same, just like you.
On the SelectionChanged event, update the button icon uri and text:
Private Sub statPivot_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles statPivot.SelectionChanged
Dim saveBtn As ApplicationBarIconButton = ApplicationBar.Buttons(0)
If statPivot.SelectedIndex = 0 Then
'calculation pane active
saveBtn.Text = "save"
saveBtn.IconUri = New Uri("/Assets/AppBar/appbar.save.rest.png", UriKind.Relative)
Else
'history pane active
saveBtn.Text = "clear"
saveBtn.IconUri = New Uri("/Assets/AppBar/appbar.refresh.rest.png", UriKind.Relative)
End If
End Sub
then in the onclick event I detect the pivot pane that's being viewed and use a basic if...then...else... to run the different code within the same handler.
If you want dynamically change app bar buttons at run time you can do this:
<phone:PhoneApplicationPage.Resources>
<shell:ApplicationBar x:Key="appbar1" IsVisible="True">
<shell:ApplicationBarIconButton x:Name="abMain1" IconUri="/icons/appbar.favs.addto.rest.png" Text="blabla1"/>
</shell:ApplicationBar>
<shell:ApplicationBar x:Key="appbar2" IsVisible="True">
<shell:ApplicationBarIconButton x:Name="abMain2" IconUri="/icons/appbar.favs.addto.rest.png" Text="blabla2"/>
<shell:ApplicationBarIconButton x:Name="abMain3" IconUri="/icons/appbar.cancel.rest.png" Text="blabla3"/>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.Resources>
And then change it from code:
ApplicationBar = (Microsoft.Phone.Shell.ApplicationBar) Resources["appbar1"];
and
ApplicationBar = (Microsoft.Phone.Shell.ApplicationBar) Resources["appbar2"];
You can big numbers of different AppBars.
Hope its help.

is it possible to change Button text color in c++ cx metro app?

I have the following XAML code
XAML:
<Button x:Name = "Btn1" Click = "Button_Click">
can i change the color of the font in the Button_Click method or anywhere else in code?
You can implement it by the following code runs in directX 2013:
void App2::DirectXPage::Button_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e) {
bt->Foreground = ref new SolidColorBrush(Windows::UI::Colors::Blue);
}
Yes you can change the color of the button any where in the code
Here's a sample button and a sample code to change color in it's click event
Heres the code to change the color in the click event of the btnChangeFontColor
private void Button_Click_1(object sender, RoutedEventArgs e)
{
btnChangeFontColor.Foreground = new SolidColorBrush(Colors.Red);
}