MVVM + IService 架构下程序结构与功能
Services 文件夹内创建接口 IStorageKeyValue,定义键值存取相关操作。
public interface IStorageKeyValue
{
string Get(string key, string defaultValue); // 读数据
void Set(string key, string value); // 写数据
}
Services 文件夹内创建类 StorageKeyValue,实现键值存取相关操作。
public class StorageKeyValue : IStorageKeyValue
{
public string Get(string key, string defaultValue)
{
return Preferences.Get(key, defaultValue);
}
public void Set(string key, string value)
{
Preferecnes.Set(key, value);
}
}
Preferences 由 Maui 提供,实现键值存储。
在 ServiceLocator 中实现依赖注入的注册
serviceCollection.AddSingleton();
ViewModel 中的 IStorageKeyValue 调用
private IStorageKeyValue _StroageKeyValue;
public ViewModelMainPage(IStorageKeyValue storageKeyValue) // 用构造函数实现依赖注入的解析
{
_StorageKeyValue = storagekeyValue;
}
Nuget 安装包 sqlite-net-pcl, Models 文件夹下生成类 Poetry
public class Poetry
{
[PrimaryKey, AutoIncrement] // 选择主键 Id,并且自增
public int Id { get; set;}
public string Title { get; set;}
public string Content { get; set;}
}
Services 文件夹内创建接口 IStoragePoetry,定义数据库存取相关操作。
理想化的接口实现
public interface IStoragePoetry
{
void Initialize(); // 建库
void Add(Poetry poetry); // 存
IEnumerable List(); // 全取
}
考虑异步机制以后,现实化的接口实现改为
public interface IStoragePoetry
{
Task InitializeAsync(); // 建库
Task AddAsync(Poetry poetry); // 存
Task> ListAsync(); // 全取
}
接口设计必须考虑实现
Services 文件夹内创建类 StoragePoetry,实现数据库存取相关操作。
public class StoragePoetry : IStoragePoetry
{
// 数据库路径
public const string DbFileName = "poetrydb.sqlite3";
public static readonly string DbPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), DbFileName); // .Net 提供跨平台位置获取机制,可以安全读写数据
// 定义并打开数据库连接
private SQLiteAsyncCoonection _Connection;
public SQLiteAsyncCoonection Connection =>
_Connection ??= new SQLiteAsyncCoonection(PoetryDbPath);
public async Task InitializeAsync()
{
await Connection.CreateTableAsync(); // 创建 Poetry 表
}
public async Task AddAsync(Poetry poetry)
{
await Connection.InsertAsync(poetry);
}
public async Task> ListAsync()
{
return await Connection.Table().ToListAsync();
}
}
在 ServiceLocator 中实现依赖注入的注册
serviceCollection.AddSingleton();
ViewModel 中的 IStoragePoetry 调用
public class ViewModelMainPage : ObservableObject
{
private IStoragePoetry _StoragePoetry;
public ViewModelMainPage(IStoragePoetry storagePoetry) // 用构造函数实现依赖注入的解析
{
_StoragePoetry = storagePoetry ;
}
public ObservableCollection Poetries { get; } = new (); // 属性定义语法糖
private RelayCommand _CommandInitial;
public RelayCommand CommandInitial => _CommandInitial ??= new RelayCommand(async () =>
{
await _StoragePoetry.InitializeAsync();
});
private RelayCommand _CommandAdd;
public RelayCommand CommandAdd => _CommandAdd ??= new RelayCommand(async () =>
{
await _StoragePoetry.AddAsync(new Poetry
{
Title = "title",
Content = "content"
});
});
private RelayCommand _CommandList;
public RelayCommand CommandList => _CommandList ??= new RelayCommand(async () =>
{
var list = await _StoragePoetry.ListAsync();
foreach (var p in list)
{
Poetries.Add(p);
}
});
}
参考 maui 官方文档
“*”父控件有多少占多少;“auto”自己有多少占多少
常规操作,略
显示类名
显示数据
在类 Poetry 中添加
[Ignore] // 不进入数据库
public string TitleContent => Title + Content;
后面讲
添加 Wrapper 类
public class WrapperPoetry
{
// 包装 Poetry,实现额外功能
}
Services 文件夹内创建接口 IServiceToken,定义 Web 服务相关操作。
public interface IServiceToken
{
Task GetTokenAsync();
}
Services 文件夹内创建类 ServiceToken,实现 Web 服务相关操作。
public class ServiceToken : IServiceToken
{
public async Task GetTokenAsync()
{
var httpClient = new HttpClient();
var response = await httpClient.GetAsync("https://v2.jinrishici.com/token");
var json = await response.Content.ReadAsStringAsync();
return json;
}
}
在 ServiceLocator 中实现依赖注入的注册
serviceCollection.AddSingleton();
ViewModel 中的 IServiceToken 调用
public class ViewModelMainPage : ObservableObject
{
private IServiceToken _ServiceToken;
public ViewModelMainPage(IServiceToken serviceToken) // 用构造函数实现依赖注入的解析
{
_ServiceToken = serviceToken;
}
private string _Json;
public string Json
{
get => _Json;
set => SetProperty(ref _Json, value);
}
private RelayCommand _CommandLoadJson;
public RelayCommand CommandLoadJson => _CommandLoadJson ??= new RelayCommand(async ()=>
{
Json = await _ServiceToken.GetTokenAsync();
});
}