0

I'm developing UWP app for Windows Phone, but I cannot even start, because I get strange error with WebView element. I also referenced Windows Mobile Extensions for the UWP

Here's a code:

XAML
<Page
    x:Class="App9.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App9"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid>
        <WebView x:Name="MyWebView" />
    </Grid>
</Page>

and

C#
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.Web.Http;

namespace App9
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            Uri authorize = new Uri("http://sub.mydomain.com", UriKind.Absolute);

            try
            {
                MyWebView.Navigate(authorize);
            }
            catch(Exception ex)
            {
                System.Diagnostics.Debug.Write(ex.ToString());
            }
        }
    }
}

and here's an Exception I get:

Exception thrown: 'System.NullReferenceException' in App9.exe
System.NullReferenceException: Object reference not set to an instance of an object.
   at App9.MainPage.<OnNavigatedTo>d__1.MoveNext()The program '[2912] BandTest.exe' has exited with code 0 (0x0).
Vlad
  • 667
  • 8
  • 19

1 Answers1

1

You're missing the InitializeComponent() call in your constructor. All XAML-defined elements are null, as long as you not call this method. Therefore, accessing MyWebView throws a NullReferenceException.

For more infos, take a look at this question: What does InitializeComponent() do, and how does it work in WPF?

Community
  • 1
  • 1
Herdo
  • 5,468
  • 2
  • 32
  • 59