mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-18 20:08:15 +08:00
添加xamarinFormApp例子,安卓,iOS,WPF
This commit is contained in:
@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Windows.Input;
|
||||
using Xamarin.Essentials;
|
||||
using Xamarin.Forms;
|
||||
|
||||
namespace xamarinFormApp.ViewModels
|
||||
{
|
||||
public class AboutViewModel : BaseViewModel
|
||||
{
|
||||
public AboutViewModel()
|
||||
{
|
||||
Title = "About";
|
||||
OpenWebCommand = new Command(async () => await Browser.OpenAsync("https://aka.ms/xamarin-quickstart"));
|
||||
}
|
||||
|
||||
public ICommand OpenWebCommand { get; }
|
||||
}
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
using Xamarin.Forms;
|
||||
|
||||
using xamarinFormApp.Models;
|
||||
using xamarinFormApp.Services;
|
||||
|
||||
namespace xamarinFormApp.ViewModels
|
||||
{
|
||||
public class BaseViewModel : INotifyPropertyChanged
|
||||
{
|
||||
public IDataStore<Item> DataStore => DependencyService.Get<IDataStore<Item>>();
|
||||
|
||||
bool isBusy = false;
|
||||
public bool IsBusy
|
||||
{
|
||||
get { return isBusy; }
|
||||
set { SetProperty(ref isBusy, value); }
|
||||
}
|
||||
|
||||
string title = string.Empty;
|
||||
public string Title
|
||||
{
|
||||
get { return title; }
|
||||
set { SetProperty(ref title, value); }
|
||||
}
|
||||
|
||||
protected bool SetProperty<T>(ref T backingStore, T value,
|
||||
[CallerMemberName] string propertyName = "",
|
||||
Action onChanged = null)
|
||||
{
|
||||
if (EqualityComparer<T>.Default.Equals(backingStore, value))
|
||||
return false;
|
||||
|
||||
backingStore = value;
|
||||
onChanged?.Invoke();
|
||||
OnPropertyChanged(propertyName);
|
||||
return true;
|
||||
}
|
||||
|
||||
#region INotifyPropertyChanged
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
|
||||
{
|
||||
var changed = PropertyChanged;
|
||||
if (changed == null)
|
||||
return;
|
||||
|
||||
changed.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Threading.Tasks;
|
||||
using Xamarin.Forms;
|
||||
using xamarinFormApp.Models;
|
||||
|
||||
namespace xamarinFormApp.ViewModels
|
||||
{
|
||||
[QueryProperty(nameof(ItemId), nameof(ItemId))]
|
||||
public class ItemDetailViewModel : BaseViewModel
|
||||
{
|
||||
private string itemId;
|
||||
private string text;
|
||||
private string description;
|
||||
public string Id { get; set; }
|
||||
|
||||
public string Text
|
||||
{
|
||||
get => text;
|
||||
set => SetProperty(ref text, value);
|
||||
}
|
||||
|
||||
public string Description
|
||||
{
|
||||
get => description;
|
||||
set => SetProperty(ref description, value);
|
||||
}
|
||||
|
||||
public string ItemId
|
||||
{
|
||||
get
|
||||
{
|
||||
return itemId;
|
||||
}
|
||||
set
|
||||
{
|
||||
itemId = value;
|
||||
LoadItemId(value);
|
||||
}
|
||||
}
|
||||
|
||||
public async void LoadItemId(string itemId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var item = await DataStore.GetItemAsync(itemId);
|
||||
Id = item.Id;
|
||||
Text = item.Text;
|
||||
Description = item.Description;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
Debug.WriteLine("Failed to Load Item");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,86 @@
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Diagnostics;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Xamarin.Forms;
|
||||
|
||||
using xamarinFormApp.Models;
|
||||
using xamarinFormApp.Views;
|
||||
|
||||
namespace xamarinFormApp.ViewModels
|
||||
{
|
||||
public class ItemsViewModel : BaseViewModel
|
||||
{
|
||||
private Item _selectedItem;
|
||||
|
||||
public ObservableCollection<Item> Items { get; }
|
||||
public Command LoadItemsCommand { get; }
|
||||
public Command AddItemCommand { get; }
|
||||
public Command<Item> ItemTapped { get; }
|
||||
|
||||
public ItemsViewModel()
|
||||
{
|
||||
Title = "Browse";
|
||||
Items = new ObservableCollection<Item>();
|
||||
LoadItemsCommand = new Command(async () => await ExecuteLoadItemsCommand());
|
||||
|
||||
ItemTapped = new Command<Item>(OnItemSelected);
|
||||
|
||||
AddItemCommand = new Command(OnAddItem);
|
||||
}
|
||||
|
||||
async Task ExecuteLoadItemsCommand()
|
||||
{
|
||||
IsBusy = true;
|
||||
|
||||
try
|
||||
{
|
||||
Items.Clear();
|
||||
var items = await DataStore.GetItemsAsync(true);
|
||||
foreach (var item in items)
|
||||
{
|
||||
Items.Add(item);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine(ex);
|
||||
}
|
||||
finally
|
||||
{
|
||||
IsBusy = false;
|
||||
}
|
||||
}
|
||||
|
||||
public void OnAppearing()
|
||||
{
|
||||
IsBusy = true;
|
||||
SelectedItem = null;
|
||||
}
|
||||
|
||||
public Item SelectedItem
|
||||
{
|
||||
get => _selectedItem;
|
||||
set
|
||||
{
|
||||
SetProperty(ref _selectedItem, value);
|
||||
OnItemSelected(value);
|
||||
}
|
||||
}
|
||||
|
||||
private async void OnAddItem(object obj)
|
||||
{
|
||||
await Shell.Current.GoToAsync(nameof(NewItemPage));
|
||||
}
|
||||
|
||||
async void OnItemSelected(Item item)
|
||||
{
|
||||
if (item == null)
|
||||
return;
|
||||
|
||||
// This will push the ItemDetailPage onto the navigation stack
|
||||
await Shell.Current.GoToAsync($"{nameof(ItemDetailPage)}?{nameof(ItemDetailViewModel.ItemId)}={item.Id}");
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Xamarin.Forms;
|
||||
using xamarinFormApp.Views;
|
||||
|
||||
namespace xamarinFormApp.ViewModels
|
||||
{
|
||||
public class LoginViewModel : BaseViewModel
|
||||
{
|
||||
public Command LoginCommand { get; }
|
||||
|
||||
public LoginViewModel()
|
||||
{
|
||||
LoginCommand = new Command(OnLoginClicked);
|
||||
}
|
||||
|
||||
private async void OnLoginClicked(object obj)
|
||||
{
|
||||
// Prefixing with `//` switches to a different navigation stack instead of pushing to the active one
|
||||
await Shell.Current.GoToAsync($"//{nameof(AboutPage)}");
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Windows.Input;
|
||||
using Xamarin.Forms;
|
||||
using xamarinFormApp.Models;
|
||||
|
||||
namespace xamarinFormApp.ViewModels
|
||||
{
|
||||
public class NewItemViewModel : BaseViewModel
|
||||
{
|
||||
private string text;
|
||||
private string description;
|
||||
|
||||
public NewItemViewModel()
|
||||
{
|
||||
SaveCommand = new Command(OnSave, ValidateSave);
|
||||
CancelCommand = new Command(OnCancel);
|
||||
this.PropertyChanged +=
|
||||
(_, __) => SaveCommand.ChangeCanExecute();
|
||||
}
|
||||
|
||||
private bool ValidateSave()
|
||||
{
|
||||
return !String.IsNullOrWhiteSpace(text)
|
||||
&& !String.IsNullOrWhiteSpace(description);
|
||||
}
|
||||
|
||||
public string Text
|
||||
{
|
||||
get => text;
|
||||
set => SetProperty(ref text, value);
|
||||
}
|
||||
|
||||
public string Description
|
||||
{
|
||||
get => description;
|
||||
set => SetProperty(ref description, value);
|
||||
}
|
||||
|
||||
public Command SaveCommand { get; }
|
||||
public Command CancelCommand { get; }
|
||||
|
||||
private async void OnCancel()
|
||||
{
|
||||
// This will pop the current page off the navigation stack
|
||||
await Shell.Current.GoToAsync("..");
|
||||
}
|
||||
|
||||
private async void OnSave()
|
||||
{
|
||||
Item newItem = new Item()
|
||||
{
|
||||
Id = Guid.NewGuid().ToString(),
|
||||
Text = Text,
|
||||
Description = Description
|
||||
};
|
||||
|
||||
await DataStore.AddItemAsync(newItem);
|
||||
|
||||
// This will pop the current page off the navigation stack
|
||||
await Shell.Current.GoToAsync("..");
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user