Skip to content

Instantly share code, notes, and snippets.

@descorp
Last active November 2, 2015 10:53
Show Gist options
  • Select an option

  • Save descorp/82fbb29fe9e490cf8c1e to your computer and use it in GitHub Desktop.

Select an option

Save descorp/82fbb29fe9e490cf8c1e to your computer and use it in GitHub Desktop.
Mvvmcross example
using Cirrious.MvvmCross.ViewModels;
using System.Collections.ObjectModel;
using System.Windows.Input;
using System;
using Cirrious.MvvmCross.Plugins.Messenger;
namespace MvvmcrossTest.Core.ViewModels
{
public class FirstViewModel
: MvxViewModel
{
IMvxMessenger mesages;
MvxSubscriptionToken token1;
MvxSubscriptionToken token2;
void DoDeleteItem(DeleteMessage e)
{
this.Collection.Remove(e.Sender as TableItem);
}
public FirstViewModel(IMvxMessenger mesages)
{
this.mesages = mesages;
token1 = this.mesages.Subscribe<DeleteMessage>(DoDeleteItem);
token2 = this.mesages.Subscribe<AddMessage>(e => this.DoAddItemCommand());
}
private string _hello = "Hello MvvmCross";
public string Hello
{
get { return _hello; }
set
{
_hello = value;
RaisePropertyChanged(() => Hello);
}
}
public ObservableCollection<TableItem> Collection { get; set; } = new ObservableCollection<TableItem>(
new[]
{
new TableItem() {Title = "item 1", Group = 1},
new TableItem() {Title = "item 2", Group = 1},
new TableItem() {Title = "item 3", Group = 4},
new TableItem() {Title = "item 4", Group = 1},
new TableItem() {Title = "item 5", Group = 2},
new TableItem() {Title = "item 6", Group = 3},
new TableItem() {Title = "item 7", Group = 3}
} );
private MvxCommand _addItemCommand;
public ICommand AddItemCommand
{
get
{
return _addItemCommand ?? (_addItemCommand = new MvxCommand(DoAddItemCommand));
}
}
private Random random = new Random();
private void DoAddItemCommand()
{
this.Collection.Add(new TableItem() { Title = this.Collection.Count.ToString(), Group = this.random.Next(5) });
this.RaisePropertyChanged(() => this.Collection);
}
}
public class DeleteMessage : MvxMessage
{
public DeleteMessage(TableItem item)
: base(item)
{
}
}
public class AddMessage : MvxMessage
{
public AddMessage(object item)
: base(item)
{
}
}
}
using Cirrious.MvvmCross.ViewModels;
using Cirrious.MvvmCross.Views;
using System.Windows.Input;
namespace MvvmcrossTest.Core.ViewModels
{
public class TableItem : MvxNotifyPropertyChanged
{
private string title;
public string Title
{
get
{
return this.title;
}
set
{
this.title = value;
this.RaisePropertyChanged(() => this.Title);
}
}
private int group;
public int Group
{
get
{
return this.group;
}
set
{
this.group = value;
this.RaisePropertyChanged(() => this.Group);
}
}
}
}
MvvmcrossTestusing Android.App;
using Android.OS;
using Cirrious.MvvmCross.Droid.Views;
namespace MvvmcrossTest.Droid.Views
{
[Activity(Label = "View for FirstViewModel")]
public class FirstView : MvxActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.FirstView);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="40dp"
local:MvxBind="Text Hello" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="40dp"
local:MvxBind="Text Hello" />
<MvxListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
local:MvxBind="ItemSource Collection; ItemClick SelectDayOfWeekCommand"
local:MvxItemTemplate="@layout/listItem"
android:id="@+id/listView" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="20dp">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="fdsfs"
local:MvxBind="Text Title"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
local:MvxBind="Text Group"
android:text="fdsfs" />
</LinearLayout>
using UIKit;
using Cirrious.MvvmCross.Touch.Views;
using Cirrious.MvvmCross.Binding.BindingContext;
using MvvmcrossTest.Core.ViewModels;
namespace MvvmcrossTest.Touch.Views
{
public partial class FirstView : MvxViewController<FirstViewModel>
{
public FirstView()
: base("FirstView", null)
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
var tableView = new UITableView(UIScreen.MainScreen.Bounds, UITableViewStyle.Grouped);
var source = new TableSource(tableView, "TitleText Title", UITableViewCellStyle.Subtitle, true);
var addBtn = new UIBarButtonItem("Add", UIBarButtonItemStyle.Bordered, (a, b) =>
this.ViewModel.AddItemCommand.Execute(null));
this.View.Add(tableView);
var set = this.CreateBindingSet<FirstView, FirstViewModel>();
set.Bind(source).To(vm => vm.Collection); // handmade control
set.Bind(this.PassText).To(vm => vm.Hello); // control defined in .xib layout
set.Bind(addBtn).For(e => e.Title).To(vm => vm.Hello);
set.Apply();
tableView.Source = source;
tableView.ReloadData();
this.SetToolbarItems(new[] { addBtn }, true);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment