Skip to content

Instantly share code, notes, and snippets.

@reuniware
Last active March 10, 2020 16:11
Show Gist options
  • Select an option

  • Save reuniware/f700b9bdf771c2228bd7daed8f6fd01a to your computer and use it in GitHub Desktop.

Select an option

Save reuniware/f700b9bdf771c2228bd7daed8f6fd01a to your computer and use it in GitHub Desktop.
Xamarin Forms : Basic ViewModel (MVVM)
using System;
using System.ComponentModel;
using Xamarin.Forms;
namespace XamarinFormsTest01
{
public class ItemCountViewModel : INotifyPropertyChanged
{
private static ItemCountViewModel instance = new ItemCountViewModel();
public static ItemCountViewModel GetInstance()
{
return instance;
}
int itemCount;
public event PropertyChangedEventHandler PropertyChanged = delegate { };
public ItemCountViewModel()
{
ItemCount = 0;
/*Device.StartTimer(TimeSpan.FromSeconds(1), () =>
{
ItemCount++;
return true;
}
);*/
}
public int ItemCount
{
set
{
itemCount = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("ItemCount"));
}
get
{
return itemCount;
}
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:maps="clr-namespace:Xamarin.Forms.Maps;assembly=Xamarin.Forms.Maps"
xmlns:local="clr-namespace:XamarinFormsTest01"
mc:Ignorable="d"
x:Class="XamarinFormsTest01.MainPage">
<StackLayout>
<Label x:Name="lblMain" Text="{Binding ItemCount}"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand" >
<Label.BindingContext>
<local:ItemCountViewModel />
</Label.BindingContext>
</Label>
<Button x:Name="BtnStart" Text="start" Pressed="BtnStart_Pressed" />
<Button x:Name="BtnStop" Text="stop" Pressed="BtnStop_Pressed" />
<!--<maps:Map></maps:Map>-->
</StackLayout>
</ContentPage>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Xamarin.Essentials;
using Xamarin.Forms;
using Xamarin.Forms.Maps;
using Map = Xamarin.Forms.Maps.Map;
namespace XamarinFormsTest01
{
// Learn more about making custom code visible in the Xamarin.Forms previewer
// by visiting https://aka.ms/xamarinforms-previewer
[DesignTimeVisible(false)]
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
lblMain.BindingContext = ItemCountViewModel.GetInstance();
}
private void BtnStart_Pressed(object sender, EventArgs e)
{
httprequest();
}
private void BtnStop_Pressed(object sender, EventArgs e)
{
}
private async void showmsg()
{
var action = await DisplayActionSheet("Select an action", "OK", "CANCEL", "Action 1", "Action 2", "Action 3");
await DisplayAlert("Alert", "action = " + action, "OK");
}
private void httprequest()
{
Device.StartTimer(TimeSpan.FromSeconds(1), () =>
{
ItemCountViewModel.GetInstance().ItemCount++;
HttpWebRequest h = WebRequest.Create("https://www.google.com/search?q=reuniware") as HttpWebRequest;
HttpWebResponse r = h.GetResponse() as HttpWebResponse;
using (var reader = new System.IO.StreamReader(r.GetResponseStream(), Encoding.UTF8))
{
string responseText = reader.ReadToEnd();
}
return true;
}
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment