How can I navigate the blazor pages with the press of a xaml button? - blazor-server-side

So here is my generally idea:
I have a flyout page in which i want to press different buttons to load different blazor pages in the flyout.details. The goal is to replace the default navbar of the blazor example with native elements of the different frameworks in the .NET MAUI XAML. (Image) My first thought was accessing the navigationManager of the blazor pages but no idea how. Does that even makes sense? I feel like I am missing sth very crucial because it seems to be a very natural thing to do with .net Maui Blazor.
Important to understand is that i want to stay on the same xaml page. I just want to change the loaded Blazor page without the need to define the nav at in the Blazor page.
I tried to access it via injection,
which kinda confused me but tried it any ways (got it from a forum). I feel like I am not understanding something fundamental.
XAML:
<FlyoutPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MauiTest"
x:Class="MauiTest.MainPage"
BackgroundColor="{DynamicResource PageBackgroundColor}">
<FlyoutPage.Flyout>
<ContentPage Title="FlyoutMenu">
<VerticalStackLayout>
<Label x:Name="TestLabel"
Text="Welcome to .NET MAUI!"
VerticalOptions="Center"
HorizontalOptions="Center" />
<Button Text="Counter" Clicked="OnCounterClicked" x:Name="Counter" />
<Button Text="Test" Clicked="OnTestButtonClicked" x:Name="TestButton"/>
<WebView BackgroundColor="red" Source="http://0.0.0.0/app/counter" />
</VerticalStackLayout>
</ContentPage>
</FlyoutPage.Flyout>
<FlyoutPage.Detail>
<ContentPage>
<BlazorWebView x:Name="blazorWebView" HostPage="wwwroot/index.html">
<BlazorWebView.RootComponents>
<RootComponent Selector="#app" ComponentType="{x:Type local:Main}" />
</BlazorWebView.RootComponents>
</BlazorWebView>
</ContentPage>
</FlyoutPage.Detail>
For the methode behind it I don't really have any idea...
C#:
private void OnTestButtonClicked(object sender, EventArgs e)
{
//Navigating to a site
}
Don't let yourself irritate from the other buttons: I am playing around.

To replace the nav bar
Maui Blazor have designed the structure for us. The easiest way i think is to modify it.
The NavMenu.razor (default blazor template) define the appearance and structure of the navigation bar. You could modify it.
<div class="sidebar">
<NavMenu />
</div>
Another way is that you do it like .net maui FlyoutPage. In MainPage
<FlyoutPage.Flyout>
<MauiPages:FlyoutMenuPage x:Name="flyoutPage" />
</FlyoutPage.Flyout>
<FlyoutPage.Detail>
<NavigationPage>
<x:Arguments>
<local:ContactsPage />
</x:Arguments>
</NavigationPage>
</FlyoutPage.Detail>
I have tested it and it worked well just like the official docs.
For more info, you could refer to FlyoutPage and for sample code Xamarin flyout doc could be help
2.I don't think we could navigate from a maui page to razor component as their route ways are much different. For a walkaround, you could wrap razor component in a maui contentpage.
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MauiBlazor"
>
<BlazorWebView HostPage="wwwroot/index.html">
<BlazorWebView.RootComponents>
<RootComponent Selector="#app" ComponentType="{x:Type local:Main}" />
</BlazorWebView.RootComponents>
</BlazorWebView>
</ContentPage>
And then use maui navigation:
App.Current.MainPage.Navigation.PushAsync(new MauiPages.MyPage());
========origin answer============
Seems you want to close the sidebar created by Blazor. That's MainLayout.razor in Shared folder. You could simply remove the following line:
<div class="sidebar">
<NavMenu />
</div>
Then the default sidebar would disappear.
if you want navigate, you could try the following:
App.Current.MainPage.Navigation.PushAsync(new MauiPages.MyPage());
Hope it works for you.

Related

Is it possible to detect where the swipe happens in Net MAUI?

