Mono navigation - xamarin

I just started out with mono and I've already gotten problems. I'm used to play with c# code and was told that mono would be easy for me, but no no. I simply want to start a new activity and close the one i just were using. I checked out some mono API examples, but they are simply too complicated for this task. It has to be some easier way of doing it. This is my first activity class:
[Activity(Label = "CryptotoDroid", MainLauncher = true, Icon = "#drawable/icon")]
public class Activity1 : Activity
{
EditText inputpassword;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
Button button = FindViewById<Button>(Resource.Id.MyButton);
inputpassword = FindViewById<EditText>(Resource.Id.beforetext);
button.Click += delegate
{
if (inputpassword.Text == "Moo")
{
StartActivity(typeof(ActivityContacts));
}
};
}
}
This is what i tried, but the program crashes. I Simply want to make the program to start a new activity when the password is "moo".
The activity I want to start is:
[Activity(Label = "My Activity")]
public class ActivityContacts : Activity
{
protected override void OnCreate(Bundle bundle)
{
SetContentView(Resource.Layout.Main);
var contactgrid = FindViewById<GridView>(Resource.Id.gridview);
}
}
Later on, i would also like to fill out my gridview with all contacts in the phone, but that belongs to another topic.

Did you mean to set both activities' content views to the Main layout?
SetContentView(Resource.Layout.Main);
public void setContentView (View view)
Set the activity content to an explicit view. This view is placed directly into the activity's view hierarchy. It can itself be a complex view hierarchy. When calling this method, the layout parameters of the specified view are ignored. Both the width and the height of the view are set by default to MATCH_PARENT. To use your own layout parameters, invoke setContentView(android.view.View, android.view.ViewGroup.LayoutParams) instead.

For starting with MonoDroid, I recommend:
getting hold of one of the excellent books out there (Wally, Greg, Chris or others will be along with suggestions - I've personally yet to see a bad one so won't give a recommendation!)
try watching a couple of the Xaminar's on youTube -http://www.yourepeat.com/g/Xaminar
try building the Xamarin sample programs and then try adapting them - it's sometimes easier to adjust working programs than it is to start new ones (sometimes!)
try building new things just as you have above.
When you hit problems - as we all do - then please do ask questions here or on the Xamarin forums - people will help.
However, when you hit a crash or exception, please try:
Give us as much info as you can about the crash/exception - including there are some ways to get extra debug logs off of a phone or emulator - http://docs.xamarin.com/Android/Guides/Deployment,_Testing,_and_Metrics/Android_Debug_Log - these logs often contain important text which will help us help you diagnose the crash
If you are running under the VS2010, VS2012 or MonoDevelop debugging tools, try adding extra Console.WriteLine statements and/or use breakpoints - this can help you and us work out which line is causing the crash - or if the crash is occurring somewhere in setup before the code even runs.
Speaking personally, I believe Mono for Android does help experience C# devs exploit their skills on Android - but there's still new things to learn, and there are still embedded development frustrations to tackle and overcome (like these sort of crashes)

Related

How do I make a ProgressBar with rounded corners using Xamarin Forms

So my goal is to create a ProgressBar with rounded corners. This seems to be way harder than what it should be. I've tried looking at examples on StackOverflow but most of them are outdated or is referring to Android Studio which is NOT what I am working with.
As far as I know, I need to create a whats called "Custom Renderer" which is essentially a class that inherits from ProgressBarRenderer. And then change most of the properties there, which seems kinda silly since the UI should be done using XAML in my opinion.
I keep running into multiple issues when trying to create the custom renderer.
The first one being this
I've already installed Xamarin.Forms and it still keeps throwing that error.
And then there is a issue regarding the constructor. The constructor that doesn't take any parameters is obsolete and doesn't work anymore and it requires me to add a Context which I can't either because it throws the same error as above.
How do I properly create a progressbar with rounded corners and text in the middle indicating how much % it's at?
You should add proper reference to use the ProgressBarRenderer:
using App498.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
The custom renderer should look like this:
[assembly: ExportRenderer(typeof(Xamarin.Forms.ProgressBar), typeof(myProgressBarRenderer))]
namespace App498.Droid
{
public class myProgressBarRenderer : ProgressBarRenderer {
public myProgressBarRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.ProgressBar> e)
{
base.OnElementChanged(e);
}
}
}
If you want to add round corner to progress bar, check this thread.
I would refer you to SkiaSharp, graphics library, that enables you to create... well everything.
See the documentation here:
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/graphics/skiasharp/
You'll need to play around for a while to get used to work with it, but it's quite intuitive and easy to work with.
To achieve round corners, you'll need to work with paths, described in here:
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/graphics/skiasharp/curves/clipping
By a chance I also needed a progress bar in my app. I've drawn a picture with transparent line within for the progress to be shown, merged it with green rectangle (the progress itself) and voila, you've got the progress bar. When you need to update the progress, you just redraw the progress bar.
There are surely plenty of other solutions, but this one I reccomend as I know it will work fine.
Hapyy coding ;)

deal with Unity 5 UI in augmented reality app

