Rootpage error xamarin forms - xamarin

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();
}
}

Related

WebView File Chooser stops to respond after cancelled selection

we have implement file chooser for web view. it works successfully when attachment is selected, but fails when cancelled without file specification. The file chooser just stops to react on click
any help is appreciated. Thanks
we use chrome client. it works fine if in all cases, file selection is listed. but even from the first file selection is cancelled, no longer file chooser will work. It is Xamarin.Android app based fully on webview
Our code is:
protected override void OnActivityResult(int requestCode, Result resultCode, Intent intent)
{
if (requestCode == FILECHOOSER_RESULTCODE)
{
if (null == _mUploadMessage)
return;
// Check that the response is a good one
if (resultCode == Result.Ok)
{
Android.Net.Uri[] results = null;
if (intent == null)
{
// If there is not data, then we may have taken a photo
if (mCameraPhotoPath != null)
{
results = new Android.Net.Uri[] { Android.Net.Uri.Parse(mCameraPhotoPath) };
}
}
else
{
if (intent.DataString != null)
{
results = new Android.Net.Uri[] { Android.Net.Uri.Parse(intent.DataString) };
}
}
_mUploadMessage.OnReceiveValue(results);
_mUploadMessage = null;
}
}
}
Chrome client:
var chrome = new FileChooserWebChromeClient((uploadMsg) =>
{
_mUploadMessage = uploadMsg;
mCameraPhotoPath = null;
Intent takePictureIntent = new Intent(Android.Provider.MediaStore.ActionImageCapture);
//Create the File where the photo should go
File photoFile = null;
try
{
string folder = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
photoFile = new File(folder, "image" + DateTime.Now.Millisecond + ".png");
takePictureIntent.PutExtra("PhotoPath", mCameraPhotoPath);
}
catch (IOException ex)
{
// Error occurred while creating the File
System.Console.WriteLine("" + ex.ToString());
}
// Continue only if the File was successfully created
if (photoFile != null)
{
mCameraPhotoPath = "file:" + photoFile.AbsolutePath;
takePictureIntent.PutExtra(Android.Provider.MediaStore.ExtraOutput,
Android.Net.Uri.FromFile(photoFile));
}
else
{
takePictureIntent = null;
}
Intent contentSelectionIntent = new Intent(Intent.ActionGetContent);
contentSelectionIntent.AddCategory(Intent.CategoryOpenable);
contentSelectionIntent.SetType("image/*");
Intent[] intentArray;
if (takePictureIntent != null)
{
intentArray = new Intent[] { takePictureIntent };
}
else
{
intentArray = new Intent[0];
}
Intent chooserIntent = new Intent(Intent.ActionChooser);
chooserIntent.PutExtra(Intent.ExtraIntent, contentSelectionIntent);
chooserIntent.PutExtra(Intent.ExtraTitle, this.GetStringFromResource(Resource.String.chose_photo));
chooserIntent.PutExtra(Intent.ExtraInitialIntents, intentArray);
base.StartActivityForResult(chooserIntent, HarmonyAndroid.AndroidMainActivity.FILECHOOSER_RESULTCODE);
});
return chrome;
Part 2
class FileChooserWebChromeClient : WebChromeClient
{
Action<IValueCallback> callback;
public FileChooserWebChromeClient(Action<IValueCallback> callback)
{
this.callback = callback;
}
public override bool OnShowFileChooser(WebView webView, IValueCallback filePathCallback, FileChooserParams fileChooserParams)
{
callback(filePathCallback);
return true;
}
public override void OnCloseWindow(WebView window)
{
base.OnCloseWindow(window);
}
}
Part 3
webView.ImprovePerformance();
webView.SetWebViewClient(new HomeWebViewClient(customWebViewClientListener, clientId));
webView.SetWebChromeClient(chrome);
webView.Settings.JavaScriptEnabled = true;
webView.Settings.DomStorageEnabled = true;
webView.SetDownloadListener(new CustomDownloadListener(activity, customDownloadListener));
webView.AddJavascriptInterface(new JavaScriptToCSharpCommunication(activity, javaScriptToCSharpCommunicationListener), Constants.JS_CSHARP_COMMUNICATOR_NAME);
Try to give a null object to the uri callback, when the resultCode is not RESULT_OK.
add in your OnActivityResult method:
if (resultCode != Result.Ok)
{
_mUploadMessage.OnReceiveValue(null);
_mUploadMessage = null;
return;
}

How to create a page in 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;
}
}

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

how to get real composite name in SWT.Show event

I want to log the information of user's action, such as button click, show composite. So I use event listener. When I use SWT.FocusIn, I can get the real composite name, but when I use SWT.Show I can't get the real class name.
Listener listener = new Listener() {
public void handleEvent(Event event) {
switch (event.type) {
case SWT.FocusIn: {
if(event!=null&&event.widget!=null){
Control parent = ((Control)event.widget).getParent();
while(parent!=null&&(parent.toString().startsWith("Composite")
||parent.toString().startsWith("Group")
||parent.toString().startsWith("Splitter")
)){
parent = parent.getParent();
}
if(parent!=null){
**//This place I can get "SWT.FocusIn:DoSomethingComposite"(The subclass of Composite).**
log.info("SWT.FocusIn:" + parent.toString());
}
}
break;
}
case SWT.Show: {
if (event.widget != null) {
if(event.widget instanceof Composite) {
Composite composite = (Composite)(event.widget);
**//But this place I just get "SWT.Show:Composite".**
log.info("SWT.Show:" + event.widget.toString());
}
}
break;
}
default: {
break;
}
}
}
}
PlatformUI.getWorkbench().getDisplay().addFilter(SWT.Show, listener);
PlatformUI.getWorkbench().getDisplay().addFilter(SWT.FocusIn, listener);

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

Resources