How to set the text of a TextView? - xamarin.android

I am trying to learn about Xamarin Android and I have some dubts.
I have a layoout, my .xaml file and my .cs file (I am not sure if this could be considerate my view model in the same way than in the WPF applications).
In my layout I have this:
<TextView
android:text="txtDireccionServicio"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txtDireccionServicio" />
but in the text I have a warning that tell me that is is a hardcode value, that I should to use a #string resource.
Well, I have in my .cs file this property:
private string txtDireccionServicio = "Prueba";
But I don't know how to use this propery in my layout. I have tried this:
android:text="#txtDireccionServicio"
But it shows "#txtDireccionServicio". But in this case the layout doesn't warn that it is a hardcode value.
So my question is, how could I bind the Text property of the TextView with the string property in my .cs file?
Thanks.

Android doesn't support Data-Binding in default . So we need to set the text by using Element.Property=xxx
In your activity
TextView tx = FindViewById<TextView>(Resource.Id.txtDireccionServicio);
tx.Text = "xxx";
Note:
We need to invoke the line tx.Text = "xxx"; if we change the value of text in runtime .

Related

How to bind a UITableView to a List<string> using MvvmCross in Xamarin iOS

I am working on an application that is Xamarin Native using MvvmCross. I have spent the afternoon looking for an example of how to bind a list of strings to a table view in iOS but every example is binding to a List of Models and not string. I know this can be done in android...
<Mvx.MvxListView android:id="#+id/lvErrors"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/lblCaption"
android:layout_alignParentLeft="true"
local:MvxBind="ItemsSource Errors" />
where "Errors" is a List of strings. However trying a similar approach in iOS fails to load data.
var set = this.CreateBindingSet<ViolationDetailsView, ViolationDetailsViewModel>();
var source = new MvxStandardTableViewSource(ErrorsTableView, "TitleText Errors;");
ErrorsTableView.Source = source;
set.Bind(source).To(vm => vm.Errors);
set.Apply();
Can anyone point me in the right direction to find an example of this?
I ended up finding the answer here. "If you want to bind to the whole string object then use a . period for whole object binding."
So...
var source = new MvxStandardTableViewSource(ErrorsTableView, "TitleText Errors;");
became...
var source = new MvxStandardTableViewSource(ErrorsTableView, "TitleText .;");
I knew it was something simple.

local:MvxLang failed to bind the resource text to TextView when it is part of itemtemplate of MvxListView in Xamarin app

Issue:- local:MvxLang failed to bind the resource text to TextView when it is part of itemtemplate of MvxListView in Xamarin app using localization feature in MVVMCross.
My application is Xamarin.android, using MVVMCross, used Localization feature of MVVMCross with resx file.
Running sample can be found here:https://github.com/pallaviak1/RestaurantBilling.Droid
I am getting localized string through code using below syntax in viewmodel (AllBillsViewModel) BillClickedCommand:-
_dialogService.ShowAlertAsync( string.Format(TextSource.GetText("InformationReceivedMessage"), bill.CustomerEmail, bill.AmountPaid), TextSource.GetText("InformationReceivedHeader"), TextSource.GetText("InformationReceivedButtonText"));
Also my main view page where local:mvxLang is button attribute, which shows button text from resource of selected culture, also works well.
<Button ... local:MvxLang="Text ViewBillsResourceText" local:MvxBind="Click NavigateAllBills" />
Problem:- However when I am using MvxLang in controls which are part of item template of MvxListView control the localized string is blank (not populated).
file:- RestaurantBilling.Droid\RestaurantBilling.Droid\Resources\layout\ListItem_Bill.axml
The control which is part of item template view looks like below:-
<TextView android:layout_alignParentBottom="true"
android:layout_marginLeft="50dp"
android:layout_marginRight="16dp"
android:layout_width="90dp"
android:layout_height="wrap_content"
local:MvxLang="Text CustomerEmailTextView" />
The mail list control looks like below:-
<Mvx.MvxListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
local:MvxBind="ItemsSource AllBills; ItemClick BillClickedCommand"
local:MvxItemTemplate="#layout/listitem_bill" />
CustomerEmailTextView resource key is present in resx files as Name: AllBillsViewModel.CustomerEmailTextView, value : "US Customer Email".
Just to add, my Localization code is as below:-
Resource files are present in library MVVMCross.Localization, whose reference is added in RestaurantBilling.core library as well as android project.
code in core, App.cs file as below:-
Mvx.RegisterSingleton<IMvxTextProvider>
(new ResxTextProvider(Strings.ResourceManager, currentCulture));
BaseViewModel has below code:-
public IMvxLanguageBinder TextSource =>
new MvxLanguageBinder("", GetType().Name);
Getting warning in visual studio output window as below:
[0:] MvxBind:Warning: 9.78 Unable to bind: source property source not found Property:TextSource on Bill
02-10 07:41:52.020 I/MvxBind ( 4357): 9.78 Unable to bind: source property source not found Property:TextSource on Bill
I could not find enough help on MVVMCross formal website, also could not find much discussion points on same. The same thing is working in sample downloaded "My Trains" when referred from pluralsight training.
Please help.
my problem is resolved. ref link: How to bind ItemClick in MvxListView in MvxListView
This link issue is somewhat similar. I resolved the issue in below way,
whatever TextSource property I have added in viewModel, I need to put in Bill.cs class which is model
public IMvxLanguageBinder TextSource
{
get {
//Mvx.Trace("****************TextSource get in bill.cs**************************");
return new MvxLanguageBinder("", GetType().Name);
}
}
Then, the resource text key looks like below:-
Bill.CustomerEmailTextView
This is kind of workaround, actually BaseViewModel has TextSource property however it is not useful in case of item template case.
Please let me know if you have better solution because we are kind of mixing viewmodel and model properties.