I'm trying to make an augmented reality application with vuforia and unity.
whenever it recognize the image target, it must tell a story by showing text , and it should enable the user to press next and back to go on reading the different parts of this story, I'm totally new to unity and don't know how to handle with UI throughout scripting, I need some help on how to accomplish the part of "going forward and backward on showing the story by hitting Next and Back buttons", and all these parts of story should be related to the same image target in the same scene.
I appreciate it if you help me with an example code.
You should create some script that attach on trackable object, maybe something like this.
public class DataBook {
string[] dataBook;
string idText;
bool isActive;
}
Then you must create another script to set that trackable object is active or not, this link can help you to get that.
https://developer.vuforia.com/forum/faq/unity-how-do-i-get-list-active-trackables
Then after you get the active trackable object, you can set the dialog from the book by create another controller script for button, example
public void Next() {
DataBook[] books = FindObjectsOfType<DataBook>(); // if the object more than one, it will be more easy if it only the one
foreach (var book in books)
{
if (book.isActive) {
book.idText += 1;
textUI.text = book.dataBook[idText]; //textUI assign to object text on canvas
}
}
}
you can learn about unity UI Button on this :
https://unity3d.com/learn/tutorials/modules/beginner/ui/ui-button
Good luck

DelayedConfirmationView doesn`t while changing layouts

I am pretty new to programming and trying to build my first own app for my Android Wear.
Basically I want to see a new layout when pressing one of two buttons. On the new layout should be a DelayedConfirmationView with the possibility to cancel and to go back to the previous layout (kind of using "ok google" "Set timer to 5 seconds")
For this I created two layouts and coded in the manifest:
public void onClickcallSpringer(View view) {
setContentView(R.layout.callspringer);
DelayedConfirmationView DelayConfirmationView = (DelayedConfirmationView) findViewById(R.id.delayed_confirmation);
DelayConfirmationView.setTotalTimeMs(2000);
DelayConfirmationView.setListener((DelayedConfirmationView.DelayedConfirmationListener) this);
DelayConfirmationView.start();
}
This leads to an crash, when pressing the button in my app.
I tried to understand the function DelayedConfirmationView from another App, but there it also leads to a crash (https://github.com/livefront/android-wear-demo).
I hope somebody can help me with this (and maybe explain to me how to solve this with a new activity as well).
Found my mistake: I had to make a new activity.
I am using now a .setOnClickListener to start a new Activity
button_callspringer.setOnClickListener((v) -> {
startActivity(new Intent(MainActivity.this, callspringer.class));})
and in the new class I coded on the onCreate-Methode:
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.callspringer);
DelayedConfirmationView delayedconfirmationview_experte = (DelayedConfirmationView) findViewById(R.id.delayed_confirmation_springer_view);
delayedconfirmationview_experte.setTotalTimeMs(5000);
delayedconfirmationview_experte.setListener(this);
delayedconfirmationview_experte.start();}
This leads to the wished result. Maybe this will help someone one day too.

Cocoa context menu causing crash when timer on main thread

I am working in mono on OSX10.6 - but this question is for Cocoa experts also please.
I have a context menu which I attach to an NSTableView. When I select the context menu the handler (which is empty for test) is called ok but then the application crashes.
This is the code I have in the window controller:
...
myNSTableView.Menu = new NSMenu();
NSMenuItem mi = new NSMenuItem("menu item 1", myHandler);
myNSTableView.Menu.AddItem(mi);
...
public void myHandler(object Sender, EventArgs e)
{
}
I also have an NSTimer setup to repeat every 200ms
myNSTimer = NSTimer.CreateRepeatingScheduledTimer(new TimeSpan(2000000),delegate {
move an NSSlider
});
NSRunLoop.Main.AddTimer(myNSTimer, NSRunLoopMode.Common);
NSRunLoop.Main.AddTimer(myNSTimer, NSRunLoopMode.EventTracking);
As soon as I try to access the context menu, the application crashes - every time.
If I dont start the timer, the context menu is fine.
I have tried literally everything to get this to work and have run out of ideas, so I really need someones help please, even just a pointer to where the problem might be. Forgetting about the fact that its mono just now, can anyone see from a cocoa point of view a problem?
It turned out that the problem was exacerbated by the timer, but the cause was actually that I wasn't declaring instance variables for the NSMenuItems, so they were getting garbage collected and causing crash at objc level.

prevent blend from adding a startupURI to application?

Is there some trick that is known to tell blend to stop trying to set a startupURI in my app.xaml? I googled but didn't see anything so I figured I would ask here.
I use a startup routine and instantiate mainwindow myself. Every once and a while blend likes to toss in the startupURI="MainWindow.xaml" when I let it compile. Occasionally I see some message along of the lines of "There is no startup scene associated with this project. Would you like blend to blah blah fix it?" or something along those lines. I click cancel/no yet it still tosses a gremlin in my code. Internal to blend there is some mechanism for checking for this property or it wouldn't complain via dialog box to me. So how do I just tell it "no thanks blend, i'm good without that?", lol.
Its quite annoying. I open blend to do something simple like using a color picker and use it to compile because VS2010 isn't open. My result is two mainwindows. But it does not do it every time so it's not a repeatable behavior. The compiler just acts out randomly.
edit: I'm using blend 4 but I saw this happen when i was using blend 3 also.
This is a horrible, terrible hack, but hey, it works. By default, StartupUri is null, but you can't set it to null using the property, so you can go around the property if you like to live on the edge.
// Dangit blend! Stop inserting a stupid StartupUri
private void FixStartupUri()
{
var type = typeof(Application);
var startupUri = type.GetField("_startupUri", BindingFlags.Public
| BindingFlags.NonPublic
| BindingFlags.Instance);
startupUri.SetValue(this, null);
}
Add this to your Application class and call it like so:
protected override void OnStartup(StartupEventArgs e)
{
FixStartupUri();
base.OnStartup(e);
// Do the rest of your startup stuff.
}

Resources