o0r*_*s0o 7 c# xaml webview windows-phone-8.1
我是Windows手机开发的新手,上周为了这个目的买了一个Windows 8.1刚刚发布^^.
我正在尝试按照Microsoft初学者教程之一创建一个Minibrowser,这是一个非常简单的应用程序,带有文本框,按钮和webview.我意识到这是一个Windows Phone 8教程,但想到Windows 8.1肯定不能大不相同?
我已经完成了教程,但我遇到的问题是webview实际上并没有显示任何内容.

页面视图的xaml是
<Page
x:Class="MiniBrowser.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MiniBrowser"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid x:Name="LayoutRoot">
<Grid.ChildrenTransitions>
<TransitionCollection>
<EntranceThemeTransition/>
</TransitionCollection>
</Grid.ChildrenTransitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!-- TitlePanel -->
<StackPanel Grid.Row="0" Margin="24,17,0,28">
<TextBlock Text="My First Application" Style="{ThemeResource TitleTextBlockStyle}" Typography.Capitals="SmallCaps"/>
<TextBlock Text="Mini Browser" Margin="0,12,0,0" Style="{ThemeResource HeaderTextBlockStyle}"/>
</StackPanel>
<!--TODO: Content should be placed within the following grid-->
<StackPanel Grid.Row="1" x:Name="ContentRoot" Margin="12,0,12,12">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBox x:Name="URL"
Text="http://www.xbox.com"
TextWrapping="NoWrap"
Height="Auto"
Width="Auto"
HorizontalAlignment="Stretch"
VerticalAlignment="Center"
Margin="0,0,12,0"/>
<Button x:Name="Go"
Grid.Column="1"
Content="Go"
Height="Auto"
Width="Auto"
HorizontalAlignment="Stretch"
VerticalAlignment="Center"
Margin="12,0,0,0"
Click="Go_Click"/>
</Grid>
<WebView x:Name="MiniBrowser"
Height="Auto"
Width="Auto"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
ScrollViewer.ZoomMode="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Disabled"
Loaded="MiniBrowser_Loaded"
NavigationFailed="MiniBrowser_NavigationFailed" NavigationCompleted="MiniBrowser_NavigationCompleted"
Visibility="Visible"/>
</StackPanel>
</Grid>
</Page>
Run Code Online (Sandbox Code Playgroud)
以及按钮和webview的相关事件处理程序
private void Go_Click(object sender, RoutedEventArgs e)
{
String site = URL.Text;
MiniBrowser.Navigate(new Uri(site, UriKind.Absolute));
}
private void MiniBrowser_Loaded(object sender, RoutedEventArgs e)
{
String site = URL.Text;
MiniBrowser.Navigate(new Uri(site, UriKind.Absolute));
}
private void MiniBrowser_NavigationFailed(object sender, WebViewNavigationFailedEventArgs e)
{
ToastTemplateType toastTemplate = ToastTemplateType.ToastImageAndText01;
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate);
XmlNodeList toastTextElements = toastXml.GetElementsByTagName("text");
toastTextElements[0].AppendChild(toastXml.CreateTextNode("Hello World!"));
ToastNotification toast = new ToastNotification(toastXml);
ToastNotificationManager.CreateToastNotifier().Show(toast);
}
private void MiniBrowser_NavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)
{
ToastTemplateType toastTemplate = ToastTemplateType.ToastImageAndText01;
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate);
XmlNodeList toastTextElements = toastXml.GetElementsByTagName("text");
toastTextElements[0].AppendChild(toastXml.CreateTextNode("Nav Complete"));
ToastNotification toast = new ToastNotification(toastXml);
ToastNotificationManager.CreateToastNotifier().Show(toast);
}
}
Run Code Online (Sandbox Code Playgroud)
我可以从运行页面看到,在启动时和单击go按钮时,当弹出导航完成帖子时,正在调用导航完成事件,但不应加载不应加载的xbox网页.
我已经调整了稍微可以看到的演示中显示的xaml,所以我没有单独的ContentRoot网格,而是嵌套了包含地址TextBox和Button的水平堆栈面板,其中WebView位于其下方.
我也使用了WebView而不是手机中的WebBrowser,因为这在WP8.1中似乎不存在,所以这很可能是问题......
有没有人知道我做错了什么,或者任何人都可以发表评论,如果我使用的风格无论如何都可以改进.
学习新平台始终是一个挑战,因为您尝试着解决事情的不同方式; Windows Phone肯定与Android不同,我更熟悉!
提前致谢
编辑:
我尝试通过注释我对页面内容(文本框,按钮和webview)的内容以及从教程中插入示例xaml来更改XAML代码,如下所示
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<TextBox x:Name="URL" Margin="10,10,85,0" Text="http://www.xbox.com" VerticalAlignment="Top"/>
<Button x:Name="Go" Content="Go" HorizontalAlignment="Right" Margin="346,10,0,0" VerticalAlignment="Top"/>
<phone:WebBrowser x:Name="MiniBrowser" Margin="10,82,0,0"/>
</Grid>
Run Code Online (Sandbox Code Playgroud)
但我得到的问题与之前的问题完全相同,因为webview只显示黑色,而且通常看起来更糟糕......

我猜这个问题是由于预期的手机:我正在使用的WebBrowser和WebView之间的差异.
我找到并解决了这个问题.问题在于我在xaml中声明了webview:
<WebView x:Name="MiniBrowser"
Height="Auto"
Width="Auto"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
ScrollViewer.ZoomMode="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Disabled"
Loaded="MiniBrowser_Loaded"
NavigationFailed="MiniBrowser_NavigationFailed"
NavigationCompleted="MiniBrowser_NavigationCompleted"
Visibility="Visible"/>
Run Code Online (Sandbox Code Playgroud)
通过将其更改为
<WebView x:Name="MiniBrowser"
Height="425"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
ScrollViewer.ZoomMode="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Disabled"
Loaded="MiniBrowser_Loaded"
NavigationFailed="MiniBrowser_NavigationFailed"
NavigationCompleted="MiniBrowser_NavigationCompleted"
Visibility="Visible"/>
Run Code Online (Sandbox Code Playgroud)
它现在有效.出现问题是使用Height ="Auto".这是将高度设置为0,因此实际上并没有显示.

| 归档时间: |
|
| 查看次数: |
9112 次 |
| 最近记录: |