I'm writing a .Net MAUI application and am using SwipeGestureRecognizer. This is all working great. But what I can seem to find is if you can detect where the swipe occurred.
.xaml
<Grid x:Name="myGrid" >
<Grid.GestureRecognizers>
<SwipeGestureRecognizer Direction="Right" Swiped="SwipeGestureRecognizer_Swiped" />
</Grid.GestureRecognizers>
...
.xaml.cs
private void SwipeGestureRecognizer_Swiped(object sender, SwipedEventArgs e)
{
...
}
But I specifically want to detect if you swipe in from an edge, primarily so I can show a popout menu on the edge. I can see nothing in the event args (of the Swiped event that I could use to detect where the swipe happened.
Consider the image attached. How can I tell (or detect) the difference between the wholly on-screen swipe action (green) from the off-screen swipe action (red). Some location parameters would be perfect, but they don't seem to exist.
I can't see anything in the MAUI docs to suggest this is even possible
Does anyone know if there is any way to achieve this, please?
Thanks?
It's not an elegant solution but you can place an invisible BoxView on the left:
<Grid>
<BoxView
HorizontalOptions="Start"
VerticalOptions="Center"
WidthRequest="40"
Color="Transparent">
<BoxView.GestureRecognizers>
<SwipeGestureRecognizer
Direction="Right"
Swiped="SwipeGestureRecognizer_Swiped"
Threshold="25" />
</BoxView.GestureRecognizers>
</BoxView>
<!-- your content -->
<VerticalStackLayout Margin="15,0,0,0">
<Button />
<Label Text="label" />
</VerticalStackLayout>
</Grid>
I have decreased the value of Threshold because the swipable distance is small.

Maui framework doesn't display sliders correctly

I have been dipping my toes in the .NET Maui framework, but unfortunately so far it has been quite disappointing. There seem to be multiple errors related to Shell (title view not working properly) and controls themselves. I have the following main page xaml file:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MauiApp1.MainPage">
<StackLayout Background="#111111" >
<Frame HasShadow="True" Margin="10" CornerRadius="10" BackgroundColor="#222222">
<StackLayout Spacing="30">
<Grid ColumnDefinitions="30,*">
<Label Text="R" FontSize="Medium" TextColor="White" />
<Slider GridLayout.Column="1" MinimumTrackColor="Red" ThumbColor="Red" MaximumTrackColor="#AAAAAA" />
</Grid>
<Grid ColumnDefinitions="30,*">
<Label Text="G" FontSize="Medium" TextColor="White" />
<Slider GridLayout.Column="1" MinimumTrackColor="#7FFF00" ThumbColor="#7FFF00" MaximumTrackColor="#AAAAAA" />
</Grid>
</StackLayout>
</Frame>
</StackLayout>
</ContentPage>
which is just a stack layout with a single frame that houses two sliders with labels. However, the two sliders seem to have a predefined width, as they are not filling the remaining space:
As soon as I add anything to any of the controls in the xaml while the app is running, for example a green background to the first grid, both sliders resize to the correct size:
However, when I reload the application with this change, the sliders go back to the wrong size:
There are additional issues that I encountered as well, when placing this content page as a flyout item in the shell where android behaviour was as described here, but additionally the Sliders are not interactable in Windows version. In this example with a clean project they do work in Windows version.
Try with any third-party packages like syncfusion, etc. to meet your requirements.

Line break after back button - Xamarin Forms

I am new to Xamarin forms and have directed to a navigation page from my main page, I'd like the title to appear on the same line as the back button but it's currently appearing on the line below, like this -
https://i.imgur.com/lpsJjle.png
I am using the following code to display the title -
<NavigationPage.TitleView>
<StackLayout>
<Label
Text="About" TextColor="{StaticResource lightGreen}">
<Label.FontFamily>
<OnPlatform x:TypeArguments="x:String"
Android="GothamRoundedMedium.ttf#GothamRoundedMedium"
iOS="GothamRoundedMedium">
</OnPlatform>
</Label.FontFamily>
</Label>
</StackLayout>
</NavigationPage.TitleView>
and this code to navigate to the about page -
Navigation.PushAsync(new NavigationPage(new Survey2()));
Any help would be much appreciated!
With new NavigationPage you are again creating a new NavigationPage.
So,just use:
Navigation.PushAsync(new Survey2());
If you need to use Modal,use this:
Navigation.PushModalAsync(new NavigationPage(new Survey2()));

Xamarin.forms content page layout

I have content page in my xaml file. I have a Stacklayout with orientation vertical in it. I want to put a dialog in middle of content page above this stacklayout. Please suggest some way. Thanks in advance!
An easy way to provide dialogs that feel native to the platform your on is through using the UserDialogs plugin.
If you want to create something of your own you would probably have to come up with something like this:
<Grid>
<StackLayout>
...
</StackLayout>
<Grid IsVisible="{Binding ShowMessage}">
<Label Text="Message" HorizontalOptions="Center" VerticalOptions="Center" />
</Grid>
</Grid>
The nested Grid will act as an overlay of sorts spanning your entire page.

Add Map to Master detail Page returns Exception

i am using xamarin forms(PCL) and execute the App using Windows phone emulator
i created a master detail page
<MasterDetailPage.Master>
<ContentPage Title="Menu">
<StackLayout Orientation="Vertical">
<Button Text="buttonOne"/>
<Button Text="ButtonTwo"/>
<Button Text="ButtonThree"/>
</StackLayout>
</ContentPage>
</MasterDetailPage.Master>
<MasterDetailPage.Detail>
<NavigationPage>
<x:Arguments>
<local:MasterPage />
</x:Arguments>
</NavigationPage>
</MasterDetailPage.Detail>
the details page is
<StackLayout>
<Label Text="Hello World"/>
<StackLayout>
<maps:Map WidthRequest="320" HeightRequest="200" />
</StackLayout>
</StackLayout>
it stops at
#if DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
UnhandledException += (sender, e) =>
{
if (global::System.Diagnostics.Debugger.IsAttached)
global::System.Diagnostics.Debugger.Break();
};
#endif
and return this exception( Native View = Unable to evaluate the expression. Operation not supported. Unknown error: 0x80070057.)
Note:
if i remove the WidthRequest and HeightRequest of the map it works
but the map would not appear.
And this Exception Takes place only when navigate to the master
detail page.
So this resolves to not being a problem with the Maps part specifically but an issue with Navigating to Master Detail pages, since PushModalAsync(...) puts the Master Detail Page on top of a new Navigation stack instead of pushing it onto the top of the current Navigation stack which is what PushAsync(...) does.
Here's a link to a Xamarin Forms Forum question that appears to have the same problem:
https://forums.xamarin.com/discussion/21461/navigation-pushasync-not-working
Apparently MasterDetail pages do not like being navigated to, and both Android and iOS prefer to have it as the root page. That isn't to say this is impossible to do, I believe it just has to be done a little differently. Some dependecy injection/custom rendering might help here.
I found this Xamarin Forms article very useful when learning about Navigation, might help you out as well:
https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/navigation/hierarchical/