英文:
MAUI reference to webview control from Android MainActivity.cs file
问题
以下是您要翻译的内容:
我正在开发一个.NET MAUI应用,针对Android,我在MainPage.xaml文件中有一个WebView控件。
```xml
<WebView x:Name="webView" Source="somewebsite"/>
如果可能的话,我想要处理“back”键以导航到上一页。我想知道如何从Android文件夹中的MainActivity.cs文件中引用该WebView控件。
public class MainActivity : MauiAppCompatActivity
{
public override bool OnKeyDown(Keycode keyCode, KeyEvent e)
{
if (keyCode == Keycode.Back)
{
var webView = //MainPage.xaml中的WebView
if (webView.CanGoBack)
{
webView.GoBack();
return true;
}
}
return base.OnKeyDown(keyCode, e);
}
}
我走错了吗?
编辑:
在我的MainPage.xaml.cs文件中使用OnBackButtonPressed方法不会导航到上一页。它只是关闭应用程序。
protected override bool OnBackButtonPressed()
{
if (webView.CanGoBack == true)
{
webView.GoBack();
return true;
}
return false;
}
希望这些翻译对您有所帮助。
<details>
<summary>英文:</summary>
I have a .NET MAUI app that I'm developing for Android and I have a WebView control in the MainPage.xaml file.
<WebView x:Name="webView" Source="somewebsite"/>
I want to handle the "back" key to navigate to the previous page if possible. I'm wondering how I can reference that WebView control from the MainActivity.cs file in the Android folder.
public class MainActivity : MauiAppCompatActivity
{
public override bool OnKeyDown(Keycode keyCode, KeyEvent e)
{
if (keyCode == Keycode.Back)
{
var webView = //WebView in MainPage.xaml
if (webView.CanGoBack)
{
webView.GoBack();
return true;
}
}
return base.OnKeyDown(keyCode, e);
}
}
Am I on the wrong track?
Edit:
Using the OnBackButtonPressed method in my MainPage.xaml.cs file doesn't navigate to the previous page. It just closes the app.
protected override bool OnBackButtonPressed()
{
if (webView.CanGoBack == true)
{
webView.GoBack();
return true;
}
return false;
}
</details>
# 答案1
**得分**: 1
你可以尝试使用以下代码来实现这个功能。
在XAML中:
```xml
<WebView x:Name="webview" Source=""/>
在页面的 OnBackButtonPressed
方法中:
protected override bool OnBackButtonPressed()
{
#if ANDROID
var web = webview.Handler.PlatformView as Android.Webkit.WebView;
if (web.CanGoBack())
{
web.GoBack();
return true;
}
#endif
return base.OnBackButtonPressed();
}
或者你也可以使用静态变量来实现这个功能。但这会导致内存泄漏,因为主页(MainPage)直到应用被终止才会被回收。
在XAML中:
<WebView x:Name="webview" Source=""/>
在页面的代码后台(code behind)中:
public partial class MainPage : ContentPage
{
#if ANDROID
public static WebView webView;
#endif
public MainPage()
{
InitializeComponent();
#if ANDROID
webView = webview;
#endif
}
}
在MainActivity中:
public override bool OnKeyDown([GeneratedEnum] Keycode keyCode, KeyEvent e)
{
if(keyCode == Keycode.Back)
{
var web = MainPage.webView.Handler.PlatformView as Android.Webkit.WebView;
if (web.CanGoBack())
{
web.GoBack();
return true;
}
}
return base.OnKeyDown(keyCode, e);
}
英文:
You can try to use the following code to do that.
In the xaml:
<WebView x:Name="webview" Source=""/>
And in the page's OnBackButtonPressed
method:
protected override bool OnBackButtonPressed()
{
#if ANDROID
var web = webview.Handler.PlatformView as Android.Webkit.WebView;
if (web.CanGoBack())
{
web.GoBack();
return true;
}
#endif
return base.OnBackButtonPressed();
}
Or you can also use the static variable to do that. But this will cause memory leak, becasue the MainPage will never be recycled untill the app be killed.
In the xaml:
<WebView x:Name="webview" Source=""/>
And in the page's code behind:
public partial class MainPage : ContentPage
{
#if ANDROID
public static WebView webView;
#endif
public MainPage()
{
InitializeComponent();
#if ANDROID
webView = webview;
#endif
}
}
In the MainActivity:
public override bool OnKeyDown([GeneratedEnum] Keycode keyCode, KeyEvent e)
{
if(keyCode == Keycode.Back)
{
var web = MainPage.webView.Handler.PlatformView as Android.Webkit.WebView;
if (web.CanGoBack())
{
web.GoBack();
return true;
}
}
return base.OnKeyDown(keyCode, e);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论