How to bind Map/Image in a List with Click event? - xamarin.ios

I want to bind map with a field and its click event as well, which will take it to a MvxCommand and show some MapViewModel.
[Register("HoursEntryCell")]
public class HoursEntryCell : MvxTableViewCell
{
public HoursEntryCell()
{
CreateLayout();
InitializeBindings();
}
public HoursEntryCell(IntPtr handle)
: base(handle)
{
CreateLayout();
InitializeBindings();
}
private UILabel hours;
private UIImageView imageView;
private UILabel jobName;
private MKMapView location;
private void CreateLayout()
{
jobName = new UILabel(new RectangleF(10, 10, 100, 30));
jobName.AdjustsFontSizeToFitWidth = true;
jobName.Lines = 0;
jobName.Font = jobName.Font.WithSize(16);
imageView = new UIImageView(UIImage.FromBundle("pencil.png"));
imageView.Frame = new RectangleF(270, 10,imageView.Image.CGImage.Width, imageView.Image.CGImage.Height);
Accessory = UITableViewCellAccessory.DisclosureIndicator;
location = new MKMapView(new RectangleF(15, 40, 280, 160));
location.AddAnnotation(new MKPointAnnotation()
{
Title = "My Loc",
Coordinate = new CLLocationCoordinate2D(23.0092509, 72.5061084)
});
location.UserInteractionEnabled = false;
salaryLable.Text = "Salary";
hours = new UILabel(new RectangleF(200,200,50,50));
ContentView.AddSubviews(jobName, location, hours,salaryLable, imageView);
}
private void InitializeBindings()
{
this.DelayBind(() =>
{
var set = this.CreateBindingSet<HoursEntryCell, ListViewModel>();
set.Bind(location).To(vm => vm.MyLocation);
set.Bind(hours).To(vm => vm.Salary);
set.Bind(jobName).To(vm => vm.EmployeeName);
set.Apply();
});
}
}
}
I want to achieve something like set.Bind(location).To(vm => vm.GoNextCommand); along with the map (set.Bind(location).To(vm => vm.MyLocation);)
Or How can I bind simple image button click event to a MvxCommand from the list?
How can I go for it?
Need Help.

You'll probably need to do a combination of things to get this to work properly...
1.) Do your binding from the view that has your list view. In the example above how does the cell actually get access to your view model? Look at the example here: https://github.com/MvvmCross/MvvmCross-Tutorials/blob/master/DailyDilbert/DailyDilbert.Touch/Views/ListView.cs#L16
2.) you'll need to probably create custom bindings to handle the map view or it might be similar to this example from Stuart: MvvmCross iOS: How to bind MapView Annotation to jump to another view?

Related

sfChart reset data for graph

I have a graph in a 'detail' view. this is accessed when an item is chosen in a tableview. the first graph is correct, but when I pick another item in the table, it still shows the first graph. I enabled ListenPropertyChange = true and raispropertychanged it twice.
view(viewdidload):
SFChart chart = new SFChart();
chart.Frame = this.headerview.Frame;
//Adding Primary Axis for the Chart.
SFCategoryAxis primaryAxis = new SFCategoryAxis();
chart.PrimaryAxis = primaryAxis;
//Adding Secondary Axis for the Chart.
SFNumericalAxis secondaryAxis = new SFNumericalAxis();
chart.SecondaryAxis = secondaryAxis;
SFLineSeries series = new SFLineSeries()
{
XBindingPath = "timestamp",
YBindingPath = "price_btc",
ListenPropertyChange = true
};
series.EnableTooltip = true;
chart.Series.Add(series);
set.Bind(series).For(s => s.ItemsSource).To(vm => vm.CoinHistory);
set.Apply();
viewModel(init):
CoinHistory = new ObservableCollection<ChartDataModel>(_CoinHistoryGraph.Select(x => new ChartDataModel(float.Parse(x.price_btc), x.timestamp)));
RaisePropertyChanged(() => CoinHistory);
ViewModel properties:
private ObservableCollection<ChartDataModel> _CoinHistory;
public ObservableCollection<ChartDataModel> CoinHistory
{
get
{
return _CoinHistory;
}
set
{
_CoinHistory = value;
RaisePropertyChanged(() => CoinHistory);
}
}
How do you pass your model data to the details view? According to your descriptions, the first ViewModel should hold the whole data. When user click one cell, the tableView source's SelectionChangedCommand event will trigger. Normally we bind this command to the corresponding ViewModel's command, then we can configure the push and pass the parameters there.
Here is my bind in the first view which contains a UITableView:
var set = this.CreateBindingSet<FirstView, FirstViewModel>();
set.Bind(source).For(s => s.ItemsSource).To(vm => vm.ItemsGroup);
set.Bind(source).For(s => s.SelectionChangedCommand).To(vm => vm.ShowDetailsCommand);
set.Apply();
When user click one cell in the TableView, ShowDetailsCommand will fire in the FirstViewModel:
private readonly Lazy<IMvxNavigationService> _navigationService = new Lazy<IMvxNavigationService>(Mvx.Resolve<IMvxNavigationService>);
private MvxCommand<Item> showDetailsCommand;
public ICommand ShowDetailsCommand
{
get
{
return showDetailsCommand ?? (showDetailsCommand = new MvxCommand<Item>(showDetails));
}
}
async void showDetails(Item item)
{
// This item is bound to the ItemsSource through ItemsGroup
await _navigationService.Value.Navigate<SecondViewModel, Item>(item);
}
Then the second ViewModel can accept this item via:
public class SecondViewModel : MvxViewModel<Item>
{
private List<CoinHistoryModel> _CoinHistory;
public List<CoinHistoryModel> CoinHistory
{
get
{
return _CoinHistory;
}
set
{
_CoinHistory = value;
RaisePropertyChanged(() => CoinHistory);
}
}
public override void Prepare(Item parameter)
{
CoinHistory = parameter.SingleCoinHistory;
}
}
At last the details view will show the CoinHistory, if you have successfully bound it to the second view model.
Here is my demo for you referring to.
I was not able to replicate the issue you have stated here, instead I have prepared a sample based on your requirement and you can find the sample from the below link.
Sample
If this does not resolve your query, can you revert me back with more details.
Hope this helps
Note: I work for Syncfusion.

