How to create a page in Xamarin? - xamarin

i have below question on page creation, i received nothing after the break. Please help.
private void ItemAction(DashboardMultipleTileItemData item)
{
switch (item.Title)
{
case "Contacts":
new NavigationPage(new TabControlAndroidSamplePage());
break;
case "Documents":
Application.Current.MainPage.Navigation.PushAsync(new EmployeePerformanceDashboardPage());
break;
//case "Enquiry":
// Application.Current.MainPage.Navigation.PushAsync(new WebSitePage()));
// break;
}
}

The Solution will be:-
private void ItemAction(DashboardMultipleTileItemData item)
{
switch (item.Title)
{
case "Contacts":
Application.Current.MainPage = new NavigationPage(new TabControlAndroidSamplePage());
break;
case "Documents":
Application.Current.MainPage.Navigation.PushAsync(new EmployeePerformanceDashboardPage());
break;
//case "Enquiry":
// Application.Current.MainPage.Navigation.PushAsync(new WebSitePage()));
// break;
}
}

Related

Access current MudBlazor breakpoint in code

I want to be able to set a property of a MudBlazor Date Picker component based on breakpoint. I can only think of having 2 versions and hiding one or the other but I am hoping there is a cleaner way.
And more generally is there an easy way to detect in the #code section what breakpoint the user is in?
You can use IBreakpointService
here is an example of changing the DateTime picker value by changing the breakpoint.
https://try.mudblazor.com/snippet/GkwcklvshoBJfwUS
#using MudBlazor.Services
<MudCard Class="pa-5">
<MudDatePicker #bind-Date="_dateTime"/>
</MudCard>
#code {
private DateTime? _dateTime { get; set; } = DateTime.Now;
[Inject] IBreakpointService BreakpointListener { get; set; }
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await BreakpointListener.Subscribe(breakpoint =>
{
switch (breakpoint)
{
case Breakpoint.Xs:
_dateTime = DateTime.Now.AddDays(1);
break;
case Breakpoint.Sm:
_dateTime = DateTime.Now.AddDays(2);
break;
case Breakpoint.Md:
_dateTime = DateTime.Now.AddDays(3);
break;
case Breakpoint.Lg:
_dateTime = DateTime.Now.AddDays(4);
break;
case Breakpoint.Xl:
_dateTime = DateTime.Now.AddDays(5);
break;
case Breakpoint.Xxl:
_dateTime = DateTime.Now.AddDays(6);
break;
case Breakpoint.SmAndDown:
_dateTime = DateTime.Now.AddDays(7);
break;
case Breakpoint.MdAndDown:
_dateTime = DateTime.Now.AddDays(8);
break;
case Breakpoint.LgAndDown:
_dateTime = DateTime.Now.AddDays(9);
break;
case Breakpoint.XlAndDown:
_dateTime = DateTime.Now.AddDays(10);
break;
case Breakpoint.SmAndUp:
_dateTime = DateTime.Now.AddDays(11);
break;
case Breakpoint.MdAndUp:
_dateTime = DateTime.Now.AddDays(12);
break;
case Breakpoint.LgAndUp:
_dateTime = DateTime.Now.AddDays(13);
break;
case Breakpoint.XlAndUp:
_dateTime = DateTime.Now.AddDays(14);
break;
case Breakpoint.None:
_dateTime = DateTime.Now.AddDays(15);
break;
case Breakpoint.Always:
_dateTime = DateTime.Now.AddDays(16);
break;
}
InvokeAsync(StateHasChanged);
});
StateHasChanged();
}
await base.OnAfterRenderAsync(firstRender);
}
}

Rootpage error xamarin forms

My root page is giving error by clicking on the back, instead of being in the background, an unhandled error occurs "An unhandled exception occurred. Is something wrong with my command?
I made the following attempt, when I call other pages besides Rootpage, the error does not occur
public class RootPage : MasterDetailPage
{
Dictionary<MenuIndex, NavigationPage> Views { get; set; }
public RootPage()
{
Title = "teste";
Views = new Dictionary<MenuIndex, NavigationPage>();
Master = new MenuPage(this)
{
Icon = "ic_menu_white.png"
};
NavigateAsync(MenuIndex.Page1);
}
public void NavigateAsync(MenuIndex id)
{
Page newPage;
if (!Views.ContainsKey(id))
{
switch (id)
{
case MenuIndex.Page1:
Views.Add(id, new ControlNavigationApp(new Page1(), "#04518c"));
break;
case MenuIndex.Page2:
Views.Add(id, new ControlNavigationApp(new Page2(), "#04518c"));
break;
case MenuIndex.Page3:
Views.Add(id, new ControlNavigationApp(new Page3(), "#04518c"));
break;
default:
Views.Add(id, new ControlNavigationApp(new Page1(), "#04518c"));
break;
}
}
newPage = Views[id];
if (newPage == null)
return;
//pop to root for Windows Phone
if (Detail != null && Device.OS == TargetPlatform.WinPhone)
{
//await Detail.Navigation.PopToRootAsync();
}
Detail = newPage;
if (Device.Idiom != TargetIdiom.Tablet)
IsPresented = false;
}
//comand example
protected override bool OnBackButtonPressed()
{
return base.OnBackButtonPressed();
}
}

