gpt4 book ai didi

android - 防止 Android WebView 在 Xamarin 环境中重新加载页面旋转

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:46:32 25 4
gpt4 key购买 nike

我意识到有很多与此相关的问题,但我已经尝试了所有本地开发解决方案,但无法让它们在 Xamarin 下工作。

我有一个 webview,它加载了一个 URL,其中包含用于绘图的图形 UI。当设备旋转时,webview 重新加载页面,用户的工作丢失。

下面的博文给出了最好的解决方案,并解释了广泛记录的解决方案是不够的,原因如下:

http://www.devahead.com/blog/2012/01/preserving-the-state-of-an-android-webview-on-screen-orientation-change/

The main problem with this implementation is that, whenever you rotate the screen, the >WebView is created again because the activity is destroyed and its saveState method doesn’t >save the full state, but only a part of it like the URL of the page that was loaded and the >browsing history. So it happens that for example the zoom and the scroll position are not >preserved after the screen orientation change and sometimes the page is reloaded from the web.

所以我已经实现了他们的解决方案,将其从 Java 移植到 Xamarin C#。它看起来很有希望,但每当我的 Activity 重新加载并调用 InitUi 时,web_view 类变量始终为空,而该示例依赖于此类变量在设备旋转之间保持其值。

有人知道为什么 web_view 失去了它的值(value)吗?

这是我的代码,它与文章完全一样,但移植到了 Xamarin C#。

public class EbookViewerActivity : ActionBarActivity
{
protected WebView web_view;
protected FrameLayout webViewPlaceholder;

Button loadButton;
Button downloadButton;
EditText testUrlText;
private string viewerPath;

protected override void OnCreate(Bundle savedInstanceState)
{
#region Set up activity and action bar

//Create the activity screen and initialise the action bar
base.OnCreate(savedInstanceState);
RequestWindowFeature(WindowFeatures.NoTitle);
SetContentView(Resource.Layout.Viewer);
InitializeActionBar();

//Set action bar button delegates
ActionBar
.AddLeftAction(new DelegateAction(Finish, Resource.Drawable.CloseIcon))
.SetTitle("Ebook Annotator");

//Set action bar logo
ActionBar.SetHomeLogo(Resource.Drawable.AcmeTrainingLogo);
#endregion

InitUi();
}

private void InitUi()
{
Logger logger = Logger.Instance;
// Retrieve UI elements
webViewPlaceholder = FindViewById<FrameLayout>(Resource.Id.webViewPlaceholder);
// Initialize the WebView if necessary
if (web_view == null)
{
web_view = new WebView(this);
web_view.Id = Resource.Id.ebookDynamicWebView;

//web_view = FindViewById<WebView>(Resource.Id.ebookWebview);
web_view.Settings.JavaScriptEnabled = true;
web_view.AddJavascriptInterface(new AnnotationApiProxy(this), "AnnotationApi");
string ebookId = Intent.GetStringExtra("ebookId");
string userEmail = Intent.GetStringExtra("userEmail");
Ebook ebook = EbookManager.GetEbook(int.Parse(ebookId));

GlobalVariableHolder.Instance.EbookToOpen = int.Parse(ebookId);
viewerPath = "file:///android_asset/Annotator/annotator.html";

web_view.Settings.AllowFileAccess = true;
logger.WriteToLog("Loading test harness with ebook id: " + ebookId, LogEntryLevel.Message);
web_view.SetWebChromeClient(new EbookWebViewClient() { });
// web_view.SetWebViewClient(new WebViewClient());
AnnotationDownloader annotationDownloader = new AnnotationDownloader();
// annotationDownloader.XmlDownloaded += (sender, args) => RunOnUiThread(() =>
// {
web_view.LoadUrl(viewerPath);
// });
annotationDownloader.GetLatestEWorkBookXml(ebook.ID, userEmail);
}

// Attach the WebView to its placeholder
webViewPlaceholder.AddView(web_view);
}

protected override void OnSaveInstanceState(Bundle outState)
{
base.OnSaveInstanceState(outState);
// Save the state of the WebView
web_view.SaveState(outState);
}

protected override void OnRestoreInstanceState(Bundle savedInstanceState)
{
base.OnRestoreInstanceState(savedInstanceState);
// Restore the state of the WebView
web_view.RestoreState(savedInstanceState);
}

public override void OnConfigurationChanged(Configuration newConfig)
{
if (web_view != null)
{
// Remove the WebView from the old placeholder
webViewPlaceholder.RemoveView(web_view);
}
base.OnConfigurationChanged(newConfig);
// Load the layout resource for the new configuration
SetContentView(Resource.Layout.Viewer);
// Reinitialize the UI
InitUi();
}
}

最佳答案

不要更改 AndroidManifest.xml 并使用自定义属性。

[Activity(ConfigurationChanges=ConfigChanges.Orientation | ConfigChanges.ScreenSize)] 
public partial class MyActivity : Activity { ...

这个解决方案对我很有效。

关于android - 防止 Android WebView 在 Xamarin 环境中重新加载页面旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19513993/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com