MvvmCross with Xamarin.Forms: Setup not called - c#

I am trying to put together my existing Xamarin.Forms application with MvvmCross.Forms. Unfortunately I am not able to go through the initialization.
MainActivity.cs
[Activity(MainLauncher = true, Label = "Main Activity")]
public class MainActivity : FormsApplicationActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
Forms.Init(this, bundle);
var app = new MvxFormsApp();
LoadApplication(app);
var presenter = (MvxFormsDroidPagePresenter) Mvx.Resolve<IMvxViewPresenter>(); // Exception
presenter.MvxFormsApp = app;
Mvx.Resolve<IMvxAppStart>().Start();
}
}
Setup.cs
public class Setup : MvxAndroidSetup
{
public Setup(Context applicationContext) : base(applicationContext)
{
}
protected override IMvxApplication CreateApp()
{
return new App();
}
protected override IMvxAndroidViewPresenter CreateViewPresenter()
{
var presenter = new MvxFormsDroidPagePresenter();
Mvx.RegisterSingleton<IMvxViewPresenter>(presenter);
return presenter;
}
}
I guess the problem is that the Setup is not called at all, not even the constructor. Am I right? What is wrong with the code?

The bootstrapping is done in the splash screen.
You have to remove MainLauncher = true from your MainActivity and add a splash screen like:
[Activity(MainLauncher = true
, Theme = "#style/Theme.Splash"
, NoHistory = true
, ScreenOrientation = ScreenOrientation.Portrait)]
public class SplashScreen
: MvxSplashScreenActivity
{
public SplashScreen()
: base(Resource.Layout.SplashScreen)
{
}
private bool _isInitializationComplete;
public override void InitializationComplete()
{
if (!_isInitializationComplete)
{
_isInitializationComplete = true;
StartActivity(typeof(MainActivity));
}
}
protected override void OnCreate(Android.OS.Bundle bundle)
{
Forms.Init(this, bundle);
Forms.ViewInitialized += (object sender, ViewInitializedEventArgs e) =>
{
if (!string.IsNullOrWhiteSpace(e.View.StyleId))
{
e.NativeView.ContentDescription = e.View.StyleId;
}
};
base.OnCreate(bundle);
}
}
If you need a working example, see our example app:
https://github.com/xabre/xamarin-bluetooth-le/tree/master/Source/BLE.Client

Related

Using Custom XAML Controls in the Xamarin.Android Project with a Keyboard Service App

I have a custom keyboard app I'm trying to write. I'd like to define the key layout as XAML control (KeyboardPage.xaml) in my shared code and then incorporate them into native code in the .Android and .iOS projects. Being primarily a desktop WPF developer, I'm comfortable with this approach.
When my MainActivity inherits from FormsAppCompatActivity or AppCompatActivity, this is pretty straight forward.
namespace MobileTestApp2.Droid
{
[Activity(Label = "MobileTestApp2", Icon = "#mipmap/icon", Theme = "#style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : FormsAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(savedInstanceState);
Forms.Init(this, bundle);
LoadApplication(new App());
}
}
}
However, in order for the app to register as an Android keyboard service, MainActivity has to inherit from InputMethodService:
namespace TestKeyboard.Droid
{
[Service(Permission = "android.permission.BIND_INPUT_METHOD", Label = "Test Keyboard")]
[MetaData("android.view.im", Resource = "#xml/method")]
[IntentFilter(new string[] { "android.view.InputMethod" })]
public class MainActivity : InputMethodService, KeyboardView.IOnKeyboardActionListener
{
private KeyboardView keyboardView;
private Keyboard keyboard;
private bool isCaps = false;
public override View OnCreateInputView()
{
keyboardView = (KeyboardView)LayoutInflater.Inflate(Resource.Layout.Keyboard, null);
keyboard = new Keyboard(this, Resource.Xml.Qwerty);
keyboardView.Keyboard = keyboard;
keyboardView.OnKeyboardActionListener = this;
return keyboardView;
}
}
I'm not sure how to convert my XAML controls into Android controls; and how to get them to replace the Keyboard.axml and.or qwerty.xml layouts I have defined in the .Android project.
From research, I found that this is possible using the CreateSupportFragment() method and the SupportFragmentManager member. However, the SupportFragmentManager member is only available when inheriting from FormsAppCompatActivity or AppCompatActivity. I tried instantiating a local object inheriting from FormsAppCompatActivity and replacing my Keyboard.axml and qwerty.xml layouts with my converted XAML control (KeyboardPage.xaml) before I instantiated a new Keyboard object; but it didn't work. I'm not even sure if the OnCreate() method is even being called.
namespace MurrayRegisterKeys.Droid
{
public class MainActivity : InputMethodService, KeyboardView.IOnKeyboardActionListener
{
private KeyboardView keyboardView = null;
private Keyboard keyboard = null;
private bool isQwerty = false;
public override View OnCreateInputView()
{
new InnerActivity();
KeyboardView keyboardView = (KeyboardView)LayoutInflater.Inflate(Resource.Layout.Keyboard, null);
keyboard = new Keyboard(this, Resource.Xml.Keypad);
keyboardView.Keyboard = keyboard;
keyboardView.OnKeyboardActionListener = this;
return keyboardView;
}
}
public class InnerActivity : FormsAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
Forms.Init(this, bundle);
SetContentView(Resource.Layout.Keyboard);
Android.Support.V4.App.Fragment keyboardPage = new KeyboardPage().CreateSupportFragment(this);
SupportFragmentManager
.BeginTransaction()
.Replace(Resource.Xml.Qwerty, keyboardPage)
.Commit();
}
}
}
Any ideas? Is what I'm trying to do even possible? Are there alternate approaches for creating a new default keyboard service?
Thanks!

