【问题标题】:Xamarin.Forms MVVM WebView not LoadingXamarin.Forms MVVM WebView 未加载
【发布时间】:2025-12-13 07:25:01
【问题描述】:

我们使用 Xamarin.Forms WebView 来显示某个对象的内容,但是在我们旋转设备两次之前,该内容不会显示在 WebView 中。

.xmal-文件代码:

<WebView x:Name="WebView" HeightRequest="{Binding Height}" WidthRequest="{Binding Width}">
    <WebView.Source>
        <HtmlWebViewSource Html="{Binding Code}"/>
    </WebView.Source>
</WebView>

还有模型视图:

    private double _height;
    private double _width;
    public ICommand ItemTappedCommand { get; private set; }
    public string SearchBarLabelText { get; private set; }
    public object LastTappedItem { get; set; }
    public int ColumnCount { get; private set; } = 2;

    public string CodeName { get; private set; }
    public string CodeContent { get; private set; }

    public string Code { get; private set; }

    public double Height
    {
        get => _height;
        private set
        {
            if (_height == value)
                return;
            _height = value;
            OnPropertyChanged(nameof(Height));
        }
    }

    public double Width
    {
        get => _width;
        private set
        {
            if (_width == value)
                return;
            _width = value;
            OnPropertyChanged(nameof(Width));
        }
    }

    public ErrorcodeDetailPageViewModel(Errorcode code)
    {
        Device.Info.PropertyChanged += DevicePropertyChanged;
        DevicePropertyChanged(null, null);

        Code = code.Content;
        CodeName = code.Label;
        CodeContent = code.Content;


    }

    private void DevicePropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        switch (Device.Info.CurrentOrientation)
        {
            case DeviceOrientation.Portrait:
                Height = Device.Info.PixelScreenSize.Height - 150; // 120 is the Height of the ControlTemplate used on this Page + the top row on uwp; only required in portrait mode
                Width = Device.Info.PixelScreenSize.Width;
                break;

            case DeviceOrientation.Landscape:
                Height = Device.Info.PixelScreenSize.Height - 50;
                Width = Device.Info.PixelScreenSize.Width;
                break;
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

如前所述,如果我们将设备旋转两次,内容就会显示出来,否则就不会。我认为问题与我们的绑定有关...

我应该提到,我们在 WebView 顶部有一些类似的横幅/标题。

【问题讨论】:

  • Code 也应该有一个支持通知。

标签: xamarin mvvm webview binding


【解决方案1】:

Code 还应该有一个支持通知,以便视图知道它的值何时发生变化。

喜欢

private string code;

public string Code {
    get => code;
    private set {
        if (code == value)
            return;
        code = value;
        OnPropertyChanged(nameof(Code));
    }
}

如果你经历过为什么必须转两次的步骤,你会明白这就是你必须这样做的原因。

首次设置该值时为空白。

在第一轮,值由事件处理程序设置,而不是因为没有通知发送到视图,所以它没有明显改变。

在第二轮时,该值已设置,当视图重绘时,该值显示在视图中。

【讨论】:

  • 抱歉回复晚了!我已经实施了您的建议,但仍然无法加载...您对我们还有其他建议吗?
  • PropertyChangedEvent 被触发,但我认为这里的问题不是“代码”绑定,而是“高度”/“宽度”绑定。
  • @FelixFischer 在DevicePropertyChanged 中放置一个断点并调试调用它时发生的情况。
  • 它现在可以工作了!我只是在构造函数中添加了一些赋值: Code = code.Content;高度 = Device.Info.PixelScreenSize.Height - 150;宽度 = Device.Info.PixelScreenSize.Width;它正在显示 WebView 内容。谢谢您的帮助! :)
  • 设置值时检查方向以确保正确发送