MAUI在Android MainActivity.cs文件中引用Web视图控件。

huangapple go评论821阅读模式
英文:

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&#39;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 &quot;back&quot; key to navigate to the previous page if possible. I&#39;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&#39;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
&lt;WebView x:Name=&quot;webview&quot; Source=&quot;&quot;/&gt;

在页面的 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中:

&lt;WebView x:Name=&quot;webview&quot; Source=&quot;&quot;/&gt;

在页面的代码后台(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:

&lt;WebView x:Name=&quot;webview&quot; Source=&quot;&quot;/&gt;

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:

&lt;WebView x:Name=&quot;webview&quot; Source=&quot;&quot;/&gt;

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);
    }

huangapple
  • 本文由 发表于 2023年6月22日 00:44:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76525490.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定