Xamarin Forms登录触发System.MissingMethodException问题求助
运行Xamarin Forms项目时,点击主页面登录按钮触发以下异常:
System.MissingMethodException: 'Method not found: void AndroidHUD.AndHUD.Show(Android.Content.Context,string,int,AndroidHUD.MaskType,System.Nullable`1<System.TimeSpan>,System.Action,bool,System.Action)'
已尝试清理重建解决方案、删除obj和bin文件夹、更新NuGet包,但均无效。登录按钮点击代码如下:
public async void LoginButtonClicked(Object sender, EventArgs e) { UserDialogs.Instance.ShowLoading("Signing in..."); var name = usernameEntry.Text; var password = passwordEntry.Text; var domain = domainnameEntry.Text; if (Utility.IsInternet()) { if (!string.IsNullOrWhiteSpace(name) && !string.IsNullOrWhiteSpace(password) && !string.IsNullOrWhiteSpace(domain)) { Utility.isTokenRefresh = false; await Utility.GetAccessToken(); if (Utility.isTokenRefresh) { GetSiteId(name, password, domain); } else { UserDialogs.Instance.HideLoading(); ShowAlert("Failed to fetch the access token, please try again later."); } } else { UserDialogs.Instance.HideLoading(); if (!Utility.IsWindowsDevice()) { await DisplayAlert("Alert", "Please enter all details.", "Ok"); } else { ShowAlert("Please enter all details."); } } } else { UserDialogs.Instance.HideLoading(); if (!Utility.IsWindowsDevice()) { await DisplayAlert("Alert", "No internet connection.", "Ok"); } else { ShowAlert("No internet connection."); } } }
后续尝试为Android项目安装AndHUD 1.4.3和Acr.UserDialogs 7.2.0.561,但为可移植类库(PCL)项目安装Acr.UserDialogs时出现以下错误:
Severity Code Description Project File Line Suppression State
Error Could not install package 'Acr.UserDialogs 7.2.0.561'. You are trying to install this package into a project that targets '.NETPortable,Version=v4.5,Profile=Profile111', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.
项目为可移植类型项目,请求解决方案。
1. 版本兼容问题说明
Acr.UserDialogs 7.x及以上版本不再支持旧的PCL Profile 111,仅支持.NET Standard。你的PCL项目目标框架是.NETPortable,Version=v4.5,Profile=Profile111,所以无法安装7.x版本,必须降级到兼容PCL的版本。
2. 具体修复步骤
- 卸载现有冲突包:先卸载Android项目中的AndHUD、Acr.UserDialogs,以及PCL项目中已安装的Acr.UserDialogs(如果有的话)。
- 彻底清理项目:手动删除所有项目的
obj和bin文件夹,再执行解决方案的清理操作。 - 安装兼容版本的Acr.UserDialogs:
- 给PCL项目安装Acr.UserDialogs 6.5.0(此版本完全支持Profile 111)。
- 给Android项目安装相同版本的Acr.UserDialogs 6.5.0,安装时会自动匹配并安装兼容的AndHUD依赖,无需手动安装AndHUD。
- 重新生成解决方案:完成安装后,重新生成整个解决方案,测试登录按钮功能。
3. 可选优化方案
如果长期维护项目,建议将PCL项目迁移到.NET Standard(推荐2.0及以上),这样可以使用最新版本的Acr.UserDialogs及其他现代NuGet包,避免版本兼容问题。迁移步骤:
- 在Visual Studio中右键PCL项目,选择“属性”,将目标框架切换为.NET Standard。
- 重新安装最新版本的Acr.UserDialogs(当前稳定版)到所有项目。
内容的提问来源于stack exchange,提问作者Matthew Pans

