I'm 12 hours into using Xamarin, and have a beginner blocker while working on a Xamarin / c# project for iOS 7.
This is how I though the view controller should call kickOff (but it doesnt work):
using iOS;
...
public void ViewDidLoad () {
base.ViewDidLoad ();
kickOff ();
}
exampleClass.cs:
namespace iOS
{
public class foo
{
public static void kickOff (){
Console.WriteLine("Success!");
}
}
What am I missing? Thanks!
You never point to the class foo. You need to tell it where to look for the method kickOff()
The following should work:
foo.kickOff();
Note
Your question doesn't say what doens't work, but I'm assuming it's a compiler error about kickOff not being a valid method
Related
please i just encountered this problem and i dont know how to go about it. I've tried to reset Visual studio settings to no avail.
I just created a new class and when i try to create a method in it, i get the following errors;
property or indexer must have at least one accessor,
property or indexer cannot have a void type
the problem is that i'm actually trying to create a method and not a property.
below is the class
public static class IoCContainer
{
public static void Setup
{
}
}
I don't know if i mistakenly toogle a visual studio setting or something.
The problem is that you're missing the parenthesis after Setup. Changing to the following should fix your issue:
public static void Setup()
{
}
The compiler thought you were trying to create a Property due to the missing parens.
You are missing parentheses () at the end of your function name.
So your code should be:
public static class IoCContainer
{
public static void Setup()
{
}
}
Can anyone point me to a Xamarin.forms sample that utilises AndHud for Android and BTProgressHud for iOS (or anything similar)?
I know there is the ACR-Xamarin-Forms example here https://github.com/aritchie/acr-xamarin-forms, but it is way too complicated and completely over my head.
Surely there is a more simple easy to understand implementation, but if not some sort of guidance on how to get started on implementing it (for a C# and Xamarin newbie)
Thanks in advance
This is how I've done it for iOS. All you should need to do for Android is create an implementation of IHud that using AndHud instead of BTProgressHud.
First, create an interface in the shared project
public interface IHud
{
void Show();
void Show(string message);
void Dismiss();
}
Next, in the iOS project, create an implementation of IHud using BTProgressHud component:
[assembly: Dependency(typeof(Hud))]
namespace project.iOS
{
public class Hud : IHud
{
public Hud ()
{
}
public void Show() {
BTProgressHUD.Show ();
}
public void Show(string message) {
BTProgressHUD.Show (message);
}
public void Dismiss() {
BTProgressHUD.Dismiss ();
}
}
}
Finally, to utilize this from Forms code, just do
var hud = DependencyService.Get<IHud> ();
hud.Show ("Loading Vehicles");
// tasks...
hud.Dismiss ();
My answer, which is quite similar to #jason's can be found here:
Loading Icon for layout screen Xamarin android
I have create a metro app which is composed by
- a c# windows runtime component
- a javascript application, wich contains the UI and is the main application.
In the c# component I created an async method:
async public void createDocument() {
}
but when I try to call it from the javascript code, I cannot use the .done() or the then() function to handle the method completed evet, because there is an error: javascript error, cannot call done() from object not set to an instance of object.
If I try to assign Task or Task to the function I have another error, which tell me Task is not a windows runtime type and to use IAsyncOperation, IAsyncAction, ecc.. instead.
So, how can I create an async function in the c# windows runtime component and call it from javascript handling the done() or then() events?
I found an article that seems to be related to the problem you are having. I haven't done this myself, but the gist of it says that you can't use the async keyword from C# for Javascript promises - you must wrap the method in a slightly different way:
instead of:
public sealed class Class1
{
public async void testAsync()
{
// do this asynchronously ...
}
}
try:
public sealed class Class1
{
public IAsyncActionWithProgress<Result> testAsync()
{
return AsyncInfo.Run<Result>((token, result) =>
Task.Run<Result>(()=>
{
// do this asynchronously ...
return new Result();
}
));
}
}
public sealed class Result { ... }
}
I copied and pasted the examples from this article by Ronald Widha - http://www.ronaldwidha.net/2012/05/10/winrt-consumer-preview-calling-c-csharp-async-class-libraries-from-javascript-winjs-promises-using-then-clause/ It was written during the consumer preview, so it might have changed between then and the final release
Hopefully that will help you a bit more!
Just for information, if you need to call asyncronous methods inside the procedure, you need to use:
public static IAsyncOperation<IList<string>> DownloadAsStringsAsync(string id)
{
return Task.Run<Type retourned>(async () =>
{
var data = await AsyncMethod(...);
return (somethingOfTypeRetourned;
}).AsAsyncOperation();
}
I am trying to write some code, and I am trying to break it up in to different classes because of the way the code will eventually need to work, and I am also working with retrieving data from the internet, so I was trying to do two new things for myself and I can't figure out why this won't work, here is my code:
using System.Collections;
public class SomeClass {
SomeProjectClass_www Test = new SomeProjectClass_www();
Test.GetData();
}
public class SomeProjectClass_www : IEnumerator {
public IEnumerator GetData()
{
WWW www = new WWW("file://C:\\Users\\generic_user\Documents\\test.txt");
yield return url;
Debug.Log(www.text);
}
}
The code generates a CS1519 error, in Unity3d which is what I am using. That is where the Debug.Log comes from. Sorry, I didn't provide much useful information, I really don't know much of whats going on.
The problem is your SomeClass has logic defined in the class, not in a method:
public class SomeClass
{
// This logic needs to go into a method
void SomeMethod()
{
SomeProjectClass_www Test = new SomeProjectClass_www();
Test.GetData();
}
}
I'm trying to implement scripting capability to my application. I'm using the code below. Whenever I instantiate a new Api object, the application is supposed to quit (a little testing thing :p)
However, the application crashes at script.AddObject(...) with the error Specified cast is not valid. Is there a step I'm missing here?
public class ApiExposed
{
public string ModuleName;
public void Exit()
{
System.Environment.Exit(0);
}
}
public class Api
{
ScriptControlClass script;
ApiExposed ApiObj;
public Api()
{
ApiObj = new ApiExposed();
script = new ScriptControlClass();
script.Language = "VBScript";
script.AddObject("tbapi", (object)ApiObj, true);
script.Eval("tbapi.Exit()");
}
}
My guess is, the ApiExposed class is not COM visible - it will need to be in order for VBScript to interact with it.
I had similar problems to this.
Make sure you use [ComVisible(true)] on the class and methods.
Also, make your class PUBLIC!