I've got a Xamarin.Forms WPF application running, but there is a huge title bar.
I have tried the solution of adding NavigationPage.SetHasNavigationBar(this, false); inside my MainPage class's constructor
Here is the code:
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
NavigationPage.SetHasNavigationBar(this, false);
}
}
Still I am getting huge title bar size in WPF application, on the other hand, while running on UWP, it is working fine.
You could try to add below codes in your MainWindow.xaml.cs in wpf project:
protected override void OnActivated(EventArgs e)
{
base.OnActivated(e);
if (!topBarsRemoved) RemoveTopBars();
}
private bool topBarsRemoved = false;
private void RemoveTopBars()
{
var topAppBar = this.Template.FindName("PART_TopAppBar", this) as FormsAppBar;
if (topAppBar != null)
(topAppBar.Parent as System.Windows.Controls.Grid)?.Children.Remove(topAppBar);
topBarsRemoved = true;
}
Related
I'm working with C# xamarin forms android and I'm trying to close my sidebar when the user touches de backbuttom but I'm not able to do it. Instead, my aplications closeses.
HomePage.xaml.cs code:
public partial class HomePage : ContentPage
{
string[] subs;
public HomePage()
{
//NavigationPage.SetHasBackButton(this, false);
InitializeComponent();
}
protected override bool OnBackButtonPressed()
{
return true;
}
}
my sidebar AppShell.xaml.cs code:
public partial class AppShell : Xamarin.Forms.Shell
{
public AppShell()
{
InitializeComponent();
nameuser.Title = Preferences.Get("displayName", "Default");
versionadoAPP.Text = "v" + $"{VersionTracking.CurrentBuild}.{VersionTracking.CurrentVersion}";
}
protected override bool OnBackButtonPressed()
{
return true;
}
}
Image of my app:
How could I resolve that?
Thank tou very much!
The easiest way that do this is to set the flyout presented property.
Try something like this:
Shell.Current.FlyoutIsPresented = false;
Good luck!
I'm a new to the MVVMCross package, and C# for that matter. I've spent the better part of the day trying to figure out what I'm not understanding reading the documentation on presenters and navigation, etc. in order to try to understand, but I'm missing something.
I originally created a WPF app not implementing MVVM and now I wanted to convert, but I'm struggling with this part. I want to have a Main Menu that is part of a grid in a "MainWindow" like shell where the remaining portion of the page (and grid column 2) are used to display a nested view.
Ultimately, I’m just trying to reproduce the same layered controls in the original WPF application. In that app there is a content control Which takes up most of the form whose content property is set to a different form depending on the users selection.
MainWindow.xaml.cs
public partial class MainWindow : MvxWindow
{
public MainWindow(IMvxNavigationService navService)
{
InitializeComponent();
DataContext = new MainViewModel(navService);
//content.Content = new AdminMenuView();
}
}
MainViewModel.cs
private MvxViewModel _nextMenuContent;
public MainViewModel(IMvxNavigationService navService)
{
_navService = navService;
MoveMenuCommand = new MvxCommand(MoveMenu);
ChildViewModel = new AdminMenuViewModel();
GoToAdminMenu = new MvxCommand(SelectAdminMenu);
}
MainView.xaml
<ContentControl Content="{Binding ChildViewModel}"/>
***The grid and columns are all working fine
MainView.xaml.cs
public partial class MainView : MvxWpfView
{
public MainView()
{
InitializeComponent();
}
}
AdminMenuModel.cs
public class AdminMenuViewModel : MvxViewModel
{
private readonly IMvxNavigationService _navService;
public AdminMenuViewModel()
{
Initialize();
}
public override void Prepare()
{
base.Prepare();
}
public override async Task Initialize()
{
await base.Initialize();
}
}
AdminMenuModel.xaml.cs
public partial class AdminMenuView : MvxWpfView
{
public AdminMenuView()
{
InitializeComponent();
}
public new AdminMenuViewModel ViewModel
{
get { return base.ViewModel as AdminMenuViewModel; }
set { base.ViewModel = value; }
}
}
When I call the AdminMenuViewModel it runs, but all I get in the content control is either a blank screen if I Bind the "ChildViewModel" to the DataContext property of the content control and a string of the path to the AdminMenuViewModel if I bind it to the content property.
You have to set MainViewModel as DataContext of your main window
public MainWindow(IMvxNavigationService navService)
{
DataContext = new MainViewModel(navService);
InitializeComponent();
}
I am trying to prevent the user of my app from pressing the hardware back button.
I have found this code snippet that is in the code behind the xaml file:
protected override bool OnBackButtonPressed()
{
return true;
}
I have tried variations of this including using Boolean instead of Bool and returning base.functionname nothing seems to fire this method.
Here is the bigger context:
Code behind:
namespace Watson.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class StartScan : ContentPage
{
public StartScan()
{
InitializeComponent();
}
protected override bool OnBackButtonPressed()
{
return true;
}
}
}
This is the second page in the stack and disabling the button only needs to happen on this page only, no where else.
Any help would be appreciated.
This is working for me, I tried in Android and iOS platforms using Xamarin.Forms.
Hope you can resolve with this piece of code.
namespace Test
{
public partial class TestPage2 : ContentPage
{
public TestPage2()
{
NavigationPage.SetHasBackButton(this, false);
InitializeComponent();
}
protected override bool OnBackButtonPressed()
{
//return base.OnBackButtonPressed();
return true;
}
}
}
Thanks,
You can also add NavigationPage.SetHasBackButton property in the XAML
NavigationPage.HasBackButton="True"
In the Content Page
You can do this way:
namespace Watson.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class StartScan : ContentPage
{
public StartScan()
{
NavigationPage.SetHasBackButton(this, false);
InitializeComponent();
}
protected override bool OnBackButtonPressed()
{
// you can put some actions here (for example, an dialog prompt, ...)
return true;
}
}
}
For me it is working with the following code (Samsung Android)
protected override bool OnBackButtonPressed()
{
//base.OnBackButtonPressed();
browser.GoBack();
return true;
}
OnBackButtonPressed firing when you click the back button on your device, not from your navigation page, do your logic in OnDisappearing!
I am trying to Remove() or Clear() ToolbarItems. Here is my code where I am creating ToolbarItem in MainPage.cs
public partial class MainPage : MasterDetailPage
{
public ToolbarItem cCounter = new ToolbarItem() { Icon = "picture.png" };
public ToolbarItem pPo = new ToolbarItem() { Text = "-" };
public MainPage()
{
InitializeComponent();
if (Device.OS == TargetPlatform.iOS)
{
provider.Clicked += iOS_Ppo_Clicked;
ToolbarItems.Add(cCounter);
ToolbarItems.Add(pPo);
}
}
private void iOS_Ppo_Clicked(object sender, EventArgs e)
{
OpenWindow();
}
public async void OpenWindow()
{
if (await Common.WindowComands.CanOpenWindow<PPoPage>(Detail))
{
page = new PPoPage(Page3.Allproviders);
this.ToolbarItems.Clear(); // here I am getting error:Index was outside the bounds of the array
page.OnSelected += Page_OnSelected;
await Detail.Navigation.PushAsync(page, false);
}
}
}
Edit: when I included this.ToolbarItems.Clear(); in OpenWindow
method which initialise that another page opens and it works! Cleans
all toolbar items but unfortunately shows this error:
System.IndexOutOfRangeException: Index was outside the bounds of the array.
This items should disappear only for iOS as you see.
Here is my page class where I would like to Remove() these ToolbarItems:
public partial class PPoPage : ContentPage
{
public MainPage main { get; set; }
private List<PPo> Pro;
public PPoPage(List<PPo> po)
{
InitializeComponent();
if (Device.OS == TargetPlatform.iOS)
{
Pro = po;
CreateLayout();
// HERE I WANT TO REMOVE TOOLBARITEMS FOR THIS PAGE
this.ToolbarItem.Remove(main.cCounter); // here there is error
this.ToolbarItems.Clear(); // this also doesn't work, because toolbar items still exist after this initialization.
}
}
}
In this class I tried both approaches, but none work. Thank you for answers or suggestions.
I had task to change visibility of toolbar item and I created base content page with wrapper around ToolbarItems collection. My sample could help you:
sample on git
some "tutorial" that I've posted on CodeReview
First of all I really want to thank for all answers and suggestions I tried to implement both answers on my solution, but unfortunately unsuccessfully. Main reason that I wasn't focusing on class where I am implemented whole iOS toolbar and it was on my iOS project.
On iOS toolbar I just wrote simple if statement:
var currentPage = ((NavigationPage)Element).CurrentPage;
if (currentPage != null && currentPage is ContentPage)
{
TopViewController.NavigationItem.RightBarButtonItems = new UIBarButtonItem[0];
return;
}
The problem here is that PPoPage is a different page instance to MainPage, and thus contains its' own ToolbarItems collection.
Try main.ToolbarItems.Clear() in place of this.ToolbarItems.Clear(), assuming the main property has been initialised, or you can access the top-level current page with Xamarin.Forms.NavigationPage.CurrentPage.
I have a MainPage, which is the first one opened, when the Windows Phone Application is launched. If the settings is empty, I want to navigate to Settings Page. By the way it seems I can't do it in the builder. This is the C# code:
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
this.NavigationCacheMode = NavigationCacheMode.Required;
checkSettings();
}
private void checkSettings()
{
var applicationData = Windows.Storage.ApplicationData.Current;
var roamingSettings = applicationData.RoamingSettings;
string surname = (string)roamingSettings.Values["surname"];
if (surname == null)
{
Frame.Navigate(typeof(Settings));
}
else
{
}
}
And this what I got when I executed the application:
System.NullReferenceException
Message=Object reference not set to an instance of an object.
What do you think is the problem ? Is there any other solution can I do, to navigate to the other page ? Thank you
UPDATE with the full code WORKS !
I finally found the solution, with the load page handler. Now everything works !
private void check_action(object sender, RoutedEventArgs e)
{
checkSettings();
}