如何使用MVVM在WPF中显示网站?

时间:2017-02-27 21:43:49

标签: c# wpf mvvm

在你们说这是一个重复的请求之前,请听我说。我已尝试过如下问题中提到的解决方案:

WPF Web Browser Control

但它并没有像我一样工作,而是显示网页,它将网址显示为纯文字。

这是我的代码:

xmlns:resources="clr-namespace:MyApplication.Resources"

<Grid> <WebBrowser resources:WebBrowserUtility.BindableSource="{Binding ReportPage}"/> </Grid>

public static class WebBrowserUtility
{
    public static readonly DependencyProperty BindableSourceProperty =
                      DependencyProperty.RegisterAttached("BindableSource", typeof(string),
                      typeof(WebBrowserUtility), new UIPropertyMetadata(null,
                      BindableSourcePropertyChanged));

    public static string GetBindableSource(DependencyObject obj)
    {
        return (string)obj.GetValue(BindableSourceProperty);
    }

    public static void SetBindableSource(DependencyObject obj, string value)
    {
        obj.SetValue(BindableSourceProperty, value);
    }

    public static void BindableSourcePropertyChanged(DependencyObject o,
                                                     DependencyPropertyChangedEventArgs e)
    {
        var webBrowser = (WebBrowser)o;
        if(webBrowser != null)
        {
            string uri = e.NewValue as string;
            webBrowser.Source = !String.IsNullOrEmpty(uri) ? new Uri(uri) : null;
        }

    }
}

public class ReportViewModel: ViewModelBase
{

    public ReportViewModel()
    {
        ReportPage = new Uri(@"https://www.google.com");
    }
    private Uri _reportPage;
    public Uri ReportPage
    {
        get
        {
            return _reportPage;
        }

        set
        {
            if (_reportPage != value)
            {
                _reportPage = value;
                OnPropertyChanged("ReportPage");
            }
        }
    }
}

但是,在视图中,而不是实际打开&#34; google&#34;网站,它只显示URI:https://www.google.com

0 个答案:

没有答案