UISearchBar SearchButtonClicked never called?

I am new to iOS, and I am using Xamarin Studio on MacOS (I already have my Xamarin.Droid app and now making it on iOS).
I am struggling using an UISearchBar, I want a really basic usage tho .. I tried to follow this recipes (https://developer.xamarin.com/recipes/ios/content_controls/search-controller/) but still can't manage to make it work.
What I am trying to get is a SearchBar, in which the user should type text then press the "search" button on the keyboard. Pressing the search button should call code that will populate an UITableView with data retrieved on my webservice.
Here's my Storyboard structure, a ViewController which contains an UITableView with a name
I tried the following code, everything seems to work fine, but when I press search on the keyboard, SearchButtonClicked event is never fired ... And I can't figure out why
public partial class SearchViewController : UIViewController
{
public SearchViewController(IntPtr handle) : base(handle)
{
}
public override void ViewDidLoad()
{
var searchController = new UISearchController(this);
searchController.SearchBar.SizeToFit();
searchController.SearchBar.ShowsCancelButton = true;
searchController.SearchBar.SearchButtonClicked += delegate {
Console.WriteLine("Hello ????");
this.Title = "This code is never called";
};
RunnersSearchTableView.TableHeaderView = searchController.SearchBar;
base.ViewDidLoad();
}
}
Also tried
public partial class SearchViewController : UIViewController
{
public SearchViewController(IntPtr handle) : base(handle)
{
}
public override void ViewDidLoad()
{
var searchController = new UISearchController(this);
searchController.SearchBar.SizeToFit();
searchController.HidesNavigationBarDuringPresentation = true;
searchController.SearchBar.ShowsCancelButton = true;
searchController.SearchBar.Delegate = new MySearchBarDelegate();
RunnersSearchTableView.TableHeaderView = searchController.SearchBar;
base.ViewDidLoad();
}
}
public class MySearchBarDelegate : UISearchBarDelegate
{
public MySearchBarDelegate()
{
}
public override void SearchButtonClicked(UISearchBar searchBar)
{
Console.WriteLine("TEST");
searchBar.ResignFirstResponder();
base.SearchButtonClicked(searchBar);
}
}
And also tried
public partial class SearchViewController : UIViewController
{
public SearchViewController(IntPtr handle) : base(handle)
{
}
public override void ViewDidLoad()
{
var searchController = new UISearchController(this);
searchController.SearchBar.SizeToFit();
searchController.HidesNavigationBarDuringPresentation = true;
searchController.SearchBar.ShowsCancelButton = true;
searchController.SearchBar.WeakDelegate = this;
RunnersSearchTableView.TableHeaderView = searchController.SearchBar;
base.ViewDidLoad();
}
[Export("searchBarSearchButtonClicked:")]
public virtual void SearchButtonClicked(UISearchBar searchBar)
{
Console.WriteLine("hello??");
searchBar.ResignFirstResponder();
}
}
None of them seems to work
You need to register delegate for searchbar .
[Foundation.Register("UISearchBar", true)]
for more : https://developer.xamarin.com/api/type/UIKit.UISearchBar/

Java.Lang.RuntimeException: native typeface cannot be made and custom fonts

I have these two codes (two activities)
MainActivity
namespace App16
{
[Activity(Label = "App16", MainLauncher = true, Icon = "#drawable/icon")]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
TextView tv = FindViewById<TextView>(Resource.Id.textView1);
Button button = FindViewById<Button>(Resource.Id.button1);
var activity2 = new Intent(this, typeof(Activity1)).SetFlags(ActivityFlags.ReorderToFront);
string[] str = new string[2];
str[0] = "hello"; str[1] = "سلام";
activity2.PutExtra("MyData", str);
button.Click += delegate
{
StartActivity(activity2);
};
}
}
}
Activity
namespace App16
{
[Activity(Label = "Activity1")]
public class Activity1 : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
SetContentView(Resource.Layout.Main2);
string[] text = Intent.GetStringArrayExtra("MyData");
TextView tvE = FindViewById<TextView>(Resource.Id.textView3);
TextView tvP = FindViewById<TextView>(Resource.Id.textView2);
Typeface typeP = Typeface.CreateFromAsset(this.Assets, "fonts/blotus.ttf");
tvP.SetTypeface(typeP, TypefaceStyle.Normal);
if (text[0] == "hello")
{
tvE.Text = text[0];
tvP.Text = text[1];
}
}
}
}
These work very well but when I use these in another program, I have this error:
Java.Lang.RuntimeException: native typeface cannot be made
I do not know why?
I changed the letters of my font to uppercase letters and my problem was solved:
BLOTUS.TTF

