開發
1、Resource.Id未包含xxx的定義
打開了一個OK的Id,是位於\obj\Debug\90\designtime\Resource.designer.cs ,打開文件搜索xxx,果然沒有。
刪除此文件 重新生成項目,並沒有重新生成Resource.designer.cs
解決方法:
刪除整個obj文件夾,重新生成項目;
或者重啟VS,自動重新生成Resource.designer.cs了,並且含有xxx
2、Button文本默認大寫
參考:xamarin forms中的Button文本默認大寫
將按鈕的屬性textAllCaps都改為false。默認沒有設置 是true。
3、ADB0020: Android ABI 不匹配。
問題1:你正將應用支持的“armeabi,armeabi-v7a,x86”ABI 部署到 ABI“x86_64”的不兼容設備。應創建匹配其中一個應用 ABI 的仿真程序,或將“x86_64”添加到應用生成的 ABI 列表中
問題原因:應該是選擇的模擬器(X86_64)不匹配,需要創建一個“armeabi,armeabi-v7a,x86”是模擬器來調試。
或者看下“使用共享運行時”是否選中
參考:
說說Android項目中的armeabi,armeabi-v7a和x86
Android 關於arm64-v8a、armeabi-v7a、armeabi、x86下的so文件兼容問題
問題2:Please open the Android SDK Manager and install the latest version of 'Android Support Repository' from the 'Extras' section, and then reload your solution.
問題原因:上一步選擇X86處理器后,需要安裝相應的SDK,和工具中的Android Support Repository
4、方法過時
4.1 [Obsolete("This constructor is obsolete as of version 2.5. Please use PageRenderer(Context) instead.")]
在使用PageRenderer時提示這個構造函數已經過時,發生這種情況的原因是從v2.5版本開始的Xamarin.Forms非常簡單,將Xamarin.Forms.Forms的Context屬性標記為過時。 因此,要獲取渲染器的全局上下文,有必要調用該渲染器的基類,這意味着在構造函數中,您需要執行以下操作: 添加以下代碼即可(Please use PageRenderer(Context) instead.)
public WriteEmailPageRenderer(Context context) : base(context) { }
參考:How do I resolve WebViewRenderer is obsolete
4.2 Context is obsolete as of version 2.5. Please use a local context instead
報過時的寫法:Android.Content.Context content = Forms.Context; 或者 Activity activity = (Activity)Forms.Context;
在Dependencies中定義特定平台的功能時,使用Forms.Context會提示已過時,請改為使用本地環境。
解決方案是在不引用Xamarin.Forms.Forms.Context的情況下獲取本地上下文。
注:Android.Content.Context是應用程序環境全局信息上下文,Activity都是繼承自此類。
有兩種方式解決:
方式1、在接口中提供Init(),然后在MainActivity中調用:VersionHelper.Init(this);
public class VersionHelper : IVersionHelper { static Context _context; public static void Init(Context context) { _context = context; } ...
方式2、在MainActivity類中創建一個靜態屬性,並將其設置為MainActivity實例:

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity { internal static MainActivity Instance { get; private set; } protected override void OnCreate(Bundle bundle) { ... global::Xamarin.Forms.Forms.Init(this, bundle); Instance = this; Xamarin.Forms.DependencyService.Register<IVersionHelper, VersionHelper>(); LoadApplication(new App()); } }
然后接口中使用

using Android.Content; namespace DependencyServiceAndLocalContext.Droid { public class VersionHelper : IVersionHelper { public string GetVersionNumber() { var versionNumber = string.Empty; if (MainActivity.Instance != null) { versionNumber = MainActivity.Instance.PackageManager.GetPackageInfo( MainActivity.Instance.PackageName, 0).VersionName; } return versionNumber; } } }
參考:Xamarin.Forms 2.5 and Local Context on Android
調試
1、Xamarin.android不能顯示網絡、遠程圖片
調試時從IDE的輸出可以看出原因:Image load failed: System.ObjectDisposedException: Can not access a closed Stream. 不知這個問題是不是還沒有去修復,還是不准備修復
因為看到Mircsoft官方示例eshop都是用的一個開源包:https://github.com/luberda-molinet/FFImageLoading
注意:各個平台需要調用: CachedImageRenderer.Init(false); 【上述github有使用方法】
否則 會報錯:
"Please call CachedImageRenderer.Init method in a platform specific project to use FFImageLoading!"
2、Can't install because provider name com.xxx.fileprovider (in package yyy) is already used by com.mesince]
這個問題其實本質就是手機里已經安裝有同名的Provider
的APK了.
比如我在甲APP(com.xxx
)中的AndroidManifest.xml
中定義了一個Provider,
安裝完這個甲APP,
我把代碼里的ApplicationId
改成com.yyy
,即換皮成乙APP(com.yyy
) ,
此時如果沒有把Provider
的android:authorities
的屬性也換掉,
安裝乙APP到手機上,provider就會和甲APP的沖突.
android:authorities= ${applicationId}.provider
參考:[Android] 該文件包與具有同一名稱的現有文件包存在沖突
3、Android http 不能聯網:Cleartext HTTP traffic to not permitted
在嘗試 請求外部的WebApi時,報出錯誤。
解決:在AndroidManifest.xml配置文件的<application>標簽中插入:
android:usesCleartextTraffic="true"
4、Android模擬機時間修改
沒做任何修改的前提下,其時間是美國紐約的時區,相對中國是-8小時的。所以程序中DateTime.Now獲取的比中國時間小8小時
可以自己修改為和中國一樣。
本來想設置 圖中1處的時區,但是發現沒有中國的。於是直接改圖中2處的時間。
5、listView.ScrollTo不生效
需要看下滾動的代碼是否有UI線程執行的
txtMessage.Focused += TxtMessage_Focused; private void TxtMessage_Focused(object sender, FocusEventArgs e) { if (listChatMessage.Count > 0) { Task.Delay(100).ContinueWith((t) => { Device.BeginInvokeOnMainThread(() => { listView.ScrollTo(listChatMessage.Last(), ScrollToPosition.End, false); }); }); } }
6、not attached to window manager
not attached to window manager有許多場景發生。最近在做設備鎖時,彈出進度圈和取消時碰到此問題
調試判斷出大概是Activity 已經銷毀了,再調Dismiss時失敗,,【泄露】
最終修改代碼,發現確實是開啟的dialog在Activity銷毀之前,沒有關閉。。然后在Activity銷毀了再去dismiss。
7、Unable to add window -- token android.os.BinderProxy@b1bab0a is not valid; is your activity running?
最近在做設備鎖時,需要彈窗的地方碰到此問題
問題:Activity不是處於運行狀態
調試查看報錯處的Activity是哪個,代碼中寫的是Activity context = (Activity)Forms.Context;
System.Console.WriteLine(TAG + context.LocalClassName);
發現此Activity調用了OnDestroy()馬上就銷毀了,所以他不是在運行中。。
修改Activity的賦值:Activity activity = MainActivity.Instance;
8、net::ERR_CLEARTEXT_NOT_PERMITTED
在安卓9.0中調試WebView加載網頁時錯誤。
解決方法: 在AndroidManifest文件中的節點Application中加入 android:usesCleartextTraffic="true"
9、Xamarin.forms包加入不進
在遷移一個項目后,Xamarin.Android中本來有Xamarin.Forms這個包的,但是同事從遠程拉取代碼后,沒有顯示此包,導致調試不通過。
報錯(17個錯誤):style attribute 'attr/colorPrimaryDark (aka com.mesince:attr/colorPrimaryDar
在Nuget中引入了 但還是不行。后來嘗試 從解決方案中卸載此Xamarin.Android項目,重新添加,居然可以顯示了,好奇怪的問題!