.NET MAUI实战 Dispatcher
Net 实战 MAUI Dispatcher
2023-06-13 09:15:42 时间
详细内容
这一期分享的内容非常简单,在之前使用过WPF的开发者对MVVM开发模式下ViewModel中后台线程转UI线程并不陌生使用Appplication.Current.Dispatcher。那么在.NET MAUI中也有同样的机制,存在于.NET MAUI Shell对象中。
那么什么是Shell?
官网描述如下,.NET 多平台应用 UI (.NET MAUI) Shell 通过提供大多数应用所需的基本功能(包括:
- 用于描述应用的视觉层次结构的单个位置。
- 常见的导航用户体验。
- 基于 URI 的导航方案,允许导航到应用中的任何页面。
- 集成的搜索处理程序。
其他内容就不搬运了,大伙可以参考下面链接内容。
ref:https://docs.microsoft.com/zh-cn/dotnet/maui/fundamentals/shell/
接下来我们直接来看看实际运用是如何的,代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MauiApp1.ViewModels
{
public class MainViewModel
{
public void Do()
{
//同步
Shell.Current.Dispatcher.Dispatch(() =>
{
//code...
});
//异步非阻塞
Shell.Current.Dispatcher.DispatchAsync(() =>
{
//code...
});
//延迟1s
Shell.Current.Dispatcher.DispatchDelayed(new TimeSpan(1000), () =>
{
//code...
});
}
}
}
如果在MVVM开发模式下,还想在ViewModel中弹出消息框,那么同样也可以在Shell中访问。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MauiApp1.ViewModels
{
public class MainViewModel
{
public void Do()
{
Shell.Current.DisplayAlert("message title","message content","cancel");
}
}
}
相关文章
- asp.net mvc 下拉框级联
- .net 温故知新:【9】.NET日志记录 ILogger使用和原理
- ASP.NET_.NET
- .net 温故知新:【8】.NET 中的配置从xml转向json
- .NET Core3.1 Dotnetty实战第二章
- .NET Core3.1 Dotnetty实战第三章
- .NET MAUI实战 MessagingCenter
- .NET MAUI实战 FolderPicker
- Net之测速工具
- Oracle 等待事件 SQL*Net more data to client 官方解释,作用,如何使用及优化方法
- .NET访问MySQL:简单实用的方法(.net访问mysql)
- Oracle NET突破技术界限,精彩开启新纪元(oracle net??)
- 基于NET技术的Oracle信息服务(.net用oracle)
- 基于NET的MySQL数据库开发实践(.net中使用mysql)
- Redis集群实现高性能NET应用(redis集群 .net)
- QQ关于.net的精彩对话
- VisualC#.Net网络程序开发-Socket篇
- asp.net下用DataSet生成XML的问题
- ASP.NET重定向的几种方法小结
- asp.net将设有过期策略的项添加到缓存中
- Asp.Net动态页面转静态页面主要代码
- asp.net中水印的具体实现代码
- asp.net动态产生checkbox(数据源为DB或内存集合)
- Asp.net程序优化js、css实现合并与压缩的方法
- ASP.NET实现二维码(QRCode)的创建和读取实例