使用 ItemTemplate 时忽略 WPF Webview 的 IsPrivateNetworkClientServerCapabilityEnabled

WPF Webview's IsPrivateNetworkClientServerCapabilityEnabled ignored when using ItemTemplate

这种简单的方法工作正常

如果我使用 WebView,将 IsPrivateNetworkClientServerCapabilityEnabled 放入 true,并加载本地网络网站,它会正确加载。

<WPF:WebView Source="http://localnetworkwebsite"  
            NavigationStarting="Wvc_NavigationStarting" 
            NavigationCompleted="Wvc_NavigationCompleted" 
            IsPrivateNetworkClientServerCapabilityEnabled="True" />

这种方法行不通

在我的应用程序中,我想同时管理多个网页(比如独立浏览器上的选项卡),所以我使用 ItemControl 来实例化我需要的 WebView 的计数(这个数字并不总是相同的) .

<ItemsControl x:Name="ItemsWebview" ItemsSource="{Binding WebviewsDataSource}">

    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Grid></Grid>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

    <ItemsControl.ItemTemplate>
        <DataTemplate>

            <!--Item-->
            <WPF:WebView Visibility="{Binding Visibility}" Source="{Binding Source}"  
                            NavigationStarting="Wvc_NavigationStarting" 
                            NavigationCompleted="Wvc_NavigationCompleted" 
                            IsPrivateNetworkClientServerCapabilityEnabled="True" />
            <!--End Item-->

        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

然后,我更改了 WebView 的 Source,并使用 ViewModel hide/make 使它们可见:

public class WebviewViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public Guid LinkedId { get; set; }

    private System.Windows.Visibility _visibility;
    public Visibility Visibility
    {
        get
        {
            return _visibility;
        }

        set
        {
            _visibility = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Visibility)));
        }
    }

    private Uri _source;
    public Uri Source
    {
        get
        {
            return _source;
        }

        set
        {
            _source = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Source)));
        }
    }
}

WebviewsDataSourceObservableCollection<WebviewViewModel>

整个事情似乎可行,但仅适用于外部网站,不适用于本地网站。当使用这种方式创建 WebView 时,我总是得到本地网站的 404 NotFound,就好像 IsPrivateNetworkClientServerCapabilityEnabled 被忽略了一样。但是本地站点使用简单的方法。

创建后我检查了WebViews的状态,IsPrivateNetworkClientServerCapabilityEnabled值仍然是true

所以...

由于此控件是 relatively new,这对我来说似乎是一个错误... 但是我做错了什么吗?

编辑

最小.sln的问题可以下载here.

这个简单的WPF应用程序在ItemTemplate中将无法打开本地网络网站(而外部站点可以),直接使用控件可以成功打开本地网络网站。

只需更改 MainWindowViewModel.cs 中的 WebviewsDataSource 值即可测试本地网络 url。

Microsoft 团队确认这是一个错误:https://github.com/Microsoft/WindowsCommunityToolkit/issues/2356

编辑: 该错误已在 v4.0.0 中修复并合并。如果你遇到这个问题,你只需要将 Microsoft.Toolkit.Win32.UI.Controls nuget 包更新到 4.0.1+