How to bind the value of slider from C# code in windows phone 8.1..?

I want to bind a custom seek bar to the position of media element.
From XAML we can achieve it by
Value="{Binding ElementName=mediaElement, Path=Position, Mode=TwoWay, Converter={StaticResource FormatConverter}}"
where FormatConverter is a converter class written to convert mediaElement object to slider value during binding.
But I am creating the media element from code, so I want to write C# code to achieve the above. How to do it..?
Please take a look at MSDN. I think this should work, if done like this:
Binding myBind = new Binding() { ElementName = "mediaElement", Path = new PropertyPath("Position"), Converter = this.Resources["FormatConverter"] as IValueConverter, Mode = BindingMode.TwoWay };
mySlider.SetBinding(Slider.ValueProperty, myBind);
Of course you need to clarify which Resources you use - page's or app's.

Android MapFragment as custom control in MvvmCross

I'm working on a project in which I can use MapFragment or SupportMapFragment as a custom control.
I tried to mix custom control(N-18) and Fraggle(N-26) tutorial but I can't make it work.
My Custom Control Class:
public class CustomMapView : MvxFragment
{
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var ignored = base.OnCreateView(inflater, container, savedInstanceState);
return this.BindingInflate(Resource.Layout.CustomMapViewLayout, null);
}
}
My CustomMapViewLayout.axml
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
In the FirstView.axml I reference the CustomMapView:
<cc.CustomMapView
android:layout_width="match_parent"
android:layout_height="match_parent" />
(I use the ViewNamespaceAbbreviations in the Setup.cs)
When I try to deploy I get these errors:
MvxBind:Error View Type Not Found - cc.CustomMapView (I'm sure the namespace is correct)
Android.Views.InflateException:Loading...
Is there anybody who managed to solve this kind problem?
As far as I know, fragments have to be used within FragmentActivity parents and have to be loaded either using the fragment xml tag or using one of the (support) fragment manager methods for dynamic loading within a parent viewgroup.
The Mvx code which is reporting View Type Not Found - cc.CustomMapView definitely won't work - it's looking for a class which inherits Android View in that namespace, not looking for a Fragment.
There may be ways to write more 'dynamic' fragment xml code - but none of the mvvmcross contributors have unearthed any of these yet. For now, I think you'll have to use the Fragments more 'conventionally' - like the ways shown in the N=26 sample

MVVMCross - MvxBind: Combine/Concat Properties

I have a little question:
In my axml Designer I have something like this:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
local:MvxBind="{'Text':{'Path':'FirstName'}}" />
That works fine, but how could I combine/concat 2 Properties (or even more)..
So something like: FirstName + SecondName (2 Properties in one Text)
This is a standard Mvvm question. I think it came up a lot in the early years of Wpf - I think there are lots of ideas around about multiple dependencies.... I've not implemented any of these yet...
If you want to do this, then you could use:
(1) expose a combination property on the ViewModel object:
public string FullName
{
get
{
return FirstName + SecondName;
}
}
if you do this, then you'll need to make sure that when you RaisePropertyChanged("FirstName") or RaisePropertyChanged("SecondName"), then you also RaisePropertyChanged("FullName")
(2) Use a converter to combine the names together:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
local:MvxBind="{'Text':{'Path':'','Converter':'MakeFullName'}}" />
Note that the Converter here takes the parent object as its input parameter.
Note that in this case, if FirstName or SecondName change then the text view might not get updated :/
(3) You could use multiple TextViews in the UI - each one bound to the necessary bit of text.
(4) You could use a single textview and use C# level binding - e.g. in the View use code like:
ViewModel.PropertyChanged += (s,e) => {
if (e.PropertyName == "FirstName" || e.PropertyName == "SecondName")
{
this.FindViewById<TextView>(Resource.Id.XXX).Text = ViewModel.FirstName + ViewModel.SecondName;
}
}
If you think multi-dependency bindings are an important requirement, please log this as an issue (feature request) in https://github.com/slodge/MvvmCross/issues - or maybe even just fork the code and add them :)

Resources