1、因为Prism的依赖注入实现机制,无法像WPF一样把所有的ViewModel单独写进一个专门用于放ViewModel的类库,每一个业务类库下应包含本业务模块的所有View、ViewModel。
2、当ListView成为FlexLayout的子级时将导致ListView不能上下滚动,需要把父级控件换掉,比如Grid。
3、当ContentView被嵌套进ContentPage中时,在ContentPage中需要设置父级Grid行列宽高为*
4、当View文件和ViewModel文件命名不规则时将导致页面注册失败,必须严格遵守View文件全部放在Views文件下,ViewModel文件全部放在ViewModels文件下的规则,文件命名规则必须遵守XView,XViewModel 这样的规则
5、Prism从Nuget下载的包和github最新代码区别比较大,用起来区别也比较大,如果出现问题比如说方法找不到之类的,建议直接从github下载源码然后引入项目
6、MAUI不再像WPF一样几乎所有控件都有点击事件,很多需要事件的控件写法有变化
<Grid.GestureRecognizers>
<TapGestureRecognizer Command="{Binding NavigateCommand}" />
</Grid.GestureRecognizers>
7、MAUI中Border控件不再像WPF一样可以直接设置圆角,需要额外的属性来设置,并且通常情况下圆角属性只有一个值,不像WPF的Thickness可以设置四周的圆角,要设置四周的圆角需要这样写
<Border.StrokeShape>
<RoundRectangle CornerRadius="40,40,0,0" />
</Border.StrokeShape>
8、如果VM中频繁的更改UI属性但是UI没有发生变化,需要使用
Device.InvokeOnMainThreadAsync(() =>
{
});
来进行强制刷新
9、在安卓设备中运行如果根据微软官网的demo发现无法发起HTTP请求,那是因为没有注册,需要根据微软在git上的demo来改项目,具体步骤如下
在MauiProgram中注册
services.AddSingleton<IHttpsClientHandlerService, HttpsClientHandlerService>();
services.AddSingleton<IVerificationApi, VerificationApi>();
HttpsClientHandlerService类中写一个方法
public HttpMessageHandler GetPlatformMessageHandler()
{
#if ANDROID
#if NET6_0
var handler = new CustomAndroidMessageHandler();
#elif NET7_0_OR_GREATER
var handler = new Xamarin.Android.Net.AndroidMessageHandler();
#endif
handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) =>
{
if (cert != null && cert.Issuer.Equals("CN=localhost"))
return true;
return errors == System.Net.Security.SslPolicyErrors.None;
};
return handler;
#elif IOS
var handler = new NSUrlSessionHandler
{
TrustOverrideForUrl = IsHttpsLocalhost
};
return handler;
#elif WINDOWS || MACCATALYST
return null;
#else
throw new PlatformNotSupportedException("Only Android, iOS, MacCatalyst, and Windows supported.");
#endif
}
在发起请求的类中构造函数构造好需要的参数
HttpClient _client;
JsonSerializerOptions _serializerOptions;
IHttpsClientHandlerService _httpsClientHandlerService;
public ApiBase(IHttpsClientHandlerService service)
{
#if DEBUG
_httpsClientHandlerService = service;
HttpMessageHandler handler = _httpsClientHandlerService.GetPlatformMessageHandler();
if (handler != null)
_client = new HttpClient(handler);
else
_client = new HttpClient();
#else
_client = new HttpClient();
#endif
_serializerOptions = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
WriteIndented = true
};
}
这样就可以正常发起请求了。
10、如果在调试时进行本地调试,需要开启HTTP明文流量,操作步骤如下:
在Platforms\Android\Resources\xml路径下创建network_security_config.xml文件
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">10.0.2.2</domain>
</domain-config>
</network-security-config>
MainActivity.cs文件中添加如下代码:
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
#if DEBUG
System.Net.ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
#endif
}
AndroidManifest.xml文件中application标签中增加一个属性android:networkSecurityConfig="@xml/network_security_config"
这样就可以开启明文流量了
11、我们的后台是使用微软的微服务架构dapr做的,开发过程中发现HttpClient调不通,不知道原因是否出在后台,调试打断点到postasync方法的时候卡住就没有反应了,编译到windows平台和安卓都是一个效果,原因不明,最后掏出HttpWebRequest解决了。
12、TabbedPage下尽量不要使用NavigationPage,否则会出现切换频繁后白屏的问题,尽量使用ContentPage