Save products in basket , when close activity

I have basket activity when I add products from list of products.
Code of adding attributes
add.Click += delegate {
var intent = new Intent (this, typeof(CartActivity));
intent.PutExtra ("title", (string)(firstitem ["post_title"]));
intent.PutExtra ("price", (string)(firstitem ["price"] + " грн"));
intent.PutExtra ("weight", (string)(firstitem ["weight"] + "г"));
StartActivity (intent);
};
Code of receiving attributes
productname.Text = Intent.GetStringExtra("title");
price.Text = Intent.GetStringExtra("price");
weight.Text = Intent.GetStringExtra("weight");
I tried OnPause
namespace MurakamiKiev
{
[Activity(Label = "Murakami", Icon = "#drawable/logo", Theme = "#android:style/Theme.Black.NoTitleBar", ScreenOrientation = ScreenOrientation.Portrait)]
public class CartActivity : Activity
{
protected override void OnPause()
{
base.OnPause();
But when I run activity I have black screen
Did you call SetContentView() in OnCreate()?
like this
namespace MurakamiKiev
{
[Activity(Label = "Murakami", Icon = "#drawable/logo", Theme = "#android:style/Theme.Black.NoTitleBar", ScreenOrientation = ScreenOrientation.Portrait)]
public class CartActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.YourLayout);
}
}
}

Load url from Activity1 to Activity2 which contains a webview

How do I load a url from firstActivity to the webpageActivity?
I would like to be able to click a button with the url from firstActivity then pass it to the webpage activity and load the url.
Here is my code: FirstActivity
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
// Get our button from the layout resource,
// and attach an event to it
var scisnews = FindViewById<Button> (Resource.Id.scisnewsbtn);
string scisnewsurl = "http://cis.ulster.ac.uk/news-a-events-mainmenu-70";
//labinduction.Click += (sender, e) => {
// var LabInductionI = new Intent (this, typeof(LabInduction));
// StartActivity (LabInductionI);
//};
scisnews.Click += delegate {
var ScisNewsI = new Intent (this, typeof(WebPage));
ScisNewsI.PutExtra ("scisnews", scisnewsurl);
this.StartActivity (ScisNewsI);
};
}
public class HelloWebViewClient : WebViewClient
{
public override bool ShouldOverrideUrlLoading (WebView view, string url)
{
view.LoadUrl ("http://cis.ulster.ac.uk/news-a-events-mainmenu-70");
return true;
}
}
}
Code: WebPageActivity
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Create your application here
SetContentView (Resource.Layout.WebPageLO);
web_view = FindViewById<WebView> (Resource.Id.webview);
web_view.Settings.JavaScriptEnabled = true;
web_view.Settings.BuiltInZoomControls = true;
web_view.SetWebViewClient (new HelloWebViewClient ());
}
}
public class HelloWebViewClient : WebViewClient
{
public override bool ShouldOverrideUrlLoading (WebView view, string url)
{
view.LoadUrl ("http://cis.ulster.ac.uk/news-a-events-mainmenu-70");
return true;
}
}
public override bool OnKeyDown (Android.Views.Keycode keyCode, Android.Views.KeyEvent e)
{
if (keyCode == Keycode.Back && web_view.CanGoBack ())
{
web_view.GoBack ();
return true;
}
return base.OnKeyDown (keyCode, e);
}
}
You generally pass information from one activity to another by using the Intent class. It looks like you are trying to, but don't have the code pulling the value out.
Here is an example:
//In your first activity
var intent = new Intent(this, typeof(WebPage));
intent.PutExtra("url", "http://cis.ulster.ac.uk/news-a-events-mainmenu-70");
StartActivity(intent);
//Then in the second activity
string url = Intent.GetStringExtra("url");
//Then you can load the page like this
web_view.LoadUrl(url);
Does this help?

Categories

Resources