MicroSugarDeveloperOrg / Avalonia.WebView

MIT License
216 stars 39 forks source link

Actual url is redirected to another but Url property is not changed #63

Open SinoAHpx opened 4 months ago

SinoAHpx commented 4 months ago

I have binded my Url property to a TextBox, but when I get Url changed inside the WebView, the Url property is not changed. For example, I set Url to https://google.com initially, then I search random thing in google, apparently the "actual url" get changed, however the Url property will not be updated.

lostmsu commented 2 months ago

Just to clarify this happened to me on Windows (I did not test other platforms). Assigning Url would also only navigate the first time.

Here's my workaround:

public static class WebViewHacks {
    // https://github.com/MicroSugarDeveloperOrg/Avalonia.WebView/issues/63
    [SupportedOSPlatform("windows")]
    public static void Go(this WebView webView, Uri uri) {
        if (!OperatingSystem.IsWindows())
            throw new PlatformNotSupportedException();

        webView.PlatformWebView!.Navigate(uri);
    }

    // https://github.com/MicroSugarDeveloperOrg/Avalonia.WebView/issues/63
    [SupportedOSPlatform("windows")]
    public static Uri? GetUri(this WebView webView) {
        if (!OperatingSystem.IsWindows())
            throw new PlatformNotSupportedException();

        if (webView.PlatformWebView is null)
            throw new InvalidOperationException();

        if (webView.PlatformWebView is not WebView2Core platform)
            throw new PlatformNotSupportedException(webView.PlatformWebView.GetType().FullName);

        if (platform.CoreWebView2 is not { } coreWebView)
            throw new InvalidOperationException();

        return Uri.TryCreate(coreWebView.Source, UriKind.Absolute, out var uri) ? uri : null;
    }
}