error in my rootpage xamarin forms

My Rootpage and the page that controls all my pages, and it is giving an unhandled error when closing, whenever a command to close the error, also when I receive a push notification and I will open the rootpage, an error occurs seconds later . Is something wrong with my command below?
public class RootPage : MasterDetailPage
{
Dictionary<MenuIndex, NavigationPage> Views { get; set; }
public RootPage()
{
Title = "App";
Views = new Dictionary<MenuIndex, NavigationPage>();
Master = new MenuPage(this)
{
Icon = "ic_menu_white.png"
};
NavigateAsync(MenuIndex.Home);
}
public void NavigateAsync(MenuIndex id)
{
Page newPage;
if (!Views.ContainsKey(id))
{
switch (id)
{
case MenuIndex.Home:
Views.Add(id, new ControlNavigationApp(new HomePage(), "#04518c"));
break;
case MenuIndex.pag1:
Views.Add(id, new ControlNavigationApp(new TabPage1(), "#04518c"));
break;
case MenuIndex.pag2:
Views.Add(id, new ControlNavigationApp(new TabPage2(), "#04518c"));
break;
case MenuIndex.pag3:
Views.Add(id, new ControlNavigationApp(new TabPage3(), "#04518c"));
break;
case MenuIndex.pag4:
Views.Add(id, new ControlNavigationApp(new TabPage4(), "#04518c"));
break;
case MenuIndex.pag5:
Views.Add(id, new ControlNavigationApp(new TabPage5(), "#04518c"));
break;
default:
break;
}
}
newPage = Views[id];
if (newPage == null)
return;
////pop to root for Windows Phone
//if (Detail != null && Device.OS == TargetPlatform.WinPhone)
//{
// await Detail.Navigation.PopToRootAsync();
//}
Detail = newPage;
if (Device.Idiom != TargetIdiom.Tablet)
IsPresented = false;
}
}
the error is in the rootpage because I did some tests calling other pages and they worked with success. I have the project in ios and it works perfectly

Xamarin NSTableView hide columns, color specific cells, color specific rows

i'm trying to implement a NSTableView in my project and fill it with specific data. This works quite fine. But now, i want to be able, to hide some columns, color specific cells, or color specific rows. I made something similar in java, but i really don't know to to do this in Xamarin:Mac.
Here is the code for my delegate:
public class Mp3FileTableDelegate : NSTableViewDelegate {
private const string CellIdentifier = "FileCell";
private Mp3FileDataSource DataSource;
public Mp3FileTableDelegate (Mp3FileDataSource datasource) {
this.DataSource = datasource;
}
public override NSView GetViewForItem (NSTableView tableView, NSTableColumn tableColumn, nint row) {
// This pattern allows you reuse existing views when they are no-longer in use.
// If the returned view is null, you instance up a new view
// If a non-null view is returned, you modify it enough to reflect the new data
NSTextField view = (NSTextField)tableView.MakeView (CellIdentifier, this);
if (view == null) {
view = new NSTextField ();
view.Identifier = CellIdentifier;
view.BackgroundColor = NSColor.Clear;
view.Bordered = false;
view.Selectable = false;
view.Editable = true;
view.EditingEnded += (sender, e) => {
SetNewValueInMp3File (DataSource.AudioFiles [(int)row], tableColumn, view.StringValue);
};
}
AudioFile audioFile = DataSource.AudioFiles [(int)row];
// Setup view based on the column selected
switch (tableColumn.Title) {
case "Path":
view.StringValue = audioFile.getPathWithFilename ();
break;
}
if (audioFile.GetType () == typeof(Mp3File)) {
Mp3File mp3File = (Mp3File)audioFile;
switch (tableColumn.Title) {
case "Artist":
view.StringValue = mp3File.Artist;
break;
case "Title":
view.StringValue = mp3File.Title;
break;
case "Album":
view.StringValue = mp3File.Album;
break;
case "BPM":
view.StringValue = mp3File.BPM;
break;
case "Comment":
view.StringValue = mp3File.Comment;
break;
case "Year":
view.StringValue = mp3File.Year;
break;
case "Key":
view.StringValue = mp3File.InitialKey;
break;
case "Quality":
view.StringValue = mp3File.Album;
break;
case "Length":
view.StringValue = mp3File.Album;
break;
}
}
return view;
}
private void SetNewValueInMp3File (AudioFile file, NSTableColumn tableColumn, String value) {
if (file.GetType () == typeof(Mp3File)) {
Mp3File mp3File = (Mp3File)file;
switch (tableColumn.Title) {
case "Artist":
mp3File.Artist = value;
break;
case "Title":
mp3File.Title = value;
break;
case "Album":
mp3File.Album = value;
break;
case "BPM":
mp3File.BPM = value;
break;
case "Comment":
mp3File.Comment = value;
break;
case "Year":
mp3File.Year = value;
break;
case "Key":
mp3File.InitialKey = value;
break;
}
}
}
}
And here for my datasource:
public class Mp3FileDataSource : NSTableViewDataSource {
public List<AudioFile> AudioFiles = new List<AudioFile> ();
public Mp3FileDataSource () {
}
public override nint GetRowCount (NSTableView tableView) {
return AudioFiles.Count;
}
}
I would be very thankful, if anyone could help me a little.
Thanks

