我正在開發一個需要從網路服務器下載和顯示 XAML 代碼的應用程式。到目前為止,我的代碼按預期作業,但現在我需要顯示一個需要訪問 GPS 資料的 WebView(可以與 android 上的 chrome 瀏覽器一起使用)。我測驗了實作和一切。在應用程式代碼中編譯 XAML 代碼時,一切都像魅力一樣。即使 XAML-Hot-Reload 也可以作業,但是一旦我嘗試使用以下代碼加載我的內容頁面:
public static class GUIFramework
{
public static readonly string PRE_FRAMEWORK_XAML = "<ContentPage xmlns=\"http://xamarin.com/schemas/2014/forms\" xmlns:x=\"http://schemas.microsoft.com/winfx/2009/xaml\" xmlns:local=\"clr-namespace:Buerger_App.Renderer;assembly=Buerger_App\" x:Class=\"Buerger_App.Views.Framework\"><ContentPage.Content>";
public static readonly string POST_FRAMEWORK_XAML = "</ContentPage.Content></ContentPage>";
/// <summary>
/// This Dictionary is used for caching all of the generated CententPages in the memory.
/// This is needed due to the Page-XAML-Code needs to be compiled JIT and this can cause noticable delay.
/// The Caching automatically jumps in after a page was loaded once.
/// </summary>
private static Dictionary<string, ContentPage> CachedContentPages = new Dictionary<string, ContentPage>();
/// <summary>
/// Loads the given XAML string as a new ContentPage and binds the Framework Binding-Context
/// </summary>
/// <param name="XAML_Content">A string representing the XAML formatted code</param>
/// <returns>The newly created ContentPage</returns>
public static ContentPage LoadFramework(string XAML_Content, string Title = "")
{
string pre_XAML = PRE_FRAMEWORK_XAML;
if (Title != "")
{
pre_XAML = pre_XAML.Replace("<ContentPage ", "<ContentPage Title=\"" Title "\" ");
}
string FinalXAML = pre_XAML XAML_Content POST_FRAMEWORK_XAML;
ContentPage ContentPage = null;
// Use caching and read the XAML live from the phone and/or cache
string hash = SHA256Hash.GetHash(FinalXAML);
if (CachedContentPages.ContainsKey(hash))
ContentPage = CachedContentPages[hash];
else
{
ContentPage = new ContentPage().LoadFromXaml(FinalXAML);
CachedContentPages.Add(hash, ContentPage);
}
ContentPage.BindingContext = new FrameworkViewModel();
return ContentPage;
}
// More code ...
}
我嘗試加載以下 XAML 代碼(使用LoadFramework-function 構建完整的 XAML 代碼):
<StackLayout>
<Image Source="logo_wide.jpg" Margin="0,0,0,25"/>
<local:GeoWebView x:Name="WebView" Margin="10,0,10,0" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Source="https://google.de" />
</StackLayout>
LoadFromXAML 擴展運行沒有任何錯誤(沒有拋出例外)。它只跳過創建我的自定義 GeoWebView(我可以在除錯器中新創建的 ContentPage 的子項中驗證(ContentPage 只有 1 個子項,這是在我的自定義 WebView 之前創建的影像))。
uj5u.com熱心網友回復:
好的,所以我擺弄了一下,找到了我的解決方案。這里也描述了我遇到的問題。提到將程式集密鑰添加到應動態加載的 XAML 代碼中的命名空間的答案通常是正確的,但我在最初的問題中做錯了(我添加了程式集密鑰但程式集名稱不正確)。
我的測驗問題是,我輸入Buerger_App的程式集是我的默認命名空間,而不是我的程式集名稱。我的程式集名稱是Buerger App.
更正 XAML 代碼后,它開始按預期作業。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/424229.html
標籤:C# xml xamarin.forms
