I am developing an Android app in Xamarin.Android(C#). However, i do feel that this question can also be answered by any Java devs as well.
I am new in android development. Anyways, i created a fragment with just a LinearLayout and a TextView inside it. When i create the background class for it, i don't inherit(on in JAVA's word, extend) it from the Fragment class but rather from the LinearLayout class.
So, the MyFragment.cs file starts like this :
public class MyFragment : LinearLayout
The JAVA equivalent would be
public class MyFragment extends LinearLayout
(P.S. I have limited knowledge of JAVA and it's sytaxes).
Anyways, all works fine. I have an Initialize method(In JAVA, it should be the Init method) which inflates the view of the fragment. From the view, it tries to find the TextView with the given Id.
So, the codes looks like this :
public class MyFragment : LinearLayout
{
Context mContext;
private void Initialize(Context ctx)
{
//Inflating the layout
mContext = ctx;
var inflatorService = (LayoutInflater)ctx.GetSystemService(Context.LayoutInflaterService);
View v = inflatorService.Inflate(Resource.Layout.MyFragmentView, this, false);
this.AddView(v);
GoalHeader = v.FindViewById<TextView>(Resource.Id.GoalHeader);
}
All works pretty well this far. I then go on implementing the MVVM pattern, using MVVMLight library. I create a ViewModel as follows :
public class Vm_MyFragment : ViewModelBase
{
private string _goaltitle = "";
public string GoalTitle
{
get { return _goaltitle; }
set { Set(ref _goaltitle, value); }
}
public void SetTest()
{
DispatcherHelper.CheckBeginInvokeOnUI(() =>
{
GoalTitle = "Test";
});
}
}
Still, everything's good. The problem starts when i try to bind the TextView's text property to the ViewModel's GoalTitle property, as follows :
private readonly List<Binding> _bindings = new List<Binding>();
private void Initialize(Context ctx)
{
//Inflating the layout
mContext = ctx;
var inflatorService = (LayoutInflater)ctx.GetSystemService(Context.LayoutInflaterService);
View v = inflatorService.Inflate(Resource.Layout.MyFragmentView, this, false);
this.AddView(v);
Vm_MyFragmentView viewmodel = new Vm_MyFragmentView();
GoalHeader = v.FindViewById<TextView>(Resource.Id.GoalHeader);
_bindings.Add(
this.SetBinding(
() => mainViewModel.GoalTitle,
() => GoalHeader.Text));
}
Note : Binding is from the GalaSoft.MvvmLight.Helpers namespace.
I add the fragment in my main view(I mean, MainActivity's view) and debug the app. Upon execution, i get the following error :
Could not activate JNI Handle 0xfff02a68 (key_handle 0x339790a) of Java type 'md55bfae9a06327fa0fdf207b4f768604b1/MyFragment' as managed type 'TestApp.MyFragment'.
Searching google, i realized that i am trying to bind the property before the view is even created(correct me if i'm wrong). The suggestions i found on other SO answers were either to put the code in the OnCreateView method or somehow delay the execution of the binding part's code.
The first solution didn't work for me as LinearLayout aka a View doesn't have such a method OnCreateView which i can override.
So, how am i supposed to bind the TextView to the ViewModel then? And also, am i on the right track on treating the fragment as a LinearLayout as i am inheriting from it?
Im not familiar with MVVMLIght extension but if you are using a fragment as it is supposed to (ie. in a tablayout) you should inherit from a fragment like this (This is a v4 support fragment):
public class CategoryFragment : SupportFragment {
RecyclerView _recyclerView;
private View _view;
public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
_view = inflater.Inflate (Resource.Layout._CategoryLayout, container, false);
// Get our RecyclerView layout:
_recyclerView = _view.FindViewById<RecyclerView> (Resource.Id.categoryRecyclerView);
// Instantiate the layout manager
var linearLayoutManager = new LinearLayoutManager (Context, LinearLayoutManager.Vertical, false);
_recyclerView.SetLayoutManager (linearLayoutManager);
// Instantiate the adapter and pass in its data source:
_adapter = new CategoryAdapter (_categories);
//Register the item click handler with the adapter:
_adapter.ItemClick += OnItemClick;
// Plug the adapter into the RecyclerView:
_recyclerView.SetAdapter (_adapter);
return _view;
}
}
Related
I create a project with infragistic which generates the view and views models folders, what I want to do now is create a binding context to the view model as it is normally done, but this view model has INavigationService parameters and I don't know how to configure it. Those parameters, if someone helps me, I would really appreciate it, I attach images so that they understand me more.
enter image description here
enter image description here
In the mainPage background code you can use following code.
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
this.BindingContext= new PersonsViewModel(Navigation);
}
}
This Navigation comes from NavigableElement you can use Navigation directly in ContentPage.
NavigableElement is under the Xamarin.Forms namespace like following code.
============Prism==============
If you use Prism. you should registe it the App.xaml.cs.
public partial class App
{
/*
* The Xamarin Forms XAML Previewer in Visual Studio uses System.Activator.CreateInstance.
* This imposes a limitation in which the App class must have a default constructor.
* App(IPlatformInitializer initializer = null) cannot be handled by the Activator.
*/
public App() : this(null) { }
public App(IPlatformInitializer initializer) : base(initializer) { }
protected override async void OnInitialized()
{
InitializeComponent();
await NavigationService.NavigateAsync("NavigationPage/MainPage");
}
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterForNavigation<NavigationPage>();
containerRegistry.RegisterForNavigation<MainPage, MainPageViewModel>();
}
}
In your MainPage.xaml.cs you do not need other binding code.
Here is a demo about it.
https://github.com/manacespereira/xamarin-prism-navigation
I really made a search for this topic and did not find anything, and because of that, I am asking the question here.
I have a WPF application with Prism installed.
I have wired the view-model with the view automatically by name convention
<UserControl x:Class="Views.ViewA"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True">
and the model in the 'Model' like this
public class ViewAViewModel {
public ViewAViewModel () {
// time-resource consuming operations
}
}
the automatic binding work perfectly without a problem and the view and its corresponding view-model is matching, but the problem here.
I have a lot of those views say (50) and for every one of them, the view-model will be created with constructor exhausting the processes. This will make the startup of the application longer and also it will create a lot of view-models objects and put them in the RAM without being sure that they will be used at all.
What I need is to create the view-model class when the view is activated (I mean when the view is navigated to). Is this possible and if yes how?
Update
here is how I register the view with the Module, this is causing all the views to be created when the startup of the module.
public class Module1 : IModule
{
public void OnInitialized(IContainerProvider containerProvider)
{
var regionManager = containerProvider.Resolve<IRegionManager>();
regionManager.RegisterViewWithRegion("region1", typeof(View1));
regionManager.RegisterViewWithRegion("region1", typeof(View2));
is there any way to delay the creating of the views, until the navigation request come?
You could use navigation, for each view.
Or you must create an interfaces for your view and view model.
An example:
public interface IMyView
{
IMyViewModel ViewModel { get; set; }
}
public interface IMyViewModel
{
}
In the module or app.cs, in the method RegisterTypes you should register these.
containerRegistry.Register<IMyView, MyView>();
containerRegistry.Register<IMyViewModel, MyViewModel>();
You must implement IMyView interface in your MyView.cs class.
public partial class MyView : UserControl, IMyView
{
public MyView(IMyViewModel viewModel)
{
InitializeComponent();
ViewModel = viewModel;
}
public IMyViewModel ViewModel
{
get => DataContext as IMyViewModel;
set => DataContext = value;
}
}
After you could use it:
public void OnInitialized(IContainerProvider containerProvider)
{
var regionManager = containerProvider.Resolve<IRegionManager>();
var firstView = containerProvider.Resolve<IMyView>();
regionManager.AddToRegion(RegionNames.MainRegion, firstView);
}
In such case you shouldn't use ViewModelLocator.AutoWireViewModel in your view.
I'm just starting with Android Things with Xamarin, and I've already successfully turned on a LED, but I'm having trouble to detect a push button input.
I think the problem is the "RegisterGpioCallback" in the code below, but I'm not sure and really don't know how to fix it. Can somebody help me?? This is the code I'm using:
public class BlinkActivity : Activity
{
private IGpio gpio;
private IGpio button;
private IGpioCallback mButtonCallback;
protected override void OnCreate(Bundle savedInstanceState)
{
this.mButtonCallback = mButtonCallback;
PeripheralManager peripheralManager = PeripheralManager.Instance;
gpio = peripheralManager.OpenGpio("BCM17");
gpio.SetDirection(Gpio.DirectionOutInitiallyLow);
gpio.Value = false;
button = peripheralManager.OpenGpio("BCM4");
button.SetDirection(Gpio.DirectionIn);
button.SetEdgeTriggerType(Gpio.EdgeNone);
button.RegisterGpioCallback(new Handler(), mButtonCallback);
base.OnCreate(savedInstanceState);
Task.Run(() =>
{
if (mButtonCallback.OnGpioEdge(button) == true)
{
gpio.Value = !gpio.Value;
}
});
}
}
You need to actually implement the IGpioCallback interface so the com.google.android.things.pio library can make a "call back" into your application when the value of the GPIO changes.
Assign the RegisterGpioCallback to the actual object instance that has implemented the interface, in the following example, that will be on the Activity.
public class BlinkActivity : Activity, IGpioCallback
{
~~~~
button.RegisterGpioCallback(new Handler(), this);
~~~~
// remove the Task.Run block
public OnGpioEdge(Gpio gpio)
{
Log.Debug("SO", gpio.Value.ToString());
}
~~~~
}
I had some issues following this in Maui. I'd created an IGPIO interface in the shared code, and then a platform-specific GPIO class inside the Android platform code. The code would run, but then crash when it got to the Registration of the callback. The error said I had to pass a Java.Lang.Object or Java.Lang.Throwable as argument 2 to com.google.android.things.pio.impl.GpioImpl.registerGpioCallback(android.os.Handler, com.google.android.things.pio.GpioCallback).
I tried using each of these as the base class for my GPIO class, but then the app wouldn't build. When I'd autogenerated the IGpioCallback interface implementation in the class it had created a dispose method and a Handle property along with the OnGpioEdge callback method. Removing these allowed the app to work properly. so my class definition ended up looking something like this for the registration and event:
public class GPIO : Java.Lang.Throwable, IGPIO, IGpioCallback
{
public event EventHandler OnButtonEdge;
IGpio ButtonPin;
public void registerPinForEdgeDetection(string pinName)
{
using (var peripheralManager = PeripheralManager.Instance)
{
ButtonPin = peripheralManager?.OpenGpio(pinName);
ButtonPin.SetDirection(Gpio.DirectionIn);
ButtonPin.SetEdgeTriggerType(Gpio.EdgeBoth);
ButtonPin.RegisterGpioCallback(new Android.OS.Handler(), this);
}
}
public bool OnGpioEdge(IGpio gpio)
{
OnButtonEdge?.Invoke(ButtonPin, EventArgs.Empty);
return true;
}
}
I'm trying to create a Portable Class such that I can use that across the platforms. It is working fine in Windows Phone 8.1 App. But when it comes to Android, then it is showing the Viewmodel as null and DataContext as Null in debugger which breaks the application debugger. When I create another viewmodel and view to test the app, its working fine on android too. What can be the possible reasons.
EDIT : It is crashing due to the constructor , in which I am passing the business Logic instance. So , Constructor is necessary i think but in that case it is crashing.I am not trying to resolve the ViewModel , i am trying to resolve the Service instance in ViewModel and for the purpose of MVVM, I am keeping the Service out from Droid Project so base.OnCreate(bundle) does not come into scene anyways.
public BookViewModel(ILogic _logic)
{
logic = _logic;
//var ss= Mvx.Resolve<ILogic>();
//var x = Mvx.CanResolve<ILogic>();
_details = logic.Read();
}
Below is the Logic Code :
public class Logic : ILogic
{
#region Attributes
List<Detail.Detail> _details = new List<Detail.Detail>();
DataLayer.DataLayer dl = new DataLayer.DataLayer();
#endregion
#region .ctor
public Logic()
{
populateList();
}
#endregion
#region Methods
private void populateList()
{
_details = dl.Access();
}
Below is the App.cs in ViewModel in which CanResolve is giving False
public class App : Cirrious.MvvmCross.ViewModels.MvxApplication
{
#region Methods
public override void Initialize()
{
Mvx.RegisterType<ILogic, Logic>();
var ss = Mvx.CanResolve<ILogic>();
RegisterAppStart<ViewModels.BookViewModel>();
}
#endregion
}
There are a few questions and answers around similar to this - e.g. similar to MVVMCross ViewModel construction failure notifications
The basic answer is that MvvmCross cannot resolve the ViewModel during the constructor - you have to wait until after the base.OnCreate(bundle) call - at this point the ViewModel will be resolved.
There's also a bit more about when ViewModel's are located in Who should create view model instances in MvvmCross and CoreDispatcher.HasThreadAccess "breaking change" (and probably a few other places too)
After downloading the newest Version of mvvmcross (V3) I had some work to do, to upgrade some of my projects to the new state. Last thing I'm not able to fulfill is to pass a parameter to the viewmodel from the tabhost. In the older versions it worked fine (but it was different) and now I got an error.
But First here the Code (Line 19 makes trouble (watch comment in code), Line 18 works but only without Parameters):
[Activity]
public class MainActivity : MvxTabActivity
{
public new MainViewModel ViewModel
{
get { return (MainViewModel)base.ViewModel; }
set { base.ViewModel = value; }
}
protected override void OnViewModelSet()
{
SetContentView(Resource.Layout.Main);
TabHost.TabSpec spec;
Intent intent;
spec = TabHost.NewTabSpec("listeaktiv");
spec.SetIndicator(App.indicatorActive, Resources.GetDrawable(Resource.Drawable.green));
//spec.SetContent(this.CreateIntentFor(ViewModel.ListViewModel)); -> It works (But only without Parameters! How could I pass them here?)
spec.SetContent(this.CreateIntentFor<ListViewModel>(new { parameter = App.indicatorActive })); //Exception (on the next Line)
TabHost.AddTab(spec);
}
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
}
}
(App.indicatorActive is the Parameter I want to pass, its in the App.cs: (public static string indicatorActive = "Active";)
My ListViewModel looks like that:
public class ListViewModel : BaseViewModel
{
public ListViewModel(string parameter)
{
}
}
Error:
Unhandled Exception:
Cirrious.CrossCore.Exceptions.MvxException: Failed to load ViewModel for type
INApplikationsMonitor.Core.ViewModels.ListViewModel from locator MvxDefaultViewModelLocator
My guess is that this is just because you are using the old ViewModel lifecycle.
In v3:
the ViewModel constructor parameters are used for IoC - for Dependency Injection of services.
for passing parameters you need to instead use an Init method within the ViewModel
For more on this, see: http://slodge.blogspot.co.uk/2013/03/v3-new-viewmodel-lifecycle.html :
The default ViewModelLocator in v3 builds new ViewModel instances using a 4-step process - CIRS:
Construction - using IoC for Dependency Injection
Init() - initialisation of navigation parameters
ReloadState() - rehydration after tombstoning
Start() - called when initialisation and rehydration are complete