MonoTouch DialogViewController - why must it be in the first place of a UINavigationController?

I want to use a DialogViewController inside of a UITabViewController.
Problem: Nested elements don't show a navigation bar, and so it is not possible to go back.
When I push my class (inherited from DialogViewController) to a UINavigationController, then the behavior is correct. If I use the same class in a tab of a UITabViewController (even with an underlying UINavigationController), then the behaviour is wrong.
Can anyone help me out?
Although the question is not assisted with some code sample, I made a small example hoping to solve your question. For this example I used the Tabbed Application template which comes with Xamarin.iOS and named it TabbingTest.
The following code goes in the AppDelegate. Change the FinishedLaunching method to contain:
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
window = new UIWindow (UIScreen.MainScreen.Bounds);
var viewControllers = new UIViewController[]
{
CreateTabFor("Test", "first", new TestDialogController ()),
CreateTabFor("Second", "second", new SecondViewController ()),
};
tabBarController = new UITabBarController ();
tabBarController.ViewControllers = viewControllers;
tabBarController.SelectedViewController = tabBarController.ViewControllers[0];
window.RootViewController = tabBarController;
window.MakeKeyAndVisible ();
return true;
}
Then add the following methods:
private int _createdSoFarCount = 0;
private UIViewController CreateTabFor(string title, string imageName, UIViewController view)
{
var controller = new UINavigationController();
controller.NavigationBar.TintColor = UIColor.Black;
var screen = view;
SetTitleAndTabBarItem(screen, title, imageName);
controller.PushViewController(screen, false);
return controller;
}
private void SetTitleAndTabBarItem(UIViewController screen, string title, string imageName)
{
screen.Title = NSBundle.MainBundle.LocalizedString (title, title);
screen.TabBarItem = new UITabBarItem(title, UIImage.FromBundle(imageName),
_createdSoFarCount);
_createdSoFarCount++;
}
Create a class named TestDialogController and paste the following code inside.
using System;
using MonoTouch.Dialog;
using MonoTouch.UIKit;
namespace TabbingTest
{
public class TestDialogController : DialogViewController
{
public TestDialogController (): base(UITableViewStyle.Plain,null,false)
{
var root = new RootElement ("Tabbing test"){
new Section (){
new RootElement ("First level", 0, 0) {
new Section (null, "This is the first level."){
new RootElement ("Second level", 0, 0) {
new Section (null, "This is the second level."){
new BooleanElement ("Flipflops", false)
}
}
}
}}
};
this.Root = root;
}
}
}
Now run the application.
You can see that even the nested elements show up nicely in the navigation bar. Even with multilevel nesting.

Section.HeaderView is resizing automatically in Monotouch Dialog

I'm using Monotouch.Dialog and
I have subclassed the class Section to implement a custom HeaderView. The problem is that the header view is automatically resized, here's the code I'm using :
public class MySection : Section
{
public MySection ()
{
UIView v = new UIView(new RectangleF(0,0,30,30));
v.BackgroundColor = UIColor.Red;
this.HeaderView = v;
}
}
I've attached the screen shot :
http://imageshack.us/photo/my-images/864/capturedcran20120508212.png
EDIT
Ok I've kind of find the way to resolve this
The trick is to add a View that will contain your real HeaderView and sets the AutosizesSubviews property to false. Then just add the view to the latest :
public class MySection : Section
{
public MySection ()
{
UIView v0 = new UIView(new RectangleF(0,0,50, 60));
v0.AutosizesSubviews = false;
UIView v = new UIView(new RectangleF(0,0,30,30));
v.BackgroundColor = UIColor.Red;
v0.AddSubview(v);
this.HeaderView = v0;
}
}