How to conditionally load edit template in telerik rad grid?

Is there a way to conditionally load different user control as edit form based on some user action (Edit vs. Create)?
This is how I ended up doing it
protected void Page_Init(object sender, EventArgs e)
{
ctlBenefitLimitsGrid.MasterTableView.EditFormSettings.EditFormType = GridEditFormType.WebUserControl;
ctlBenefitLimitsGrid.ItemDataBound += BenefitLimitsGrid_ItemDataBound;
ctlBenefitLimitsGrid.ItemCommand += BenefitLimitsGrid_ItemCommand;
ctlBenefitLimitsGrid.MasterTableView.EditFormSettings.EditFormType = GridEditFormType.WebUserControl;
}
void BenefitLimitsGrid_ItemCommand(object source, GridCommandEventArgs e)
{
switch (e.CommandName)
{
case "Edit":
e.Item.OwnerTableView.IsItemInserted = false;
ctlBenefitLimitsGrid.MasterTableView.EditFormSettings.UserControlName =
#"UserControls/BenefitLimitEdit.ascx";
break;
case "Add":
{
InsertAddControl(e);
break;
}
case "Delete":
var benefitLimitId = Convert.ToInt32(e.CommandArgument);
Presenter.Delete(benefitLimitId);
Presenter.LoadView();
break;
case "Save":
IEditView item;
if (e.Item.GetType() == typeof(GridEditFormInsertItem))
{
item =
(IEditView)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);
}
else
{
item = ((GridDataItem)e.Item).EditFormItem.FindControl(GridEditFormItem.EditFormUserControlID) as IEditView;
}
if (item != null && item.HasErrors)
{
e.Canceled = true;
return;
}
e.Item.Edit = false;
ctlBenefitLimitsGrid.MasterTableView.ClearEditItems();
Presenter.LoadView();
break;
case "SaveAndNew":
{
var benefitLimitCreate = (IBenefitLimitCreate)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);
if (benefitLimitCreate.HasErrors)
{
e.Canceled = true;
return;
}
Presenter.LoadView();
InsertAddControl(e);
break;
}
}
}
private void InsertAddControl(GridCommandEventArgs e)
{
ctlBenefitLimitsGrid.MasterTableView.ClearEditItems();
ctlBenefitLimitsGrid.MasterTableView.EditFormSettings.UserControlName = #"UserControls/BenefitLimitCreate.ascx";
e.Item.OwnerTableView.InsertItem();
var insertedItem = e.Item.OwnerTableView.GetInsertItem();
var ctlBenefitLimitCreate = (BenefitLimitCreate)insertedItem.FindControl(GridEditFormItem.EditFormUserControlID);
ctlBenefitLimitCreate.Presenter.LoadView();
}
I think that there was an article in the Telerik online documentation (under Insert/Update/Delete -> HowTo chapter) which does exactly what you are after.
Dick

Resources