Search code examples
c#wpfmvvmitemtemplate

WPF Webview's IsPrivateNetworkClientServerCapabilityEnabled ignored when using ItemTemplate


This simple way of doing is working fine

If I use a WebView, put IsPrivateNetworkClientServerCapabilityEnabled to true, and load a local network website, it will load properly.

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

This way of doing is NOT working

In my application, I want to manage several webpage simultaneously (like tabs on a standalone browser), so I use an ItemControl to instantiate the WebView's count I need (this number is not always the same).

<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>

And then, I change the Source of ths WebView, and hide/make them visible using the ViewModel:

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

WebviewsDataSource is ObservableCollection<WebviewViewModel>

The whole thing seems to works, but ONLY for the external websites, not for the local one. I always get an 404 NotFound for the local network sites when using this ways for creating WebViews, as if IsPrivateNetworkClientServerCapabilityEnabled was ignored. But the local site works with the simple method.

I've checked the states of the WebViews after the creation, and the IsPrivateNetworkClientServerCapabilityEnabled value is still to true.

So...

Since this control is relatively new, this looks like a bug to me... But am i doing something wrong?

Edit

Minimal .sln of the problem can be downloaded here.

This simple WPF application will fail to open local network website in ItemTemplate (while external site will works), and will succeed to open local network websites when using the control directly.

Just change the WebviewsDataSource value in MainWindowViewModel.cs to test local network urls.


Solution

  • Microsoft team confirmed this as a bug: https://github.com/Microsoft/WindowsCommunityToolkit/issues/2356

    Edit: The bug has been fixed and merged in the v4.0.0. If you experience this problem, you just need to update the Microsoft.Toolkit.Win32.UI.Controls nuget package to 4.0.1+