Monotouch Dialog Custom Element and GetHeight not being called

Using Monotouch 5.2.11 iOS
I have followed this tutorial (http://tirania.org/monomac/archive/2011/Jan-18.html), created a custom cell with an image and have also added the IElementSizing Interface. The GetHeight is never called.
Similar questions have been asked and the generally accepted solution was to make sure and create the RootElements first, set the UnEvenRows=true before adding them to the Controller. This did not work. I've tried that as well as about every other combination of adding sections to root elements and have not ever seen the GetHeight fired.
The MyDataElement is an image that is 320x200 which displays fine, but the string element that comes after it is not shown (assuming it is behind it). Consequently if I drag the custom cell up above the top, it disappears, reappears, and the second stringelement displays on top of it.
Here is the code I've tried:
public class MyDataElement : Element, IElementSizing {
static NSString key = new NSString ("myDataElement");
public MyData MyData;
public MyDataElement (MyData myData) : base (null)
{
MyData = myData;
}
public float GetHeight (UITableView tableView, NSIndexPath indexPath){
return 200f; // break point here is never hit ever
}
public override UITableViewCell GetCell (UITableView tv)
{
var cell = tv.DequeueReusableCell (key) as MyDataCell;
if (cell == null)
cell = new MyDataCell (MyData, key);
else
cell.UpdateCell (MyData);
return cell;
}
public partial class TableTester : DialogViewController
{
public TableTester () : base (UITableViewStyle.Grouped, null)
{
var re = new RootElement("Sample") {
new Section("Testy") {
new MyDataElement(new MyData() { stuff="hello"}),
new StringElement("Sample")
}
};
re.UnevenRows = true;
this.Root = re;
//this.Root = root;
}
}
In addition to that I've even done this which didn't work either:
public class TestNavigator : UINavigationController {
public TestNavigator() {
TabBarItem = new UITabBarItem("Test", null, 1);
var re = new RootElement("Sample") {
new Section("Sammy") {
new StringElement("Sample"),
new MyDataElement(new MyData() { stuff="sam"}),
new StringElement("Sample 2")
}
};
re.UnevenRows = true;
var dv = new DialogViewController(re);
re.UnevenRows = true;
PushViewController(dv, true);
}
After plenty of trial and error, I had to make sure and remove the reference to Monotouch.Dialog that I had downloaded from github and use the built in reference. It seems that the getheight maybe broken in github.

Monotouch Dialog Datepicker and RadioGroup Navigationitem

I can´t figure out how to correctly design the pushed viewcontrollers navigation back item of a RadioGroup.
So in the follow up screen where the languages are selectable the back Button says "Settings" and is blue. But I want to make it say back and change its design which mechanisms exists and I´m already using in other screens.
I build it up like this:
var rootSettings = new RootElement ("Settings");
var sectionNotificationSettings = new Section ("Notification settings");
BooleanElement pushEnabled = new BooleanElement("Push notifications", settings.PushEnabled);
sectionNotificationSettings.Add(pushEnabled);
var sectionCountrySettings = new Section("Country settings");
var rootRadioGroup = new TransparentRootElement ("Language", new RadioGroup("languages", 0));
var sectionRadioElements = new Section("");
foreach(var language in settings.Languages)
{
RadioElement selectableLanguage = new RadioElement(language.Key, "languages");
sectionRadioElements.Add(selectableLanguage);
}
rootRadioGroup.Add(sectionRadioElements);
sectionCountrySettings.Add(rootRadioGroup);
rootSettings.Add (sectionNotificationSettings);
rootSettings.Add(sectionCountrySettings);
And here I define the TransparentRootElement where I thought I can edit the navigation Item:
public class TransparentRootElement : RootElement {
public TransparentRootElement (string caption) : base (caption)
{
}
public TransparentRootElement (string caption, Group radioGroup ) : base (caption, radioGroup)
{
}
public override void Selected (DialogViewController dvc, UITableView tableView, NSIndexPath path)
{
base.Selected (dvc, tableView, path, true);
}
void HandleMenuButtonTouchUpInside (object sender, EventArgs e)
{
_dvc.NavigationController.PopViewControllerAnimated(true);
}
public override UITableViewCell GetCell (UITableView tv)
{
var cell = base.GetCell (tv);
//cell.BackgroundColor = UIColor.White;
return cell;
}
}
I tried many edit approaches but none of the worked. I also began editing the Elements.cs in Monotouch Dialog but this also did not helped me alot.
Anyone who has a suggestion?
Thank you very much!
I believe to change the navigation's back button, you have to hide the default back button and then assign your own. (Right before you push the new view controller, I believe in your case, it would be before you push the Language screen) Something like this:
toPushscreen.NavigationItem.SetHidesBackButton(true, true);
toPushscreen.NavigationItem.LeftBarButtonItem = new UIBarButtonItem("Back", UIBarButtonItemStyle.Plain, myHandler);
currentScreen.NavigationController.PushViewController(toPushscreen, true);
Hope this helps!

Resources