首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >.NET 5显示WebView2

.NET 5显示WebView2
EN

Stack Overflow用户
提问于 2020-12-08 23:28:11
回答 1查看 667关注 0票数 1

我试图在WebView2 5上的Xamarin应用程序中显示一个.NET。

因此,在启动时,我将MainPage设置为Navigator,如下所示:

Navigator.xaml:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:XamTestNET5.Views"
             x:Class="XamTestNET5.Navigator" Title="Here is a title">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <StackLayout Grid.Row="0" Grid.Column="0" Orientation="Vertical" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
            <Label FontSize="Medium">Hello</Label>
            <!--<local:PlaceHolderView></local:PlaceHolderView>-->
            <local:CustomWebBrowserView x:Name="browser" Source="https://www.google.com"></local:CustomWebBrowserView>
        </StackLayout>
        <StackLayout Grid.Row="1" Grid.Column="0" Orientation="Horizontal" HorizontalOptions="FillAndExpand" BackgroundColor="{StaticResource Primary}">
            <Button FontSize="Large" Text="Tab 1" BorderWidth="0" BorderColor="Transparent"></Button>
            <Button FontSize="Large" Text="Tab 2" BorderWidth="0" BorderColor="Transparent"></Button>
            <Button FontSize="Large" Text="Tab 3" BorderWidth="0" BorderColor="Transparent"></Button>
        </StackLayout>
    </Grid>
</ContentPage>

CustomWebBrowserView.xaml:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="XamTestNET5.Views.CustomWebBrowserView">
  <ContentView.Content>
        <Grid></Grid>
  </ContentView.Content>
</ContentView>

CustomWebBrowserView.xaml.cs:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace XamTestNET5.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class CustomWebBrowserView : ContentView
    {
        public static readonly BindableProperty SourceProperty = BindableProperty.Create(
          propertyName: "Source",
          returnType: typeof(string),
          declaringType: typeof(string),
          defaultValue: "");

        public string Source
        {
            get { return (string)GetValue(SourceProperty); }
            set { SetValue(SourceProperty, value); }
        }
        public CustomWebBrowserView()
        {
            InitializeComponent();
        }
    }
}

WPF项目中的WebBrowserRenderer.cs:

代码语言:javascript
复制
using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.Wpf;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms.Platform.WPF;
using XamTestNET5.Views;
using XamTestNET5WPF.Renderers;

[assembly: ExportRenderer(typeof(CustomWebBrowserView), typeof(WebBrowserRenderer))]
namespace XamTestNET5WPF.Renderers
{
    public class WebBrowserRenderer : ViewRenderer<CustomWebBrowserView, WebView2>
    {
        WebView2 webView;
        CoreWebView2EnvironmentOptions options = new CoreWebView2EnvironmentOptions();
        CoreWebView2Environment env;
        String userDataFolder = Path.GetTempPath();

        public WebBrowserRenderer() : base()
        {
        }

        private void WebView_CoreWebView2Ready(object sender, EventArgs e)
        {
            this.webView.CoreWebView2.Navigate(@"https://www.bing.com");
        }

        protected async override void OnElementChanged(ElementChangedEventArgs<CustomWebBrowserView> e)
        {
            base.OnElementChanged(e);
            if (e.NewElement != null)
            {
                webView = new WebView2();
                env = await CoreWebView2Environment.CreateAsync("", userDataFolder, options);
                webView.CoreWebView2Ready += WebView_CoreWebView2Ready;
                webView.Source = new Uri(@"https://www.bing.com");
                webView.Visibility = System.Windows.Visibility.Visible;
                this.SetNativeControl(webView);
                await webView.EnsureCoreWebView2Async(env);
            }
        }

        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);
            if (e.PropertyName == nameof(CustomWebBrowserView.SourceProperty))
            {
                this.webView.Source = new Uri(Element.Source);
                this.webView.CoreWebView2.Navigate(Element.Source);
            }
        }
    }
}

通过调试,我可以看到我得到的是WebView2,但是它抹掉了内容页,只留下了标题。

如果我注释掉CustomWebBrowserView并放入一个PlaceHolderView存根,它看起来就像这样:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="XamTestNET5.Views.PlaceHolderView">
  <ContentView.Content>
      <StackLayout>
          <Label Text="Placeholder" />
      </StackLayout>
  </ContentView.Content>
</ContentView>

布局至少没有被抹掉,但当然没有WebView2。

我也不能让壳牌正常工作,但这是意料之中的:https://github.com/xamarin/Xamarin.Forms/issues/7377

因此,我认为在此期间的解决办法可能只是在应用程序中创建自己的自定义导航,但我想在WPF中提供WebView2控件。我在WebBrowserRenderer或Xaml中做错什么了吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-09 01:58:13

将CustomWebBrowserView更改为以下内容,基于视图而不是基于ContentView,现在一切都正常工作,而视图没有被抹掉:

CustomWebBrowserView.Xaml:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<View xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="XamTestNET5.Views.CustomWebBrowserView">
</View>

CustomWebBrowserView.cs:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace XamTestNET5.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class CustomWebBrowserView : View
    {
        public static readonly BindableProperty SourceProperty = BindableProperty.Create(
          propertyName: "Source",
          returnType: typeof(string),
          declaringType: typeof(string),
          defaultValue: "");

        public string Source
        {
            get { return (string)GetValue(SourceProperty); }
            set { SetValue(SourceProperty, value); }
        }
        public CustomWebBrowserView()
        {
            InitializeComponent();
        }
    }
}

Navigator.xaml:

代码语言:javascript
复制
    <StackLayout Grid.Row="0" Grid.Column="0" Orientation="Vertical" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
        <Label FontSize="Medium">Hello</Label>
        <!--<local:PlaceHolderView></local:PlaceHolderView>-->
        <local:CustomWebBrowserView HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" x:Name="browser" Source="https://www.sneauxkones.com"></local:CustomWebBrowserView>
    </StackLayout>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65208454

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档