How to import CreateDataBinding into Xamarin - android

I am new to MVVMCross concept and Hybride development.
In my project I have a .xml file with a UI-Component that has a listener attribute "o'clock" for example.
I want to bind the o'clock to the ViewModel. I check some posts in the internet and I found the one shown below.
The question is, how can I import or have access to:
CreateBindingSet and DelayBind
into the project because I do not have access to it.
code:
this.DelayBind(() => {
var set = this.CreateBindingSet<KittenCollectionCell,
Kitten>();
set.Bind(NameLabel).To(kitten => kitten.Name);
set.Bind (PriceLabel).To (kitten => kitten.Price);
set.Bind (_loader).To (kitten => kitten.ImageUrl);
set.Bind(MyAwesomeButton).To(vm => vm.MyAwesomeCommand);
set.Apply();
});

Both CreateBindingSet and DelayBind are extension methods living in namespace MvvmCross.Binding.BindingContext.
In order to use them, add a using statement like so on top of you file:
using MvvmCross.Binding.BindingContext;

Related

ReactNative-What's an easy way to implement PageView analytics across all pages?

I am implementing Microsoft's AppCenter for Crashalytics and Analytics. In Native development, I am able to add two lines of code and it adds pageview analytics (track each time a page is viewed) to every page in the app. I am curious to know if there's an easy way I can do it in a React Native app as well. This is what it looks like on native:
In native iOS, I just create a BaseViewController and make sure every ViewController inherits from that class. And then in the ViewDidLoad of the BaseViewController I add something like the line below.
And in native Android, I just create a BaseFragment and make sure every Fragment inherits from that class. And then in the OnCreate of the BaseFragment I add something like the line below:
Analytics.TrackEvent("PageView: " + this.GetType().Name)
How can I implement something similar on React Native? I am guessing that since I am using a across all pages, I could somehow create a BaseSafeAreaView that I can then add similar analytics code in the UseEffect?
How about Mixpanel ? You can implement a fully blown analytic solution.
They have a react native integration
segment is also great. They provide support for [react native integration] as well. You can integrate segment with mixpanel if necessary.
Firebase analytics is also a good solution. This can be done with rnfirebase analytics package
Since I am using react-navigation, I was able to use this with my NavigationContainer so I didn't need any base class
...
export default () => {
const navigationRef = useNavigationContainerRef();
const routeNameRef = useRef();
return (
<NavigationContainer
ref={navigationRef}
onReady={() => {
routeNameRef.current = navigationRef.getCurrentRoute().name;
}}
onStateChange={async () => {
const previousRouteName = routeNameRef.current;
const currentRouteName = navigationRef.getCurrentRoute().name;
if (previousRouteName !== currentRouteName) {
await Analytics.trackEvent('PageView: ' + currentRouteName);
}
routeNameRef.current = currentRouteName;
}}
>
...

What is the best approach for many routes / pages and self-made template in Flutter, for mobile?

I started to build an app in Android Studio using Flutter (Dart). I
want to insert into the app a summary of the two books, so one it will have
35 routes (or pages) and the other another 35 routes (or pages).
At this moment my app looks like this: I have a Intro_Page with a
button that routes to the Main_Page. From the Main_Page there are
two buttons: one for BookA, the other for BookB. After entering the BookA the user, after the text, will find another
button to the next page, BookA_1 and on this page another button
to BookA_2 etc.
My question are:
Is there any way to use a template that can be reused for each page?
I have to put all the code into the main.dart file or I can make a dart file for each page and navigate between them with
buttons?
Is there any possible way to create a self-made template that it uses the design layout in all the pages so I don't have to rewrite the code for
one of each? Just the layout not the content (the text).
Is there any other better way to do this?
PS: Any other suggestion will be helpful.
Thank you very much if you answer this question!
You can create one generic class for routing, in that you can just reduce the navigation code.
You can create one class like this
import 'package:flutter/material.dart';
class MyNavigator {
static navigate({BuildContext context, Widget page}) async {
await Navigator.of(context).push(
MaterialPageRoute(builder: (_) {
return page;
}),
);
}
}
And use it as
MyNavigator.navigate(context: context, page: BookA());
I hope it will help you.
------------------------------------ UPDATE ---------------------------------
Also you can use extension methods as well.
For this to work, you'll need Dart 2.7 minimum!
Implementation for extension method is as follows
import 'package:flutter/material.dart';
extension NavigatorHelper on Widget {
goHere(BuildContext context) async {
await Navigator.of(context).push(
MaterialPageRoute(builder: (_) => this),
);
}
}
And you can use it as
BookA().goHere(context);
In my opinion extension method is good way.
You can use both.

Using UserDialogs in Android

I'm stucked with usage of plugin Acr.UserDialogs in android app, based on MVVMCross.
In PCL project i used IUserDialog in viewmodel constructor injection.
I have installed Acr.UserDialogs package both in PCL and in Droid project, but when i run app, it throws:
In android, you must call UserDialogs.Init(Activity) from your first
activity OR UserDialogs.Init(App) from your custom application OR
provide a factory function to get the current top activity via
UserDialogs.Init(() => supply top activity)
I tryed to call in my viewModel:
UserDialogs.Init(this);
But Init is not recognized
And calling of UserDialogs.Instance.Loading ().Hide(); in app throws the same issue.
How it should be initialized in android project?
Upd: Final solution to workaround this looks like:
In PCL project App.cs add: Mvx.RegisterSingleton(() =>
UserDialogs.Instance);
In Your first loaded activity in OnCreate
add: UserDialogs.Init(() => this);
This error is very clearly. You can't initialize it in viewModel, You can only do that in your main activity.
FAQ
I'm getting a nullreferenceexception when using loading.
This happens when you run loading (or almost any dialog) from the
constructor of your page or viewmodel. The view hasn't been rendered
yet, therefore there is nothing to render to.
Android Initialization In your MainActivity
UserDialogs.Init(this);
OR UserDialogs.Init(() => provide your own top level activity provider)
OR MvvmCross - UserDialogs.Init(() => Mvx.Resolve<IMvxTopActivity>().Activity)
OR Xamarin.Forms - UserDialogs.Init(() => (Activity)Forms.Context)
GitHub docs.

Global Variable / Constant in React Native

is there a way in React Native that I can define on a global variable all the strings that I will be using like in Android Development there is a String.xml where you can put all of your strings.
What' I've done is create a globals module...
// File: Globals.js
module.exports = {
STORE_KEY: 'a56z0fzrNpl^2',
BASE_URL: 'http://someurl.com',
COLOR: {
ORANGE: '#C50',
DARKBLUE: '#0F3274',
LIGHTBLUE: '#6EA8DA',
DARKGRAY: '#999',
},
};
Then I just require it at the top...
const GLOBAL = require('../Globals');
And access them like so...
GLOBAL.COLOR.ORANGE
_____________________
UPDATE on Feb 10, 2018
This seems to be a pretty popular and useful answer, so I thought I should update it with the more current syntax. The above still works in CommonJS module systems, but now days you're just as likely to run into ES6 and importmodules rather than require them.
ECMAScript Modules (ESM) Syntax
// File: Globals.js
export default {
STORE_KEY: 'a56z0fzrNpl^2',
BASE_URL: 'http://someurl.com',
COLOR: {
ORANGE: '#C50',
DARKBLUE: '#0F3274',
LIGHTBLUE: '#6EA8DA',
DARKGRAY: '#999',
},
};
// to use...
import GLOBALS from '../Globals'; // the variable name is arbitrary since it's exported as default
// and access them the same way as before
GLOBALS.COLOR.ORANGE
global in react native is like the window in web development.
// declare a global varible
global.primaryColor = '***';
//now you can use this variable anywhere
console.log(primaryColor);
I too made a module like in Chris Geirman's answer, but was not able to reference it with the require. Instead I got it to work with import * as GLOBAL from '../Globals';
If you want to switch between languages depening on platform localisation.
fetch node_module via npm
npm i react-native-localization --save
Define variables in class:
// Localisation.js
let LocalizedStrings = require ('react-native-localization');
let strings = new LocalizedStrings ({
en: {
loginTitle: "Login",
},
de: {
loginTitle: "Anmelden",
}
})
When you need the strings:
var STRINGS = require ('./Localization');
<Text>{STRINGS.loginTitle}</Text>

Titanium : can't get the current window in tabbed app

I need to get current window in the tabbed application.
I tried with
var win = Ti.UI.currentTab.window();
and with
var win = Ti.UI.currentWindow;
but get the Uncaught TypeError: Cannot call method 'window' of undefined error in both cases.
If you are using TabGroup, you can access current window through activeTab property. It's not global function, so you have to have keep reference to TabGroup object in your code:
var tabGroup = Ti.UI.createTabGroup({ tabs: createTabs() });
tabGroup.activeTab.window
currentTab and currentWindow are holdovers from when you could spin off multiple JavaScript contexts from a Ti.UI.createWindow({ url: 'foo.js' }). I'd recommend you don't use them.
There are a couple of patterns to choose from when you need to communicate from one part of your app to the UI at another part.
1st option: listen for and fire events a la Ti.App.addEventListener('foo', function(data){}) and Ti.App.fireEvent('foo', { }). This decouples the code nicely, so you can move the UI handling code around without changing the firing code.
2nd option: expose the UI objects through modules. In your controller, exports.win = $.win;.
3rd option: set a global object: Alloy.Globals.win = $.win;.
ok so in alloy framework, we just use $ sign to get all the controls which are created in .xml file...
so if you set id of your window tag in .xml file then you can get current window with below example...
.xml file
<Alloy>
<Window id="win">
</Window>
</Alloy>
.js file
var win = $.win
so from above code you can get your current window.
Hope you get it.

Categories

Resources